0

我正在尝试播放由同一应用程序生成并位于其中的文件 test3.mid cache directory(我已使用文件管理器验证该文件存在)

我在这里读到可能setDataSource(string)会导致错误,所以我选择使用filedesciptor. 当我添加prepare()方法时,代码只给出错误(见下文)。

我在某处犯了错误吗?我看到' NullPointerException'。这是否意味着它找不到文件?我可以尝试什么?

一世

   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 输出为:

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)
4

1 回答 1

0

这完全是愚蠢的,但我忘记了

mediaPlayer = new MediaPlayer();
于 2012-07-25T12:00:05.327 回答