2013-08-30 82 views
0

我有了這個活動,並儘快爲我的聲音的播放過程中擊中home鍵 聲音繼續打球,當聲音完成了它帶回的應用程序。的MediaPlayer繼續播放,雖然我關閉應用程序

public class View5 extends MasterView implements OnCompletionListener { 

    int[] tracks = new int[4]; 
    int[] times = new int[4]; 
    int currentTrack = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     InputStream stream = null; 
     try { 
      stream = getAssets().open("herzdruck_animiert2.gif"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     SharedPreferences settings = getSharedPreferences("settings", 0); 
     String lang = settings.getString("lang", ""); 


     if (lang.equals("de")) { 
      tracks[0] = getResources().getIdentifier("raw/view5_1_de", 
        null, this.getPackageName()); 

      tracks[1] = getResources().getIdentifier("raw/view5_2_de", null, 
        this.getPackageName()); 

      tracks[2] = getResources().getIdentifier("raw/view5_3_de", null, 
        this.getPackageName()); 

      tracks[3] = getResources().getIdentifier("raw/view5_4_de", null, 
        this.getPackageName()); 

     } else if (lang.equals("tr")) { 
      tracks[0] = getResources().getIdentifier("raw/view5_1_tr", 
        null, this.getPackageName()); 

      tracks[1] = getResources().getIdentifier("raw/view5_2_tr", null, 
        this.getPackageName()); 

      tracks[2] = getResources().getIdentifier("raw/view5_3_tr", null, 
        this.getPackageName()); 

      tracks[3] = getResources().getIdentifier("raw/view5_4_tr", null, 
        this.getPackageName()); 

     } else if (lang.equals("en")) { 
      tracks[0] = getResources().getIdentifier("raw/view5_1_en", 
        null, this.getPackageName()); 

      tracks[1] = getResources().getIdentifier("raw/view5_2_en", null, 
        this.getPackageName()); 

      tracks[2] = getResources().getIdentifier("raw/view5_3_en", null, 
        this.getPackageName()); 

      tracks[3] = getResources().getIdentifier("raw/view5_4_en", null, 
        this.getPackageName()); 

     } 

     times[0] = 5000; 
     times[1] = 2000; 
     times[2] = 2000; 
     times[3] = 1000; 

     GifMovieView view = new GifMovieView(this, stream); 

     mPlayer = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]); 
     mPlayer.setOnCompletionListener(this); 
     mPlayer.start(); 



     setContentView(view); 
    } 

    @Override 
    public void onCompletion(MediaPlayer arg0) { 
     arg0.release(); 
     arg0 = MediaPlayer.create(getApplicationContext(), 
       tracks[currentTrack]); 

     if (currentTrack < tracks.length-1) { 
      currentTrack++; 
      arg0 = MediaPlayer.create(getApplicationContext(), 
        tracks[currentTrack]); 
      arg0.setOnCompletionListener(this); 
      try { 
       Thread.sleep(times[currentTrack]); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      arg0.start(); 
     }else if(currentTrack==tracks.length-1) { 
      Intent i = new Intent(getApplicationContext(), ViewBeat1.class); 
      startActivity(i); 
     } 
    } 

} 

我該如何擺脫這種行爲?

的馬西德威持有MPlayer和也關閉它onRestart,和的onDestroy在onPause。

+0

我以前也有同樣的問題。您需要明確停止onPause中的音樂。你最後的評論聽起來像你。你能告訴我們那個代碼嗎? –

+0

我在馬西德威這樣做(其從活動延伸) 保護無效的onPause(){ \t \t mPlayer.release(); \t \t super.onPause(); \t \t \t} – Kazoooka

+3

無關但是......我可以建議*不要*命名類'View5'並花費三秒鐘將參數名稱從'arg0'改爲更具描述性的描述。 – ChiefTwoPencils

回答

1

enter image description here
這裏是MediaPlayer的狀態圖,這將幫助你在你應該叫什麼點在Activity的生命週期你MediaPlayer對象上的各種方法瞭解。

就像Jesse J說,嘗試調用stop()onPause()

相關問題