如何在操作栏

时间:2017-12-18 15:11:47

标签: android android-actionbar

我正在尝试创建一个音板,因为它是第一次在动作栏上工作,所以我卡住了 所以我创建了菜单,按钮显示在操作栏上,并显示了我想要的活动但是我不知道如何在按下时停止声音池中的所有声音。
当我尝试创建SoundPool.stop()时,我得到了#34; (非静态方法' 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 个答案:

答案 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);
     }