VideoView in Android is not playing video correctly - java

I am having a strange issue playing a VideoView. I have done my best to simplify the code as much as possible. Below are 4 classes: A, MirrorActivity, Replay, and MyTask. Assume the following occurs in this order:
A is created.
MirrorActivity() is created.
Inside MirrorActivity()'s onCreate(), that instance calls A's setMirrorActivity() to allow A to have a reference to it.
A's doThis() method is called, which executes mirrorActivity.playVideo().
playVideo() is executed.
Replay's executeVideo() is called.
MyTask is executed.
For some strange reason, when the above is executed, the video does not play. However, when the myButton ImageButton is pressed inside MirrorActivity, it plays the video on command. Both of these seem to be doing the same thing by calling MirrorActivity's playVideo(). Do you know why the above does not execute?
A
public class A{
private static final A instance = new A();
private MirrorActivity mirrorActivity;
public static A getInstance() {
return instance;
}
public void setMirrorActivity(MirrorActivity mirrorActivity) {
this.mirrorActivity = mirrorActivity;
}
public void doThis(String url){
mirrorActivity.playVideo(String url);
}
}
MirrorActivity
public class MirrorActivity extends Activity {
public static String VIDEO_URL = "example.mp4";
public VideoView mVideoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overlay_gradient);
// Set Mirror Activity
A.getInstance().setMirrorActivity(this);
mVideoView = (VideoView) findViewById(R.id.mirrorVideoView);
MyTask vTask = new MyTask(mVideoView);
ImageButton myButton = (ImageButton) findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
MirrorActivity.this.playVideo(MirrorActivity.VIDEO_URL);
}
});
}
public void playVideo(String videoURL)
{
MyTask mt = new MyTask(mVideoView);
Replay.executeVideo(MirrorActivity.VIDEO_URL,
this,
mVideoView,
mt);
}
}
Replay
public class Replay{
public static void executeVideo(String uri, Activity activity, VideoView vid, MyTask mt)
{
vid.setMediaController(new MediaController(activity););
vid.setVideoURI(Uri.parse(uri));
mt.execute();
}
}
MyTask
public class MyTask extends AsyncTask<Void, Integer, Void> {
private VideoView video;
private int duration = 0; // in milliseconds
public MyTask(VideoView vid) {
video = vid;
}
#Override
protected Void doInBackground(Void... params) {
video.start();
video.requestFocus();
video.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
duration = video.getDuration();
}
});
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
}

I think UI element cannot access in doInBackground . And also start() after every declaration.
Try this method too.
private MediaController ctlr;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_overlay_gradient);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
// Set Mirror Activity
A.getInstance().setMirrorActivity(this);
mVideoView = (VideoView) findViewById(R.id.mirrorVideoView);
Uri uri=Uri.parse(videourl or video path);
mVideoView .setVideoURI(uri);
mVideoView .setVideoPath(videourl);
ctlr=new MediaController(this);
ctlr.setAnchorView(video);
ctlr.setMediaPlayer(video);
mVideoView .setMediaController(ctlr);
mVideoView .requestFocus();
mVideoView .start();
}

Omg. I believe I found the solution. I'm sorry to those who were investigating, but here is what I did to get it to work both ways. Pressing imageButton and calling A.doThis() will be able to play the video now.
I modified MirrorActivity's playVideo() function to use the following:
public void playVideo(final String videoURL)
{
Runnable runnable = new Runnable(){
#Override
public void run()
{
MyTask mt = new MyTask(mVideoView);
Replay.executeVideo(videoURL,
MirrorActivity.this,
mVideoView,
mt);
}
});
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(runnable);
}

Related

Android AsyncTask : UI operation not working in Android from Background Task

What i want :
wait for connection. Until the connection is received, display a
blank screen
as soon as connected, set the current view to a custom view ( using setContentView() ). This I'm trying to do from Background Thread( AsyncTask )
Then after that I'll keep accepting the points from client and keep updating them on UI thread. For this I'm accepting points from client and sending to publishProgess()
But for some reason, the second step itself isn't working.
MainActivity.java
public class MainActivity extends AppCompatActivity {
public ServerSocket server;
public Socket socket;
public Slate slate ;
private Paint mPaint;
public static MainActivity context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
(new ServerTask()).execute();
}
public static MainActivity getContext()
{
return context;
}
public void start_slate(){
slate = new Slate(this);
slate.setBackgroundColor(Color.parseColor("#000000"));
setContentView(slate);
}
public class Slate extends View {
.....
.....
draw(){
....
....
}
} /*custom drawing view*/
}
Background Task
/*Background Task*/
class ServerTask extends AsyncTask<Void, Action, Void>
{
private WeakReference<MainActivity> mainActivity;
private Action action;
#Override
protected void onPreExecute() {
super.onPreExecute();
mainActivity = new WeakReference<>(MainActivity.getContext());
}
#Override
protected Void doInBackground(Void... voids) {
ObjectInputStream stream;
try{
mainActivity.get().server = new ServerSocket(3107);
mainActivity.get().socket = mainActivity.get().server.accept();
stream = new ObjectInputStream(mainActivity.get().socket.getInputStream());
MainActivity.getContext().runOnUiThread(new Runnable() {
#Override
public void run() {
MainActivity.getContext().start_slate();
}
}); /*BUT THE VIEW IS NOT CHANGING*/
while(true)
{
if(isCancelled()){
break;
}
action = (Action)stream.readObject();
publishProgress(action);
}
}catch (Exception exception)
{
exception.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(Action... actions) {
super.onProgressUpdate(actions);
((mainActivity.get()).slate).draw(actions[0]);
}/*this will send the co-ordinates to current drawing view*/
}
NOTE : even if i try to set the view in onProgressUpdate() , it still does not happen.
It happens only in onPostExecute(). But I don't want that because, after changing the view, I still have to accept coordinates (The while loop part)

AsyncTask data communication with nested Classes

I have a specific scenario and I need your help.
I'm trying to build an App in Android that involves network communication.
I am using AsyncTask for the http POST requests.
I have another class called Proxy (not a good one.. will be changed) which holds different kinds of functionalities (registerUser, setUserName, getUserPermission...)
And Of course, I have an Activity.
My Activity holds an instance of Proxy class.
My goal, is to push a button in the activity, it will call a method from Proxy class, which in its turn calls the AsyncTask's execute() method that actually run the http POST.
I was wondering how to get the data from AsyncTask's onPostExecute to my activity.
What I have in mind is to have an interface in AsyncTask, which will be implemented in Proxy class, and another interface in Proxy class which will be implemented in my Activity class.
Roll the data all the way to my Activity.
I want to hear your thoughts about whether this is the way to go, or another approach is preffered.
Thanks a lot for your help.
Adding some code
public class RegisterActivity extends FragmentActivity implements Proxy.OnProxyHttpPostResponseListener {
private Proxy proxy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
this.proxy = new Proxy();
this.proxy.setHttpPostResponseListener(this);
}
#Override
public void onProxyHttpPostResponse(String response) {
//Do something when http post returns
}
}
public class Proxy {
public interface OnProxyHttpPostResponseListener {
void onProxyHttpPostResponse(String response);
}
private OnProxyHttpPostResponseListener httpPostResponseListener;
public void setHttpPostResponseListener(OnProxyHttpPostResponseListener listener) {
this.httpPostResponseListener = listener;
}
private class HttpPostAsync extends AsyncTask<Pair<String, ArrayList<Pair<String, String>>>, Void, String> {
#Override
protected String doInBackground(Pair<String, ArrayList<Pair<String, String>>>... params) {
return this.httpPost(params[0].first, params[0].second);
}
protected void onPostExecute(String response) {
httpPostResponseListener.onProxyHttpPostResponse(response);
}
}
If you're just needing HTTP POST functionality then an AsyncTask might not be the best choice. AsyncTask really shines if you need to get progress updates as the task is executing (with onProgressUpdate(Progress... progress)). If you'd like to use AsyncTask nonetheless, iroiroys' reply should help.
A bit more simply, you could just use a Handler thread straight up. Something like this:
public class HandlerExampleActivity extends Activity {
private Button postButton;
private Button getButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handler_example);
backgroundThread = new BackgroundThread();
backgroundThread.start();
postButton = (Button) findViewById(R.id.button_post);
postbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
backgroundThread.post("DATA_HERE");
}
});
getButton = (Button) findViewById(R.id.button_get);
getbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
backgroundThread.get("URL_HERE");
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
backgroundThread.exit();
}
private class BackgroundThread extends Thread {
private Handler backgroundHandler;
public void run() {
Looper.prepare();
backgroundHandler = new Handler();
Looper.loop();
}
public void post(DataType data) {
backgroundHandler.post(new Runnable() {
#Override
public void run() {
// pull data and do the POST
uiMsg = uiHandler.obtainMessage(POST_COMPLETE, whatever_data_passing_back, 0, null);
uiHandler.sendMessage(uiMsg);
}
});
}
public void get(URL data) {
backgroundHandler.post(new Runnable() {
#Override
public void run() {
// GET data
uiMsg = uiHandler.obtainMessage(GET_COMPLETE, whatever_data_passing_back, 0, null);
uiHandler.sendMessage(uiMsg);
}
});
}
public void exit() {
backgroundHandler.getLooper().quit();
}
}
private final Handler uiHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case POST_COMPLETE:
// handle it
break;
case GET_COMPLETE:
// handle it
break
case MESSAGE_BACK_TO_UI_THREAD:
// do something
break;
case OPERATION_FAIL:
// oh no!
break;
case OPERATION_SUCCESS:
// yay!
break;
}
}
};
}
I suggest you try Handler and Handler.Callback.
Below I made it simple example..
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
public class MainActivity extends Activity implements Callback {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler(this);
Proxy proxy = new Proxy(handler);
proxy.foo();
}
private class Proxy {
Handler handler;
public Proxy(Handler handler) {
this.handler = handler;
}
private void foo() {
new myAsync().execute();
}
private class myAsync extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Message msg = handler.obtainMessage();
msg.obj = result;
handler.sendMessage(msg);
}
}
}
#Override
public boolean handleMessage(Message msg) {
// Handle Message here!
return false;
}
}

Android: Why does my String value not persist when I rotate Android device? However the countdown portion does

I am extending the Application class to do 2 things. 1) Make onPause() stop the CounterThread until onResume() is pressed. 2) To make the threads continue working while I change orientation of the phone from portrait mode to landscape. Both of the above statements work, however the MakingFileThread when it returns a string value to set the TextView does not persist the orientation changes. Help!!
public class MainActivity extends Activity {
private TextView mText;
private EditText mUserInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mText = (TextView)findViewById(R.id.text);
mUserInput = (EditText)findViewById(R.id.userInput);
}
#Override
public void onPause(){
super.onPause();
MyApplication app = (MyApplication)getApplication();
app.pause(null);
}
#Override
public void onResume(){
super.onResume();
MyApplication app = (MyApplication)getApplication();
app.resume(this);
}
public void button_handler(View v){
String val = mUserInput.getText().toString();
String file = "myfile.txt";
MyApplication app = (MyApplication)getApplication();
app.MakeFileThread(val,file);
app.startCounterThread();
}
public void updateCountDown(final int seconds){
String secondText = String.valueOf(seconds);
mText.setText(secondText);
}
public void updateFileOutput(final String userText){
mText.setText(userText);
}
}
public class CounterApplication extends Application {
private MainActivity currentActivity = null;
private CounterThread myThread;
public synchronized void resume(MainActivity update){
this.currentActivity = update;
myThread.interrupt();
}
public synchronized void pause(MainActivity update){
this.currentActivity = update;
myThread.interrupt();
}
public synchronized void update(final long seconds){
Runnable notifyAction = new Runnable(){
public void run(){
if(currentActivity != null){
currentActivity.updateResult(seconds);
}
}
};
currentActivity.runOnUiThread(notifyAction);
}
public void onCreate(){
super.onCreate();
myThread = new CounterThread();
myThread.start();
}
private class CounterThread extends Thread{
private long seconds = 0;
private boolean running = false;
#Override
public void run(){
while(true){
try{
Thread.sleep(running?1000:100000);
}
catch(InterruptedException e){
running = !running;
}
if(interrupted()){
running = !running;
}
if(running){
update(++seconds);
}
}
}
}
}
I am not sure, this might help you, add the following line in your manifest file for the activities you need to work in both portrait and landscape:
android:configChanges="orientation|screenSize"

How do I release my media player after it is done playing?

I am creating a sound board and after clicking about 30 different sounds it stops working; I believe android is running out of memory. Below is my code. How can I implement .release() so that when the sound is done playing it is released? I don't really care if two things play at the same time; the clips are t0o short for this to be possible. I would just like to get my code set.
public class soundPageOne extends Activity {
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.soundsone);
final MediaPlayer pg1 = MediaPlayer.create(this, R.raw.peter1);
Button playSound1 = (Button) this.findViewById(R.id.peter1Button);
playSound1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pg1.start();
}
});
I have done a lot of searching around but due to my lack of java/android knowledge I have not been able to get anything to work. Thanks in advance, let me know if anyone needs anymore code.
I left a comment, but I'll post an answer to show what I mean anyway...
The idea is that you have a set number of MediaPlayer instances that you can use. That way you never exceed the maximum number of instances. The array should be the length of the number of concurrent sounds you expect to be able to hear. If the sounds are local files, the length of time it takes to prepare the sounds should be almost negligible, so calling create inside the click handler should not result in terrible performance. Each of your buttons is associated with a particular resource, I suppose, so I set up a helper method to create and play the sounds for each button in the same way.
public class soundPageOne extends Activity {
private MediaPlayer[] mPlayers = new MediaPlayer[2];
private int mNextPlayer = 0;
#Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.soundsone);
Button playSound1 = (Button)this.findViewById(R.id.peter1Button);
playSound1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startSound(R.raw.peter1);
}
});
}
public void onDestroy() {
super.onDestroy(); // <---------------------- This needed to be there
for (int i = 0; i < mPlayers.length; ++i)
if (mPlayers[i] != null)
try {
mPlayers[i].release();
mPlayers[i] = null;
}
catch (Exception ex) {
// handle...
}
}
private void startSound(int id) {
try {
if (mPlayers[mNextPlayer] != null) {
mPlayers[mNextPlayer].release();
mPlayers[mNextPlayer] = null;
}
mPlayers[mNextPlayer] = MediaPlayer.create(this, id);
mPlayers[mNextPlayer].start();
}
catch (Exception ex) {
// handle
}
finally {
++mNextPlayer;
mNextPlayer %= mPlayers.length;
}
}
}
Create a class, say AudioPlayer with a SoundPool variable. Setup a constructor to initialise the AudioPlayer object and create a Play method. SoundPool works better for short sounds played many times and does not require you to release.
public class AudioPlayer {
private SoundPool sPool = new SoundPool(Integer.MAX_VALUE, AudioManager.STREAM_MUSIC, 0);
public AudioPlayer(Context c, int id){
sounds.put("1",sPool.load(c, id, 1));
}
public void play(Context c) {
sPool.play("1", 1, 1, 1, 0, 1f);
}
}
So your class should look like
public class soundPageOne extends Activity {
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.soundsone);
final AudioPlayer ap = new AudioPlayer(this, R.raw.sound);
Button playSound1 = (Button) this.findViewById(R.id.peter1Button);
playSound1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ap.play();
}
});
Could you use a MediaPlayer.OnCompletionListener?
Something like:
public class soundPageOne extends Activity implements MediaPlayer.OnCompletionListener {
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.soundsone);
final MediaPlayer pg1 = MediaPlayer.create(this, R.raw.peter1);
//***set the listener here***
pg1.setOnCompletionListener(this);
Button playSound1 = (Button) this.findViewById(R.id.peter1Button);
playSound1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pg1.start();
}
});
}
//***this code will be executed once the sound finishes playing***
#Override
public void onCompletion(MediaPlayer mp) {
//log messages, other things can go here
mp.release();
}
Try something like this
Your activity class:
public class soundPageOne extends Activity {
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.soundsone);
final AudioPlayer pg1 = new AudioPlayer();
Button playSound1 = (Button) this.findViewById(R.id.peter1Button);
playSound1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pg1.play(this, R.raw.sound);
}
});
}
This is another Java Class:
public class AudioPlayer {
private MediaPlayer mPlayer;
public void stop() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
public void play(Context c, int sound) {
stop();
mPlayer = MediaPlayer.create(c, sound);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
stop();
}
});
mPlayer.start();
}
public boolean isPlaying() {
return mPlayer != null;
}
}

How to implement handler in another class?

In my app I have three buttons and when one button is clicked it calls a thread to start thing is i want to be able to input in edittext string in to the threads and the do some work to it then have it returned to the UI Thread where i can display it or put it into an opengl to display an Object. I've read up on Handles and im not sure i fully understand them and maybe if anyone knows a way to make my own handler code. Also I've read up on Async and I dont think it would benifit my app.(personal opion if it would benifit my app let me know) My question is how would i get the info from UI edittext when enter is pressed to the line thread in DrawingUtils class then work is done to it then it comes back to the UI to be ethier displayed or inputed in an openGl program?
Here is MainActivity class:
public class MainActivity extends Activity implements OnClickListener {
EditText cl;
TextView info;
Button enter;
Button line;
Button arc;
Line callLine = new DrawingUtils.Line();
Enter callEnter = new DrawingUtils.Enter();
Arc callArc = new DrawingUtils.Arc();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
info = (TextView) findViewById(R.id.info);
enter = (Button) findViewById(R.id.enter);
line = (Button) findViewById(R.id.line);
arc = (Button) findViewById(R.id.arc);
Handler UIhandler = new Handler() {
#Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
String string = bundle.getString("myKey");
}
};
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.enter:
callEnter.start();
break;
case R.id.line:
callLine.start();
break;
case R.id.arc:
callArc.start();
break;
}
};
}
Here is DrawingUtils class:
public class DrawingUtils {
MainActivity handle = new MainActivity();
// Thread classes for buttons
public static class Enter extends Thread {
Thread enter = new Thread() {
public void run() {
}
};
public static class Line extends Thread {
Thread line = new Thread() {
public void run() {
}
};
}
public static class Arc extends Thread {
Thread arc = new Thread() {
public void run() {
}
};
}
}
}
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstance) {
// ....
Handler myHandler = new Handler() {
#Override
public void handleMessage (Message msg) {
doCoolStuffWhenMessageReceived();
}
}
MySecondClass secondClass = new MySecondClass(myHandler);
// ....
}
}
public class MySecondClass {
private handler;
public MySecondClass(Handler handler){
this.handler = handler;
}
private void someMethodToCallActivity() {
handler.sendEmptyMessage(0);
}
}

Categories

Resources