[英]my soundpool project won't play properly?
我一直在嘗試實現此soundpool類(在教程中得到了一些幫助),但是當我嘗試在虛擬設備上運行它時,聲音將無法播放... main_activity XML文件可以很好地加載,但是可以預期加載時會播放聲音。
這是源代碼。
public class MainActivity extends Activity {
private SoundPool soundpool;
private boolean soundPoolLoaded = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a new SoundPool instance.
// 2 = number of sounds that can be simultaneously played
// AudioManager.STREAM_MUSIC = The type of sound stream. STREAM_MUSIC is the most common one
// 0 = sound quality, default is 0.
// setOnLoadCompleteListener loads the file
soundpool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
soundpool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPoolLoaded = true;
}
});
playSound("car_phonic");
}
// This method will load a sound resource from the res/raw folder by the input name given as a string (soundName).
// If the sound resource is found by name, it will attempt to play the sound
// soundPoolLoaded value becomes true when the file is fully loaded
public void playSound(String soundName) {
if(soundPoolLoaded == true)
{
// Get the current context resources and find the correct sound resource for playing the sound
Resources res = this.getResources();
int soundId = 0;
soundId = res.getIdentifier(soundName, "raw", this.getPackageName());
if(soundId != 0){
soundpool.play(soundId, 1, 1, 0, 0, 1);
}
SystemClock.sleep(500);
playSound(soundName);
}
}
}
任何幫助,將不勝感激!
因此,以其基本形式:
soundpool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundpool.play(sampleId, 1, 1, 0, 0, 1);
}
});
soundpool.load(this, R.raw.car_phonic, 0);
請注意,SoundPool在后台加載數據,因此無法立即獲得樣本。
這是您應該如何使用聲音池的方法
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
float curVolume = audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float leftVolume = curVolume / maxVolume;
float rightVolume = curVolume / maxVolume;
int priority = 10;
// int priority_1=5;
int no_loop = 0;
float normal_playback_rate = 1f;
final int ID=1;
soundPool.play(soundPoolMap.get(ID,
leftVolume, rightVolume, priority, no_loop,
normal_playback_rate);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.