2014-02-22 99 views
1

我有一個按鈕,播放隨機聲音片段後跟隨另一個然後另一個媒體播放器,但按多次按鈕後(15-20次)所有音頻只是停下來。我在播放最後一個音頻剪輯後釋放媒體播放器,所以我沒有看到這是原因。任何指針?Media Player在播放幾次後停止播放

public class Main extends Activity { 


MediaPlayer mp; 
Button generate; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    generate = (Button) findViewById(R.id.sound); 

} 

public void youSir (View view) 
{ 
    generate.setVisibility(View.INVISIBLE); 
    Random randomGenerator = new Random(); 
    int randomInt = randomGenerator.nextInt(3) + 1; 
    switch (randomInt){ 
    case 1: mp = MediaPlayer.create(this, R.raw.beg); mp.start(); 
    break; 
    case 2: mp = MediaPlayer.create(this, R.raw.begone); mp.start(); 
    break; 
    case 3: mp = MediaPlayer.create(this, R.raw.begtwo); mp.start(); 
    break; 
    } 
    mp.setOnCompletionListener(new OnCompletionListener(){ 
     // @Override 
     public void onCompletion(MediaPlayer arg0) { 

      audioTwo(); 
     } 
    }); 

} 


protected void audioTwo() { 
    // TODO Auto-generated method stub 
    Random randomGenerator = new Random(); 
    int randomInt = randomGenerator.nextInt(3) + 1; 
    switch (randomInt){ 
    case 1: mp = MediaPlayer.create(this, R.raw.start); mp.start(); 
    break; 
    case 2: mp = MediaPlayer.create(this, R.raw.end); mp.start(); 
    break; 
    case 3: mp = MediaPlayer.create(this, R.raw.mid); mp.start(); 
    break; 
    } 
    mp.setOnCompletionListener(new OnCompletionListener(){ 
     // @Override 
     public void onCompletion(MediaPlayer arg0) { 
      audioThree(); 
     } 
    }); 
} 

protected void audioThree() { 
    // TODO Auto-generated method stub 
    Random randomGenerator = new Random(); 
    int randomInt = randomGenerator.nextInt(3) + 1; 
    switch (randomInt){ 
    case 1: mp = MediaPlayer.create(this, R.raw.three); mp.start(); 
    break; 
    case 2: mp = MediaPlayer.create(this, R.raw.two); mp.start(); 
    break; 
    case 3: mp = MediaPlayer.create(this, R.raw.one); mp.start(); 
    break; 
    mp.setOnCompletionListener(new OnCompletionListener(){ 
     // @Override 
     public void onCompletion(MediaPlayer arg0) { 
      mp.stop(); 
      mp.release(); 
      generate.setVisibility(View.VISIBLE); 
     } 
    }); 
} 

回答

2

這是一個有點舊的問題,但我試圖回答它,以防其他人有類似的問題。

您正在用盡音頻資源。該代碼實際上不會釋放所有MediaPlayer實例。它每次運行只發布三次。 audioTwo()audioThree將新的MediaPlayer實例分配給mp變量,但它們不釋放已存儲在那裏的實例。做類似:

protected void audioTwo() { 
    // RELEASING PREVIOUS MEDIAPLAYER 
    mp.release(); 

    Random randomGenerator = new Random(); 
    int randomInt = randomGenerator.nextInt(3) + 1; 
    switch (randomInt){ 
    case 1: mp = MediaPlayer.create(this, R.raw.start); mp.start(); 
    break; 
    ... 

audioThree()也是這樣。