2017-12-18 38 views
0

サウンドボードを作成しようとしていますが、初めてアクションバーを操作してから立ち往生しています。
私はメニューを作成しましたが、ボタンはアクションバーに表示され、私が望むアクティビティに表示されますが、サウンドプールからすべてのサウンドを停止するために押されたときにどのようにするかわかりません。
私はSoundPool.stop()を作成しようとすると、「(非静的メソッド 'stop(int)'は静的コンテキストから参照できません」(おそらく間違っていますが、それを行う方法はわかりません) 。それ誰かが私はそれについて学ぶためにしようとしてきたと私はそれに近い何かを見つけるように見えるカント以来、助けることができればアクションバーのサウンドプールからサウンドを停止するにはどうすればいいですか

は、ここでは、コードです:

:ここ

import android.media.AudioManager; 
    import android.media.SoundPool; 
    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.view.Menu; 
    import android.view.View; 



    public class soundMainjava extends AppCompatActivity { 

    SoundPool mySound; 
    int sound1Id; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.soundMain); 
     setVolumeControlStream(AudioManager.STREAM_MUSIC); 
     mySound = new SoundPool(99, AudioManager.STREAM_MUSIC, 0); 
     sound1Id = mySound.load(this, R.raw.sound1, 1); 
    } 

    public void pb1(View view) { 
     mySound.play(sound1Id, 1, 1, 1, 0, 1); 
    } 

    //inflates the menu; 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu, menu); 
     return true; 
    } 
} 

は、メニューのコードです

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
tools:context="com.example.---.testsounds.soundMainjava"> 
<item 
    android:id="@+id/action_sound" 
    android:icon="@drawable/ic_volume_off_black_24dp" 
    android:title="" 
    app:showAsAction="always"/> 
</menu> 

ここにコードがあります動作しないで:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.action_sound: 
      SoundPool.stop(sound1Id); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
+1

質問にエラーの説明を追加してください。 – svgrafov

+0

大丈夫です –

答えて

0

だから、問題は次のとおりです。

switch (item.getItemId()) { 
     case R.id.action_sound: 
// You are calling the static method. 
      SoundPool.stop(sound1Id); 
// You have to call the object created from SoundPool Class 
      return true;` 

だから、これはここで

switch (item.getItemId()) { 
     case R.id.action_sound: 
      mySound.stop(sound1Id); 
      return true; 

完全に動作するサンプルである必要があります:

 public class SoundPlayer { 

    private static final float leftVol = 1.0f; 
    private static final float rightVol = 1.0f; 
    private static final float rate = 1.0f; 
    private static final int loop = 0; 
    private static final int priority = 1; 
    private static SoundPool soundPool; 

    private static int startListeningSound; 
    private static int stopListeningSound; 

     public SoundPlayer(Context context) { 
     soundPool = new SoundPool.Builder().setMaxStreams(2).build(); 

     startListeningSound = soundPool.load(context, R.raw.google_now_tone, priority); 
     stopListeningSound = soundPool.load(context, R.raw.google_glass_done, priority); 

} 
     public void playStartVoiceSound() { 
     soundPool.play(startListeningSound, leftVol, rightVol, priority, loop, rate); 
    } 

     public void playStopVoiceSound() { 
     soundPool.play(stopListeningSound, leftVol, rightVol, priority, loop, rate); 
    } 

あなたの活動を呼び出すとき:

private SoundPlayer soundPlayer; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    ... 
     soundPlayer = new SoundPlayer(this); 
    ... 
    // calling soundPlayer to play the sound : 
    int streamId = soundPlayer.playStartVoiceSound(); 


    // calling the soundPlayer stop 

    soundPlayer.stop(streamId); 
    } 
関連する問題