0

I've made a MediaPlayer play some audio from soundcloud and the play and pause are working but the issue is that when I press back button on phone it continues to play? Any advice would be great. onBackPressed code is at bottom :)

 public class player1 extends Activity implements Runnable {


private  MediaPlayer mp;
private ProgressBar progressBar;
private ImageButton pauseicon;



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



        /**
         * Play button click event
         * plays a song and changes button to pause image
         * pauses a song and changes button to play image
         * */

        String res = "https://api.soundcloud.com/tracks/84973999/stream?client_id=cd9d2e5604410d714e32642a4ec0eed4";

        final MediaPlayer mp = new MediaPlayer();
        try {
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mp.setDataSource(res);
                mp.prepare();
                mp.start();
                progressBar.setVisibility(ProgressBar.VISIBLE);
                progressBar.setProgress(0);
                progressBar.setMax(mp.getDuration());
            } catch (IOException e) {

            }

        mp.setOnCompletionListener(new OnCompletionListener() {
            //When audio is done will change pause to play
            public void onCompletion(MediaPlayer mp) {
                pauseicon.setImageResource(R.drawable.playicon);
            }
        });


        pauseicon.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                 // No need to check if it is pauseicon

                if(mp.isPlaying()){
                    mp.pause();
                 ((ImageButton) v).setImageResource(R.drawable.playicon);

                } else {
                    mp.start();
                    ((ImageButton) v).setImageResource(R.drawable.pauseicon);
             }}});



   }



        //To update progress bar
   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;
            }            
            progressBar.setProgress(currentPosition);
        }
    }
   @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(); 

    }}
4

2 回答 2

3

我认为问题出在这里:

final MediaPlayer mp = new MediaPlayer();

您正在创建一个本地实例,mp因此您的全局实例始终为空。

只需替换为:

mp = new MediaPlayer();

希望它有效

于 2013-06-18T16:29:04.027 回答
0

不知道为什么它不起作用,在我的应用程序中,我通常以这种方式覆盖它:

public boolean onKeyDown(int keyCode, KeyEvent event) {      
    if (keyCode == KeyEvent.KEYCODE_BACK) {
    ...

文档说它应该是一样的,但我试试看:)

编辑:final MediaPlayer从 onCreate 中删除(我注意到为时已晚,抱歉)哈哈

于 2013-06-18T16:28:43.547 回答