我正在使用 MediaPlayer 播放一些有时会重叠的声音文件。我注意到在 LogCat 窗口中我不断收到此消息:
android 组件 OMX.TI.ACC 的最大实例。解码已创建。
它似乎对我的应用程序没有影响,因为声音继续正常播放。有谁知道这条消息是什么意思,我需要担心吗?
最佳答案
SoundPool 可能是播放多个短声音的更好选择。
创建 SoundPool
public static final int SOUND_1 = 1;
public static final int SOUND_2 = 2;
SoundPool mSoundPool;
HashMap<Integer, Integer> mHashMap;
@Override
public void onCreate(Bundle savedInstanceState){
mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 100);
mSoundMap = new HashMap<Integer, Integer>();
if(mSoundPool != null){
mSoundMap.put(SOUND_1, mSoundPool.load(this, R.raw.sound1, 1));
mSoundMap.put(SOUND_2, mSoundPool.load(this, R.raw.sound2, 1));
}
}
然后通过调用自定义函数播放声音。
播放声音
/*
*Call this function from code with the sound you want e.g. playSound(SOUND_1);
*/
public void playSound(int sound) {
AudioManager mgr = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
if(mSoundPool != null){
mSoundPool.play(mSoundMap.get(sound), volume, volume, 1, 0, 1.0f);
}
}
关于android - 播放媒体文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3910703/