2016-09-23 113 views
0

我想通过单击按钮打开和关闭声音。打开/关闭声音

public void onClick(View v) { 
    buttonSound.setBackgroundResource(R.drawable.soundan); 
    CommonMethod.soundplayer.stop(); 

    //buttonSound.setBackgroundResource(R.drawable.soundaus); 
    //CommonMethod.soundplayer.setVolume(0,0); 
} 

这是的commonMethod

public class CommonMethod { 

    public static MediaPlayer soundplayer; 
    public static void Soundplayer(Context ctx,int raw_id) 
    { 
     soundplayer = MediaPlayer.create(ctx,raw_id); 
     soundplayer.setLooping(true); 
     soundplayer.setVolume(100, 100); 
    } 
} 

它通过单击停止声音,但它不打开它再次

我得到这个代码太多,但其同样的问题

public void onClick(View v) { 
       // TODO Auto-generated method stub 
       counter++; 
       if (counter % 2 == 0) { 
        buttonSound.setBackgroundResource(R.drawable.soundan); 
        CommonMethod.soundplayer.setVolume(100,100); 

       } 
       else 
       { 
        buttonSound.setBackgroundResource(R.drawable.soundaus); 
        CommonMethod.soundplayer.setVolume(0,0); 

       } 

      } 
+0

什么问题?另外,这不是Eclipse的问题。 – zgc7009

+0

如果我关掉它不能再次打开的声音 –

+0

soundaus或soundan检查 – phpdroid

回答

0

请看这片documentation

还有像一些有用的选票:

一个MediaPlayer对象必须先进入准备状态之前 回放可以开始。

在停止状态

一次,播放不能启动,直到准备() 或prepareAsync()被称为重新设置MediaPlayer对象到 准备状态。

0

可以pause它,而不是stooping如果停止,那么你再有prepaid它,然后它会play而不是你可以pause并再次从同一位置play``。

if(mPlayer.isPlaying()){ 
    mPlayer.pause(); 
} else { 
    mPlayer.start(); 
} 

了解下图。

enter image description here