如何在特定时间段内使用SoundPool同时播放声音 [英] How to play a sound simultaneously using SoundPool for a specific period of time
问题描述
我正在使用一个Android媒体应用程序,因为我想在特定时间段内同时播放声音文件.因此,我创建了一个类,并且在该类中使用了SoundPool
和Thread
概念来播放声音,但是当我运行我的应用程序时,它将进入ANR状态并在log-cat
I am working with an android media application for that i want play a sound file simultaneously for a specific period of time. So i've created a Class and i am using SoundPool
and Thread
concepts inside this class to play the sound but when i run my application it is going to an ANR state and showing a warning in log-cat
样品2130968576尚未就绪
sample 2130968576 not READY
任何人请帮助我解决此问题...
anyone please help me to fix this...
课程
package com.kodspider.funk;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class CustomPlayer implements Runnable{
int song_id;
long time;
int button_id;
Context ctx;
SoundPool soundpool;
public CustomPlayer(int s_id, long Time, Context ct){
song_id = load(ctx, s_id, 0);
time = Time;
ctx = ct;
}
public void run(){
long start = System.currentTimeMillis();
System.out.println("set_time:"+time+"Song_id:"+song_id+"current_time:"+start);
long end = start + time;
while (System.currentTimeMillis() < end){
//Initialization
soundpool = new SoundPool(8, AudioManager.STREAM_MUSIC, 0);
soundpool.play(song_id, 1.0f, 1.0f, 1, -1, 1.0f);
}
}
public int load (Context context, int resId, int priority){
return resId;
}
}
logcat
07-07 10:57:51.828: I/ApplicationPackageManager(13625): cscCountry is not German : INS
07-07 10:57:57.656: I/ApplicationPackageManager(13625): cscCountry is not German : INS
07-07 10:57:59.343: W/KeyCharacterMap(13625): Can't open keycharmap file
07-07 10:57:59.343: W/KeyCharacterMap(13625): Error loading keycharmap file
07-07 10:57:59.343: W/KeyCharacterMap(13625): Using default keymap
07-07 10:58:44.820: I/ApplicationPackageManager(13625): cscCountry is not German : INS
07-07 10:58:49.718: I/System.out(13625): TAG------->60000---->2130968576
07-07 10:58:49.726: W/SoundPool(13625): sample 2130968576 not READY
推荐答案
I'm doing something similar with my app Beat Shaker and I was having the same problem. I solved it using the onLoadCompleteListener:
// Sound pool new instance
SoundPool spool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
// Sound pool on load complete listener
spool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
Log.i("OnLoadCompleteListener","Sound "+sampleId+" loaded.");
boolean loaded = true;
}
});
// Load the sample IDs
int soundId = new int[3];
soundId[0] = spool.load(this, R.raw.sound1, 1);
soundId[1] = spool.load(this, R.raw.sound2, 1);
soundId[2] = spool.load(this, R.raw.sound3, 1);
稍后,在播放声音之前,您可以使用布尔值"loaded"检查声音是否正确加载.
Later before playing the sound you can check if it is properly loaded using the boolean "loaded".
这篇关于如何在特定时间段内使用SoundPool同时播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!