2016-03-21 194 views
0

我的應用程序包含一個媒體播放器,在audio.xml中有一個(播放/暫停)按鈕,但問題是我無法在頁面被破壞之前找到正確的代碼來停止MediaPlayer,當我登錄的音頻頁面(XML)應用程序崩潰和退出而不運行媒體播放器,這裏是我使用的代碼:停止媒體播放器的問題

public class audio extends Langue implements Runnable, 

     OnClickListener, OnSeekBarChangeListener { 
      private SeekBar seekBar; 
      private Button startMedia; 
      // private Button stopMedia; 
     private MediaPlayer mp; 


     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.audio); 
      seekBar = (SeekBar) findViewById(R.id.seekBar1); 
      startMedia = (Button) findViewById(R.id.button1); 
      //stopMedia = (Button) findViewById(R.id.button2); 
      startMedia.setOnClickListener(this); 
      //stopMedia.setOnClickListener(this); 
      seekBar.setOnSeekBarChangeListener(this); 
      seekBar.setEnabled(false); 


     } 

     public void run() { 
      int currentPosition = mp.getCurrentPosition(); 
      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) { 
      if (v.equals(startMedia)) { 
      if (mp == null) { 
      mp = MediaPlayer.create(getApplicationContext(), R.raw.espoir); 
      seekBar.setEnabled(true); 
      } 
      if (mp.isPlaying()) { 
      mp.pause(); 
      startMedia.setText("Play"); 
      } else { 
      mp.start(); 
      startMedia.setText("Pause"); 
      seekBar.setMax(mp.getDuration()); 
      new Thread(this).start(); 
      } 
      } 

     /* if (v.equals(stopMedia) && mp != null) { 
      if (mp.isPlaying() || mp.getDuration() > 0) { 
      mp.stop(); 
      mp = null; 
      startMedia.setText("Play"); 
      seekBar.setProgress(0); 
      } 
      } 
*/ 
     } 

     public void onProgressChanged(SeekBar seekBar, int progress, 
      boolean fromUser) { 
      try { 
      if (mp.isPlaying() || mp != null) { 
      if (fromUser) 
      mp.seekTo(progress); 
      } else if (mp == null) { 
      Toast.makeText(getApplicationContext(), "Media is not running", 
       Toast.LENGTH_SHORT).show(); 
      seekBar.setProgress(0); 
      } 
      } catch (Exception e) { 
      Log.e("seek bar", "" + e); 
      seekBar.setEnabled(false); 

      } 
     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 
      // TODO Auto-generated method stub 

                  } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 
      // TODO Auto-generated method stub 
                  } 

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

              } 
       return super.onOptionsItemSelected(item); 
                   } 
     // stop song 
      @Override 
      protected void onPause(){ 
      super.onPause(); 
      mp.stop(); 
      finish(); 
            } 


     } 

回答

0

做這樣的事情到的onPause()方法,以防止碰撞

try{ 
mp.stop(); 
} catch(Exception e) { 
} 

finish(); 
+0

它爲我工作,不再崩潰,非常感謝你。 – hakim