soundpool关于停止声音?

问题描述

声音池。您好,我正在为Android准备音频播放项目,但有一些缺点。我为我的短声音文件分配了-1值,并不断重复它们。通过按下其他按钮,我通过运行新声音来停止其他声音。我通过关闭程序来停止声音。我想要的是当我按下imageButton时,我希望按钮切换到第二个png文件并播放声音,而当我再次按下该按钮时,我希望它静音或停止声音并返回到我的第一个png文件图片。如果您能帮助我,我会很高兴。

MainActivity.Java

import android.media.soundPool;

private SoundPool soundPool = new SoundPool (3,AudioManager.STREAM_MUSIC,0);
private int sound1,sound2,sound3;

        soundPool = new SoundPool.Builder()
                .setMaxStreams(1)
                .setAudioAttributes(audioAttributes)
                .build();
    }

    else{
        soundPool = new SoundPool (3,0);
    }
    sound1 = soundPool.load(this,R.raw.sound1,1);
    sound2 = soundPool.load(this,R.raw.sound2,1);
    sound3 = soundPool.load(this,R.raw.sound3,1);

public void playSound(View v) {
    switch (v.getId()){
        case R.id.ImageButton1:
            soundPool.play(sound1,1,-1,1);
            break;
        case R.id.ImageButton2:
            soundPool.play(sound2,1);
            break;
        case R.id.ImageButton3:
            soundPool.play(sound3,1);
            break;
     }
}

 @Override
 protected void onPause() {
     super.onPause();
}

@Override
protected void onResume() {
    super.onResume();
}

protected void onDestroy() {
     super.onDestroy();
}

activity_main.xml

<ImageButton
            android:id="@+id/ImageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/image_button_1"
            android:onClick="playSound"

            />

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)