Music played using asyncTask does not stop using undo

I use music in the background of my activity. But when I try to cancel it, it does not work. Music just works continuously until it ends. Below is the code:

public class xx extends Activity { BackgroundSound mBackgroundSound = new BackgroundSound(); @Override protected void onCreate(Bundle savedInstanceState) { .... } @Override protected void onResume() { super.onResume(); mBackgroundSound.execute(); } @Override protected void onPause() { super.onPause(); mBackgroundSound.cancel(true); } 

and to select the options menu:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: mBackgroundSound.cancel(true); NavUtils.navigateUpFromSameTask(this); return true; case R.id.menu_Add: { mBackgroundSound.cancel(true); Intent intent = new Intent(xx.this,yy.class); intent.putExtra("flag", "add"); intent.putExtra("AddObj", "mm"); startActivity(intent); break; } case R.id.menu_list_quote: { mBackgroundSound.cancel(true); Intent intent = new Intent(xx.this,zz.class); intent.putExtra("Obj", "nn"); startActivity(intent); break; } } //return true; return super.onOptionsItemSelected(item); } 

and asynTask:

  public class BackgroundSound extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { try { while( !isCancelled()) { // FileDescriptor afd = openFd("cock_alarm.mp3"); MediaPlayer player = new MediaPlayer(); player.setDataSource(_musicFilePath); player.prepare(); //player.setLooping(true); // Set looping player.setVolume(100,100); player.start(); // if(isCancelled()) // player.stop(); } } catch(Exception exp) { exp.printStackTrace(); } return null; } } 

Also tried using for loop:

  for(int i=0;i<100 && !isCancelled();i++) 

and tried this inside the try asyncTask block:

 if(isCancelled()) player.stop(); 

How can I solve it?

0
source share
2 answers

Instead of creating AsyncTask, why not just create a MediaPlayer and start it from your activity?

MediaPlayer has built-in flow logic. You do not need to create a stream to control the media player. You can read here: http://developer.android.com/guide/topics/media/mediaplayer.html

In your work you can do the following:

 private MediaPlayer mMediaPlayer; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initalize the media player mMediaPlayer = ... however you are initializing it ...; // Set the listener so the media player can tell you when it is finished preparing mMediaPlayer.setOnPreparedListener(this); // Prepare the MediaPlayer asynchronously so that the UI thread does not lock up mMediaPlayer.prepareAsync(); } // You need to listen for when the Media Player is finished preparing and is ready public void onPrepared(MediaPlayer player) { // Start the player player.start(); } 

Then, when you need to stop the player, just call

 mMediaPlayer.stop(); 
+5
source

How, vogella explains this on his website: "AsyncTask does not automatically process configuration changes, ie If activity is recreated, the programmer must process this in his own encoding.

A common solution is to declare AsyncTask in a saved headless fragment.

Look for the full text for: http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html#androidbackground

+1
source

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


All Articles