How to change image See when sound ends in streams

I need to change the ImageView when the sound ends, but when I try to use the stream to use it to freeze the screem, the application closes;

mp = MediaPlayer.create(this, R.raw.ave_maria); mp.start(); im = (ImageView) findViewById(R.id.imag1); Thread thread = new Thread() { public void run() { while (mp.isPlaying()) { } im.setImageResource(R.drawable.primeiro_misterio_gozoso07); } }; thread.start(); 
+4
source share
4 answers

You must synchronize the code with the stream user interface.

 mp = MediaPlayer.create(this, R.raw.ave_maria); mp.start(); im = (ImageView) findViewById(R.id.imag1); Thread thread = new Thread() { public void run() { while (mp.isPlaying()) { } //YouActivity YouActivity.this.runOnUiThread(new Runnable() { public void run() { im.setImageResource(R.drawable.primeiro_misterio_gozoso07); } }); } }; thread.start(); 
+2
source

Use OnCompletionListener on mp object:

 mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // SET THE BACKGROUND HERE } }); 
+4
source

You cannot change the user interface from a background thread. MediaPlayer also provides an OnCompletionListener interface that you should probably use instead of a while loop. Infact, if you use onCompletionListener, you donโ€™t even have to put this in your own stream, so you donโ€™t have to worry about how to change the user interface.

something like this should work fine:

 mp.setOnCompletionListner(new OnCompletionListner() { public void onCompletion(MediaPlayer m){ im.setImageResource(R.drawable.primeiro_misterio_gozoso07); } }); 
+1
source

You can use AsyncTask, this is a very useful class from Android and gives you the opportunity to override 3 methods (onPreExecute, doInBackground and onPostExecute).

You can use doInBackground during audio playback and onPostExecute to set the image.

Even better, you can use onPreExecute to display some nice things in the progress window or something like that.

For AsyncTask see: http://developer.android.com/reference/android/os/AsyncTask.html

0
source

Source: https://habr.com/ru/post/1447988/


All Articles