2017-01-12 7 views
-2

私はアンドロイドのサウンドボードを作っています。いずれかのボタンをタッチすると音が鳴りますが、もう一度触れると音だけでなく他のボタンも動きません。一度に1つの音。私の主な活動は、ボタンの再生機能を呼び出すことです。ボタンをもう一度押すと音が止まる

public class MainActivity extends AppCompatActivity { 

    MediaPlayer whine, cry, weed, chup; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     whine = MediaPlayer.create(this, R.raw.gone); 
     cry = MediaPlayer.create(this, R.raw.mock); 
     weed = MediaPlayer.create(this, R.raw.phen); 
     chup = MediaPlayer.create(this, R.raw.rg); 
    } 
    public void playwhine(View view) { 
     if (cry.isPlaying()) 
      cry.stop(); 
     if (weed.isPlaying()) 
      weed.stop(); 
     if (chup.isPlaying()) 
      chup.stop(); 
     whine.start(); 
    } 

    public void playcry(View view) { 
     if (whine.isPlaying()) 
      whine.stop(); 
     if (weed.isPlaying()) 
      weed.stop(); 
     if (chup.isPlaying()) 
      chup.stop(); 
     cry.start(); 
    } 

    public void playweed(View view) { 
     if (cry.isPlaying()) 
      cry.stop(); 
     if (whine.isPlaying()) 
      whine.stop(); 
     if (chup.isPlaying()) 
      chup.stop(); 
     weed.start(); 
    } 

    public void playchup(View view) { 
     if (cry.isPlaying()) 
      cry.stop(); 
     if (whine.isPlaying()) 
      whine.stop(); 
     if (weed.isPlaying()) 
      weed.stop(); 
     chup.start(); 
    } 
} 

答えて

0

このサンプルでは、​​あなたの最後の方法から取られている:あなたは、最後の3を見れば、あなたは音を停止することを見ることができれば、文

public void playchup(View view) { 
    if (cry.isPlaying()) 
     cry.stop(); 
    if (whine.isPlaying()) 
     whine.stop(); 
    if (weed.isPlaying()) 
     weed.stop(); 
    chup.start(); 
} 

。サウンドが再生されている場合、ボタンを押すと停止します。

は、私はさらに説明しましょう:

  1. ボタンが押された
  2. コールは、あなたが他の音
  3. を停止して、サウンドを再生する4つの方法の1(プレー())
  4. に行われます関連する方法

可能性のある問題

理論的には、MediaPLayerを再生するときは、再生する必要があります。サウンドをループさせる方法と、毎回メディアプレーヤーを再作成する必要がない方法については、this questionを参照してください。

whine = MediaPlayer.create(this, R.raw.gone); 
cry = MediaPlayer.create(this, R.raw.mock); 
weed = MediaPlayer.create(this, R.raw.phen); 
chup = MediaPlayer.create(this, R.raw.rg); 

whine.setLooping(true); 
cry.setLooping(true); 
weed.setLooping(true); 
chup.setLooping(true); 

をそして、あなたは他のすべてのサウンドを停止したくない場合は、1つのボタンが押されたときは、他の音があるかどうかを確認if文を削除する必要があります。

あなたのコードを意味することは次のようになります。それらを再生し、停止する。 MediaPlayerの

代替あなたは、いくつかを扱っている場合は、一度に複数の重複サウンドを扱うように設計されているとして、あなたがSoundPoolを使用することができます聞こえます。

0

あなたはを解放MusicPlayerオブジェクトを作成しますがされていないので、それら

release()メソッドを呼び出すためにsetOnCompletionインターフェイスとオーバーライドを宣言します。

private class musicCompletionListener implements MediaPlayer.OnCompletionListener { 
     @Override 
     public void onCompletion(MediaPlayer mediaPlayer) { 
      mediaPlayer.release(); 
     } 
    } 

及びその後setOnCompletion各メディアプレーヤオブジェクトのリスナー。

public void playwhine(View view) { 
    if (cry.isPlaying()) 
     cry.stop(); 
    if (weed.isPlaying()) 
     weed.stop(); 
    if (chup.isPlaying()) 
     chup.stop(); 
whine.setOnCompletionListener(new musicCompletionListener()); 
    whine.start(); 
} 

public void playcry(View view) { 
    if (whine.isPlaying()) 
     whine.stop(); 
    if (weed.isPlaying()) 
     weed.stop(); 
    if (chup.isPlaying()) 
     chup.stop(); 
cry.setOnCompletionListener(new musicCompletionListener()); 
    cry.start(); 
} 

public void playweed(View view) { 
    if (cry.isPlaying()) 
     cry.stop(); 
    if (whine.isPlaying()) 
     whine.stop(); 
    if (chup.isPlaying()) 
     chup.stop(); 
weed.setOnCompletionListener(new musicCompletionListener()); 
    weed.start(); 
} 

public void playchup(View view) { 
    if (cry.isPlaying()) 
     cry.stop(); 
    if (whine.isPlaying()) 
     whine.stop(); 
    if (weed.isPlaying()) 
     weed.stop(); 
chup.setOnCompletionListener(new musicCompletionListener()); 
    chup.start(); 
} 
関連する問題