I am working on a simple AudioTrack example which reads in a PCM file then plays it back. It works great on the android emulator, but on my test phone it fails when reading in the data with the readShort() function. Here is where the code is failing:
//! 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();
}
The function readShort() is throwing an IOException the first time it is called. No other details are provided in the exception. The DataInputStream appears to be looking at the file correctly since it changes the variable audiodata to the correct length array.
Any thoughts?
Edit: Adding stacktrace
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开发者_如何学Python-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)
Just figured this out! The file size was too large to be read in. Using a smaller file worked. Couldn't find any documentation on this but finally found some posts where this happened to other people. Thanks for the help!
Rename this file extension to .jpg and it should work. It will prevent Android packager from trying to compress this file. Hope it works
byte arrays instead of shorts should work. More like this:
BufferedInputStream bis = new BufferedInputStream(in);
DataInputStream dis = new DataInputStream(bis);
int len = dis.readInt();
byte[] data = new byte[len];
and try changing your check type audioFileStream.available() > 0 with this:
while ((len = dis.read(data)) != -1) { .... }
精彩评论