当用户按下后退键时声音不会停止

时间:2013-07-14 23:04:50

标签: android audio playback

我是android编程的新手。我有一个媒体播放器的以下代码。当用户按下后退键或进入另一个活动时,声音不会停止。

有什么问题?我有onDestory方法,但我的问题存在!

    public class Elementary1Activity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{
        private SeekBar seekBar;
        private ImageButton startMedia;
        private ImageButton pauseMedia;
        private MediaPlayer mp;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.elementary1);             


            AudioControl();    

        }

        public void AudioControl(){
            seekBar = (SeekBar) findViewById(R.id.seekBarnew2);
            startMedia = (ImageButton) findViewById(R.id.playbuttonnew2);
            pauseMedia = (ImageButton) findViewById(R.id.pausebuttonnew2);
            seekBar.setOnSeekBarChangeListener(this);
            startMedia.setOnClickListener(this);
            pauseMedia.setOnClickListener(this); 
        }    

        public void run() {
            int currentPosition= 0;
            int total = mp.getDuration();
            while (mp!=null && currentPosition<total) {
                try {
                    Thread.sleep(1000);
                    currentPosition= mp.getCurrentPosition();
                } catch (InterruptedException e) {
                    return;
                } catch (Exception e) {
                    return;
                }            
                seekBar.setProgress(currentPosition);
            }
        }

        public void onClick(View v) {
            pauseMedia.setVisibility(View.VISIBLE);
            seekBar.setVisibility(View.VISIBLE);
            startMedia.setVisibility(View.INVISIBLE);
            if (v.equals(startMedia)) {
                if (mp != null && mp.isPlaying()) return;
                if(seekBar.getProgress() > 0) {
                    mp.start();
                    return;
                }
                mp = MediaPlayer.create(Elementary1Activity.this, R.raw.e1);
                mp.start();            

                seekBar.setProgress(0);
                seekBar.setMax(mp.getDuration());
                new Thread(this).start();
            }

            if (v.equals(pauseMedia) && mp!=null) {
                mp.pause();
                pauseMedia.setVisibility(View.INVISIBLE);
                startMedia.setVisibility(View.VISIBLE);

            }       

        }

        @Override
        public void onDestroy() {
           super.onStop();
           mp= MediaPlayer.create(Elementary1Activity.this,R.raw.e1);
           if(mp.isPlaying())
           {
             mp.stop();
             mp.release();
             finish();
           }
          }


        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if(fromUser) mp.seekTo(progress);

        }


    }

2 个答案:

答案 0 :(得分:1)

我会在你的Elementary1Activity中使用onPause()事件来管理音乐暂停。

类似的东西:

@Override
public void onPause() {
    super.onPause();
    if (mp!=null) {
        mp.pause();
    }  
}

答案 1 :(得分:0)

你可以做两件事。如果您希望MediaPlayer在按下按钮时停止,并且您想关闭应用程序,则最佳选择是:

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

如果您想在更改活动时停止MediaPlayer:

@Override
public void onPause(){
    if (mp != null && mp.isPlaying()){
        mp.pause();
    }
}

除此之外,当您想要关闭应用时,请不要创建新的MediaPlayer。