音乐使用AsyncTask的不是用取消停止打 [英] Music played using asyncTask isn't stopping by using cancel

查看:125
本文介绍了音乐使用AsyncTask的不是用取消停止打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的活动的背景音乐。但是,当我试图取消它不工作。音乐只是连续运行,直到其完成自身。下面是code:

I am using a music in background of my activity. But when i am trying to cancel it its not working. The music is just continuously running until it finishes itself. 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 for options menu selection:

@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);
}

和asynTask:

and the 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++)

和尝试的AsyncTask的这里面try块:

and tried this inside try block of asyncTask:

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

我应该如何去解决呢?

How am i going to solve it?

推荐答案

,为什么不创建的MediaPlayer,并从活动开始了吗?

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

在MediaPlayer有内置到它自己的线程的逻辑。你并不需要创建一个线程来管理媒体播放器。你可以在这里阅读更多: http://developer.android.com/guide/topics /media/mediaplayer.html

The MediaPlayer has its own threading logic built into it. You don't need to create a thread to manage the media player. You can read more here: http://developer.android.com/guide/topics/media/mediaplayer.html

在你的活动,你可以做到以下几点:

In your activity, you could 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 whenever you need to stop the player, simply call

mMediaPlayer.stop();

这篇关于音乐使用AsyncTask的不是用取消停止打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆