Skip to content

Instantly share code, notes, and snippets.

@ih8legal
Last active July 18, 2016 22:29
Show Gist options
  • Save ih8legal/a4597931aa668cb8fe8939c86d00db60 to your computer and use it in GitHub Desktop.
Save ih8legal/a4597931aa668cb8fe8939c86d00db60 to your computer and use it in GitHub Desktop.
For reddit eyes only
package com.example.android.summonthefirelord;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.VideoView;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
Checker checker = new Checker();
VideoView card;
MediaPlayer mp = new MediaPlayer();
String video;
//this are where the url of the ogg files will be stored later
String play1;
String dataSource = "http://www.hearthpwn.com/cards/391-tirion-fordring";
//these are the element names
String play1El = "audio[id=soundPlay1]";
//Loading screen
private ProgressDialog progressDialog;
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.mylayout);
linearLayout.setVisibility(View.INVISIBLE);
new getVideoLink().execute();
new getSoundLinks().execute();
}
// This one gets the image url
private class getVideoLink extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Connection connect = Jsoup.connect(dataSource);
connect.userAgent("Mozilla/5.0");
//Get the html and select the first <video class="hscard-video" ...
Document doc = connect.get();
Element videoEl = doc.select("video.hscard-video").first();
//Grab all the data from it as a map (ex. data-href, data-usegold...)
Map<String, String> dataSet = videoEl.dataset();
//If data-animationurl exists, print it (here you can store it as a String instead
if (dataSet.containsKey("animationurl")) {
MainActivity.this.video = (dataSet.get("animationurl"));
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
card = (VideoView) findViewById(R.id.video);
card.setVideoPath(video);
//Video Loop
card.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
card.start();
// Set video into TextView
}
}
private class getSoundLinks extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
progressDialog = ProgressDialog.show(MainActivity.this,"Loading...",
"Shovelling coal into the server...", false, false);
}
@Override
protected Void doInBackground(Void... params) {
try {
Connection connect = Jsoup.connect(dataSource);
connect.userAgent("Mozilla/5.0");
//Get the webpage
Document doc = connect.get();
//select the first sound
Element playSoundEl = doc.select(play1El).first();
//check if it exists
if (checker.exists(playSoundEl, play1El)) {
//if it does select it and set it as the source for the playsound1 button
play1 = doc.select(play1El).first().attr("src");
} else {
play1 = null;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (play1 != null) {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.buttonPanel);
final Button btn = new Button(MainActivity.this);
btn.setText("Play");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.reset();
mp.setDataSource(play1);
mp.prepareAsync();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
try{
linearLayout.addView(
btn,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
TextView textView = (TextView) findViewById(R.id.test);
textView.setText(play1);
linearLayout.setVisibility(View.VISIBLE);
progressDialog.dismiss();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment