I am trying to playback a file test3.mid that is generated by the same app and is in the cache directory (I have verified that the file is there with filemanager)
I read here that setDataSource(string) can cause errors, so I opted to use filedesciptor. The code only gives errors (see below) when I add the prepare() method.
Am I making an error somewhere? I see ‘NullPointerException‘. Does that mean it does not find the file? What could I try?
I
try {
String filePath = getCacheDir() + "/test3.mid";
File file = new File(filePath);
FileInputStream inputStream = new FileInputStream(file);
mediaPlayer.setDataSource(inputStream.getFD());
inputStream.close();
} catch (Exception e1) {
e1.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Logcat output is:
07-25 10:36:44.264: E/AndroidRuntime(31467): FATAL EXCEPTION: main
07-25 10:36:44.264: E/AndroidRuntime(31467): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.optimuse/com.example.optimuse.MainActivity}: java.lang.NullPointerException
07-25 10:36:44.264: E/AndroidRuntime(31467): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-25 10:36:44.264: E/AndroidRuntime(31467): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-25 10:36:44.264: E/AndroidRuntime(31467): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-25 10:36:44.264: E/AndroidRuntime(31467): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-25 10:36:44.264: E/AndroidRuntime(31467): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 10:36:44.264: E/AndroidRuntime(31467): at android.os.Looper.loop(Looper.java:137)
07-25 10:36:44.264: E/AndroidRuntime(31467): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-25 10:36:44.264: E/AndroidRuntime(31467): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 10:36:44.264: E/AndroidRuntime(31467): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 10:36:44.264: E/AndroidRuntime(31467): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-25 10:36:44.264: E/AndroidRuntime(31467): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-25 10:36:44.264: E/AndroidRuntime(31467): at dalvik.system.NativeStart.main(Native Method)
07-25 10:36:44.264: E/AndroidRuntime(31467): Caused by: java.lang.NullPointerException
07-25 10:36:44.264: E/AndroidRuntime(31467): at com.example.optimuse.MainActivity.onCreate(MainActivity.java:127)
07-25 10:36:44.264: E/AndroidRuntime(31467): at android.app.Activity.performCreate(Activity.java:4465)
07-25 10:36:44.264: E/AndroidRuntime(31467): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-25 10:36:44.264: E/AndroidRuntime(31467): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
This is completely stupid, but I forgot