我正在研究一个简单的 AudioTrack 示例,它读取 PCM 文件然后播放它。它在 android 模拟器上运行良好,但在我的测试手机上使用 readShort() 函数读取数据时失败。这是代码失败的地方:
//! Read in the raw audio file
audioData= new short[0];
try {
InputStream is= this.getAssets().open("Vocals.pcm");
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream audioFileStream = new DataInputStream(bis);
audioData= new short[audioFileStream.available() / 2];
int i = 0;
while (audioFileStream.available() > 0)
{
audioData[i]= audioFileStream.readShort();
i++;
}
audioFileStream.close();
} catch (IOException e) {
Log.e("AudioTrackTest", "Loading PCM audio file failed");
e.printStackTrace();
}
函数 readShort() 第一次被调用时抛出 IOException。例外中没有提供其他细节。DataInputStream 似乎正在正确查看文件,因为它将变量 audiodata 更改为正确的长度数组。
有什么想法吗?
编辑:添加堆栈跟踪
03-14 13:45:40.248: ERROR/AudioTrackTest(1166): Loading PCM audio file failed
03-14 13:45:47.438: ERROR/AudioTrackTest(1166): android.content.res.AssetManager.readAsset(Native Method)
03-14 13:45:48.588: ERROR/AudioTrackTest(1166): android.content.res.AssetManager.access$700(AssetManager.java:36)
03-14 13:45:49.478: ERROR/AudioTrackTest(1166): android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
03-14 13:45:50.389: ERROR/AudioTrackTest(1166): java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:157)
03-14 13:45:51.309: ERROR/AudioTrackTest(1166): java.io.BufferedInputStream.read(BufferedInputStream.java:346)
03-14 13:45:52.169: ERROR/AudioTrackTest(1166): java.io.DataInputStream.readToBuff(DataInputStream.java:157)
03-14 13:45:53.008: ERROR/AudioTrackTest(1166): java.io.DataInputStream.readShort(DataInputStream.java:374)
03-14 13:45:53.908: ERROR/AudioTrackTest(1166): example.audiotrack.AudioTrackTest.loadPCMFile(AudioTrackTest.java:68)
03-14 13:45:54.549: ERROR/AudioTrackTest(1166): example.audiotrack.AudioTrackTest.onCreate(AudioTrackTest.java:40)
03-14 13:45:55.288: ERROR/AudioTrackTest(1166): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-14 13:45:55.959: ERROR/AudioTrackTest(1166): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-14 13:45:56.688: ERROR/AudioTrackTest(1166): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-14 13:45:57.239: ERROR/AudioTrackTest(1166): android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-14 13:45:57.808: ERROR/AudioTrackTest(1166): android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-14 13:45:58.309: ERROR/AudioTrackTest(1166): android.os.Handler.dispatchMessage(Handler.java:99)
03-14 13:45:59.058: ERROR/AudioTrackTest(1166): android.os.Looper.loop(Looper.java:123)
03-14 13:45:59.808: ERROR/AudioTrackTest(1166): android.app.ActivityThread.main(ActivityThread.java:4627)
03-14 13:46:00.409: ERROR/AudioTrackTest(1166): java.lang.reflect.Method.invokeNative(Native Method)
03-14 13:46:01.029: ERROR/AudioTrackTest(1166): java.lang.reflect.Method.invoke(Method.java:521)
03-14 13:46:01.448: ERROR/AudioTrackTest(1166): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-14 13:46:01.848: ERROR/AudioTrackTest(1166): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-14 13:46:02.429: ERROR/AudioTrackTest(1166): dalvik.system.NativeStart.main(Native Method)