无法停止活动android

时间:2013-09-12 14:49:58

标签: android call android-mediaplayer

收到电话时,MediaPlayer不会暂停。所以我添加了on onStop,它可以工作但是如果我尝试回到操作栏中的后退按钮或后退按钮转到应用程序的主菜单,它就会崩溃。如果有人能提供帮助就会很棒!只有初学者

public class player1 extends Activity implements Runnable {


private  MediaPlayer mp;
private ProgressBar progressBar;
private ImageButton pauseicon;
private final int NUM_SOUND_FILES = 3;  //*****REPLACE THIS WITH THE ACTUAL NUMBER OF SOUND FILES YOU HAVE*****
private int mfile[] = new int[NUM_SOUND_FILES];
private Random rnd = new Random();
boolean flag = true;

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player_1);
        pauseicon = (ImageButton) findViewById(R.id.pauseicon);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        getActionBar().setDisplayHomeAsUpEnabled(true);



        mfile[0] = R.raw.sound01;  //****REPLACE THESE WITH THE PROPER NAMES OF YOUR SOUND FILES
        mfile[1] = R.raw.sound02;  //PLACE THE SOUND FILES IN THE /res/raw/ FOLDER IN YOUR PROJECT*****
        mfile[2] = R.raw.sound03;
        // Listeners
        /**
         * Play button click event
         * plays a song and changes button to pause image
         * pauses a song and changes button to play image
         * */


        try{
             mp = MediaPlayer.create(player1.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
             mp.seekTo(0);
             mp.start(); ;         
             progressBar.setVisibility(ProgressBar.VISIBLE);
             progressBar.setProgress(0);
             progressBar.setMax(mp.getDuration());
             new Thread(this).start();

         } catch (IllegalArgumentException e) {
             e.printStackTrace();
         } catch (IllegalStateException e) {
             e.printStackTrace();
         }


        mp.setOnCompletionListener(new OnCompletionListener() {

            public void onCompletion(MediaPlayer mp) {
                pauseicon.setImageResource(R.drawable.playicon);
              mp.seekTo(0);

            }
        });

        pauseicon.setOnClickListener(new View.OnClickListener() {

               public void onClick(View v) {
                  if(mp.isPlaying()) {
                     mp.pause();
                     ((ImageButton) v).setImageResource(R.drawable.playicon);
                  } else {
                     mp.start();
                     ((ImageButton) v).setImageResource(R.drawable.pauseicon);

                  }

               }
            });
   }
   static boolean runThread = true;
   public void run() {
        while ( runThread )  {
              int currentPosition=0;
              int total = mp.getDuration();
              if ( mp != null && currentPosition <= total )  {
              try {
                 Thread.sleep(1000);
                 currentPosition= mp.getCurrentPosition();
              } catch (InterruptedException e) {
                 return;
              } catch (Exception e) {
                 return;
              }            
              progressBar.setProgress(currentPosition);
           } else
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        runThread = false; 
    }
   @Override
   protected void onStop() {
       super.onStop();
       if (mp != null && mp.isPlaying()){
           mp.pause();
       }
   }


   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);


        if (mp != null)
        if(mp.isPlaying())
              mp.stop();

          mp.release();

            return true;
        default:
            return super.onOptionsItemSelected(item);

    }

       }
    @Override 
    public void onBackPressed(){
      if (mp != null){
          if(mp.isPlaying())
              mp.stop();

          mp.release();
      }

      //there is no reason to call super.finish(); here
      //call super.onBackPressed(); and it will finish that activity for you
      super.onBackPressed(); 
    }

    }

错误

  09-12 14:52:49.755: E/AndroidRuntime(26104): FATAL EXCEPTION: main
  09-12 14:52:49.755: E/AndroidRuntime(26104): java.lang.RuntimeException: Unable to stop activity {com.apps.seconds/com.apps.seconds.player1}: java.lang.IllegalStateException
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3463)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3517)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at android.app.ActivityThread.access$1200(ActivityThread.java:141)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at android.os.Handler.dispatchMessage(Handler.java:99)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at android.os.Looper.loop(Looper.java:137)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at android.app.ActivityThread.main(ActivityThread.java:5103)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at java.lang.reflect.Method.invokeNative(Native Method)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at java.lang.reflect.Method.invoke(Method.java:525)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at dalvik.system.NativeStart.main(Native Method)
  09-12 14:52:49.755: E/AndroidRuntime(26104): Caused by: java.lang.IllegalStateException
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at android.media.MediaPlayer.isPlaying(Native Method)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at com.apps.seconds.player1.onStop(player1.java:154)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1212)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at android.app.Activity.performStop(Activity.java:5275)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3458)
  09-12 14:52:49.755: E/AndroidRuntime(26104):   ... 11 more

3 个答案:

答案 0 :(得分:1)

不要使用此

  @Override 
    public void onBackPressed(){
      if (mp != null){
          if(mp.isPlaying())
              mp.stop();

          mp.release();
      }

你可以在简历上使用恢复音乐播放

protected void onResume()
    {   
        // TODO Auto-generated method stub
        super.onResume();
    }

你必须学习LifeCycle活动 或参考我的答案here

答案 1 :(得分:0)

这里有一些解释: onPause() Android

或在官方网站上,您拥有非常有用的活动工作流程

答案 2 :(得分:0)

  

09-12 14:52:49.755:E / AndroidRuntime(26104):引起:   java.lang.IllegalStateException 09-12 14:52:49.755:   E / AndroidRuntime(26104):at   android.media.MediaPlayer.isPlaying(原生方法)

发生了{p> IllegalStateException异常,因为当您按下后,内部播放器已经被释放,之后您正在调用isPlaying ..

Refer here