在使用soundpool 做多音軌程式測驗時,發現一個奇怪的現象。就是load assets檔案加載2首音頻,播放后僅聽到同一首音頻2次
想咨詢一下論壇里有沒有朋友知道這個問題的原因或者有什么線索?
代碼下載自https://blog.csdn.net/aduovip/article/details/14695779,略有改動。
感謝分享,
謝謝各位。
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements SoundPool.OnLoadCompleteListener {
private static final int SRC_QUALITY = 0;
private static final int PRIORITY = 1;
private SoundPool soundPool = null;
private AudioManager aMgr;
private int sid_background;
private int sid_roar;
private int sid_bark;
private int sid_chimp;
private int sid_rooster;
private int BG_bac;
private int BG_Str_bac;
private float BG_bac_Vol;
private int BG_bac_Fre;
private int BG_bac_Rate;
private int BG_talk;
private int BG_Str_talk;
private int BG_eff;
private Boolean isLoaded;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onResume() {
//soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, SRC_QUALITY);
SoundPool.Builder spb = new SoundPool.Builder();
spb.setMaxStreams(4);
AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);//設定音頻流的合適的屬性
spb.setAudioAttributes(attrBuilder.build()); //轉換音頻格式
final SoundPool soundPool = spb.build(); //創建SoundPool物件
//soundPool.setOnLoadCompleteListener(this);
// 此方式讀取2首不同MP3檔案后,play會播放同一首音頻2次。
try {
AssetFileDescriptor afd = this.getAssets().openFd("res/sound/fight.mp3"); // 狗叫聲
BG_eff = soundPool.load(afd.getFileDescriptor(), 0, afd.getLength(), PRIORITY);
afd.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
AssetFileDescriptor afd2 = this.getAssets().openFd("res/sound/enter.mp3"); // 狗叫聲
BG_talk = soundPool.load(afd2.getFileDescriptor(), 0, afd2.getLength(), PRIORITY);
afd2.close();
} catch (IOException e) {
e.printStackTrace();
}
// 這種方式讀取后可正常同時播放不同音頻檔案
// BG_eff = soundPool.load(this, R.raw.enter, PRIORITY);
// BG_talk = soundPool.load(this, R.raw.fight, PRIORITY);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener(){
@Override
public void onLoadComplete(SoundPool sound,int sampleId,int status){
isLoaded=true;
if(status != 0)
return;
// 此處輸出播放后回傳的 STREAMID
if(sampleId == BG_eff) {
BG_Str_bac = soundPool.play(BG_eff, 1, 1, PRIORITY, 5, 1.0f);
Log.e("JAVA", "加載音頻檔案成功:str_ID:" + BG_Str_bac + ",回傳ID: "+BG_eff + ",回傳狀態:" + status);
} else if(sampleId == BG_talk) {
BG_Str_talk = soundPool.play(BG_talk, 1, 1, PRIORITY, 5, 1.0f);
Log.e("JAVA", "加載音頻檔案成功:str_ID:" + BG_Str_talk + ",回傳ID: "+BG_talk + ",回傳狀態:" + status);
}
}
});
super.onResume();
}
private byte[] InputStreamToByte(InputStream is) throws IOException {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
byte imgdata[] = bytestream.toByteArray();
bytestream.close();
return imgdata;
}
public void doClick(View view) {
switch(view.getId()) {
case R.id.button:
if(((ToggleButton)view).isChecked()) {
soundPool.autoResume();
} else {
soundPool.autoPause();
}
break;
}
}
@Override
protected void onPause() {
soundPool.release();
soundPool = null;
super.onPause();
}
private void releaseSoundPool() {
if (soundPool != null) {
soundPool.autoPause();
//soundPool.unload(mSoundId);
soundPool.release();
soundPool = null;
}
}
private void queueSound(final int sid, final long delay, final float volume) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(soundPool == null) return;
if(soundPool.play(sid, volume, volume,PRIORITY, 0, 1.0f) == 0) {
Log.e("soundPool", "Failed to start sound (" + sid + ")");
}
queueSound(sid, delay, volume);
}}, delay);
}
}
uj5u.com熱心網友回復:
找到原因了,由于在獲取檔案時使用的是 AssetFileDescriptor 型別,雖然load時 從 AssetFileDescriptor 中獲取了 FileDescriptor類,但實際效果是歌曲播放時錯亂。修改后將 load 使用的api 調整為:int load(AssetFileDescriptor afd, int priority) 。
這么修改后,多音軌的效果就正常了!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/123466.html
標籤:Eclipse
上一篇:求助:java.net.ConnectException: no avaiable server
下一篇:啟動專案報錯
