2014-01-20 116 views
0

我试图做10个按钮,这将播放一些音乐点击和当用户点击后退按钮,他应该退出应用程序和音乐应停止活动。所有我tryied没有做任何事情。onDestroy停止mediaplayer

如果有人可以建议我的东西,这是代码:

package app.technozed.yearstest; 

import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ToggleButton; 

public class MainActivity extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ToggleButton unu = (ToggleButton)this.findViewById(R.id.button1); 
    final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.tt); 
    ToggleButton doi = (ToggleButton)this.findViewById(R.id.button2); 
    final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.rr); 
    ToggleButton trei = (ToggleButton)this.findViewById(R.id.button3); 
    final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.dd); 
    ToggleButton patru = (ToggleButton)this.findViewById(R.id.button4); 
    final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.vv); 
    ToggleButton cinci = (ToggleButton)this.findViewById(R.id.button5); 
    final MediaPlayer mp5 = MediaPlayer.create(this, R.raw.ss); 
    unu.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v) { 
      // If the music is playing 
      if(mp1.isPlaying()) { 
       // Pause the music player 

       mp1.setLooping(false); 
       mp1.pause(); } 
      // If it's not playing 
      else 
      { 
       // Resume the music player 
       mp1.setLooping(true); 
       mp1.start(); } 
     } 
    }); 
     doi.setOnClickListener(new OnClickListener(){ 

      public void onClick(View v) { 

       // If the music is playing 
       if(mp2.isPlaying() == true) { 
        mp2.setLooping(false); 
        // Pause the music player 
        mp2.pause(); } 
       // If it's not playing 
        else { 
        // Resume the music player 
        mp2.setLooping(true); 
        mp2.start(); } 
      } 
     }); 
     trei.setOnClickListener(new OnClickListener(){ 

      public void onClick(View v) { 

       // If the music is playing 
       if(mp3.isPlaying()) { 
        mp3.setLooping(false); 
        // Pause the music player 
        mp3.pause(); } 
       // If it's not playing 
       else { 
        // Resume the music player 
        mp3.setLooping(true); 
        mp3.start(); } 
      } 
     }); 
     patru.setOnClickListener(new OnClickListener(){ 

      public void onClick(View v) { 

       // If the music is playing 
       if(mp4.isPlaying()) { 
        mp4.setLooping(false); 
        // Pause the music player 
        mp4.pause(); } 
       // If it's not playing 
       else { 
        // Resume the music player 
        mp4.setLooping(true); 
        mp4.start(); } 
      } 
     }); 
     cinci.setOnClickListener(new OnClickListener(){ 

      public void onClick(View v) { 

       // If the music is playing 
       if(mp5.isPlaying()) { 
        mp5.setLooping(false); 
        // Pause the music player 
        mp5.pause(); } 
       // If it's not playing 
       else { 
        // Resume the music player 
        mp5.setLooping(true); 
        mp5.start(); } 
      } 
     }); 
} 
@Override 
protected void onDestroy() 
{ 
    super.onDestroy(); 
    MediaPlayer mp = new MediaPlayer(); 
    if (mp.isPlaying()) { mp.stop(); mp.release(); 
    //finally 
} 
} 

我的理解是,在部分的onDestroy我做错了,但我怎么能释放MP1,MP2,MP3,。 ...?

回答

0

您需要将MediaPlayers声明为“全局”变量,以更具体地作为类字段。像这样:

public class MainActivity extends Activity { 

    public MediaPlayer mp1, mp2, mp3 ..... 

并将它们在onCreate中实例化并将它们置于onDestroy中。

通过以这种方式声明它们,您可以使用类方法访问它们。

你的方式,你声明他们作为函数的局部变量,因此它们以其他方法

+0

好的,我做了你说的话,声音停在了Destroy上,但它并没有停止在最小化。我怎样才能阻止它? –

+2

只需在您的onPause方法中调用相同的代码即可。但也从你的onDestroy中删除它。 – JanBo

+0

只是做了这个。我用onPause改变了onDestroy,它在最小化时起作用。 ' \t @覆盖 \t保护无效的onPause() \t { \t super.onDestroy(); \t mp1.stop(); \t mp2.stop(); \t mp3.stop(); \t mp4.stop(); \t mp5.stop(); \t} ' 但是,当最大化声音不想玩,我怎么才能添加onResume选项来恢复只有在哪里播放的声音? –

1

不存在,可以储存在类中MediaPlayer对象,你可以再后来停止参考。你目前正在做的是创建一个MediaPlayer的新实例,它不会播放任何东西并试图阻止它。

public class MainActivity extends Activity { 
    private MediaPlayer mediaPlayer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    ... 
    mediaPlayer = MediaPlayer.create(this, R.raw.whatever); 
    ... 
    } 

    @Override 
    protected void onDestroy() { 
    ... 
    if (mediaPlayer.isPlaying()) { 
     mediaPlayer.stop(); 
    } 
    ... 
    } 
} 
0

您正在创建一个新的MediaPlayer。你应该停止mp1,mp2,mp3,mp4和mp5。

个人而言,我会做这样的事每个的onClick

 public void onClick(View v) { 
      MediaPlayer mp = MediaPlayer.create(this, //Specified type); 
      // If the music is playing 
      if(mp.isPlaying() == true) { 
       mp.setLooping(false); 
       // Pause the music player 
       mp.pause(); } 
      // If it's not playing 
       else { 
       // Resume the music player 
       mp.setLooping(true); 
       mp.start(); } 
     } 

这将是一个很多更容易,因为你可以用一个简单的线条停止播放。不幸的是,你将无法同时播放两个不同的流。