gpt4 book ai didi

android - SoundPool 不播放?

转载 作者:行者123 更新时间:2023-11-29 16:04:03 25 4
gpt4 key购买 nike

我有 2 个 SoundPool 实例,如果我运行我的应用程序,soundPool2 运行与 soundPool1 相同的声音,我不知道为什么这播放相同的声音。谁能帮我找出问题所在?

public class MainActivity extends Activity implements OnTouchListener {
private SoundPool soundPool;
private SoundPool soundPool2;
private int soundID;
private int soundID2;
boolean loaded = true;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.button1);
view.setOnTouchListener(this);

// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});

soundID = soundPool.load(this, R.raw.sound1, 1);

View view2 = findViewById(R.id.button2);
view2.setOnTouchListener(this);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool2 = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool2.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
@Override
public void onLoadComplete(SoundPool soundPool2, int sampleId,
int status) {
loaded = true;


}
});


soundID2 = soundPool.load(this, R.raw.bird, 1);

}

public boolean onTouch( View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played ");
}
}
return false;
}

public boolean onTouch2( View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool2.play(soundID2, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
return false;
}}

最佳答案

首先,您只需要一个 SoundPool 实例,而不是两个。

此外,您的onTouchonTouch2 对我来说意义不大,那里也可能存在问题,所以我也重写了那部分。

试试下面的代码。

public class MainActivity extends Activity implements OnTouchListener {
private SoundPool mSoundPool;
private int mSoundID;
private int mSoundID2;
private boolean mSoundLoaded = false;
private float mVolume = 0f;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

View view = findViewById(R.id.button1);
view.setOnTouchListener(this);
View view2 = findViewById(R.id.button2);
view2.setOnTouchListener(this);

// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

// Load the sound
mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
if (sampleId == R.raw.bird) {
mSoundLoaded = true;
}
}
});

mSoundID = mSoundPool.load(this, R.raw.sound1, 1);
mSoundID2 = mSoundPool.load(this, R.raw.bird, 1);

// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mVolume = actualVolume / maxVolume;
}

public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Is the sound loaded already?
if (mSoundLoaded) {
if (v.getId() == R.id.button1) {
playSound(mSoundID);
Log.e("Test", "Played mSoundID");
} else if (v.getId() == R.id.button2) {
playSound(mSoundID2);
Log.e("Test", "Played mSoundID2");
}
}
}
return false;
}

private void playSound(int soundId) {
mSoundPool.play(soundId, mVolume, mVolume, 1, 0, 1f);
}
}

注意:可能存在错误,这是未经测试的代码。

关于android - SoundPool 不播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com