0

MediaPlayer にサウンドクラウドからオーディオを再生させましたが、再生と一時停止は機能していますが、電話の戻るボタンを押しても再生が続くという問題がありますか? どんなアドバイスも素晴らしいでしょう。onBackPressed コードは一番下にあります:)

 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