未处理的IO异常?使用媒体播放器播放mp3文件 - Android

时间:2013-08-13 10:15:41

标签: android audio mp3

这是我的代码: (举个例子,假设我把它放在OnCreate中,以便在活动开始时播放声音。)

AssetFileDescriptor afda = getAssets().openFd("AudioFile.mp3");
MediaPlayer player;
player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor());
player.prepare();
player.start();

除此之外,这是我在SO上找到的解决方案:

try {// above erroneous code here
     }
  mediaPlayer.start();
} catch (IllegalArgumentException e) {
 e.printStackTrace();
} catch (SecurityException e) {
 e.printStackTrace();
} catch (IllegalStateException e) {
 e.printStackTrace();
} catch (IOException e) {
 e.printStackTrace();
}

我尝试在try catch语句中放入导致Unhandled IO异常错误的每个语句。但是一旦这样做,应用程序进入ANR,无法启动,并且logcat提供类似“无法初始化MainActivity”的内容....

经过进一步研究,我发现这段代码适用于其他所有人,也许我没有添加任何东西..

这段代码有什么问题?如何纠正?

(或者,是否有更好的或更简单的方式来播放文件?我只需要播放/暂停功能。) (我有一个[可滚动的标签+滑动]导航应用程序,我想在每个标签的屏幕上播放一个不同的mp3文件...只需将它放在oncreate中进行测试..)

这是修改后的代码和它产生的运行时错误的logcat。基本上,我在try catch框中放入的所有语句都给出了“Unhandled exception type IO”编译时错误。

将这3个错误的语句放在try catch中会导致编译没有问题,但会导致活动无法启动(请参阅代码下面的logcat输出)

try {
    AssetFileDescriptor afda = getAssets().openFd("AudioFile.mp3");
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (SecurityException e) {
    e.printStackTrace();
} catch (IllegalStateException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
MediaPlayer player;
player = new MediaPlayer();


try {
    player.setDataSource(afd.getFileDescriptor());
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (SecurityException e) {
    e.printStackTrace();
} catch (IllegalStateException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}


try {
    player.prepare();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (SecurityException e) {
    e.printStackTrace();
} catch (IllegalStateException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

player.start();

08-13 16:00:41.869: E/AndroidRuntime(16412): FATAL EXCEPTION: main
08-13 16:00:41.869: E/AndroidRuntime(16412): java.lang.RuntimeException: Unable to start activity ComponentInfo{wn.w/wn.w.MainActivity}: java.lang.NullPointerException
08-13 16:00:41.869: E/AndroidRuntime(16412):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at android.app.ActivityThread.access$600(ActivityThread.java:134)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at android.os.Looper.loop(Looper.java:154)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at android.app.ActivityThread.main(ActivityThread.java:4624)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at java.lang.reflect.Method.invokeNative(Native Method)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at java.lang.reflect.Method.invoke(Method.java:511)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at dalvik.system.NativeStart.main(Native Method)
08-13 16:00:41.869: E/AndroidRuntime(16412): Caused by: java.lang.NullPointerException
08-13 16:00:41.869: E/AndroidRuntime(16412):    at wn.w.MainActivity.onCreate(MainActivity.java:71)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at android.app.Activity.performCreate(Activity.java:4479)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
08-13 16:00:41.869: E/AndroidRuntime(16412):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
08-13 16:00:41.869: E/AndroidRuntime(16412):    ... 11 more

在第71行,我有:     player.setDataSource(afd.getFileDescriptor()); (这是在try catch中(参见上面的代码))

1 个答案:

答案 0 :(得分:0)

替换

AssetFileDescriptor afda = getAssets().openFd("AudioFile.mp3");
player.setDataSource(afd.getFileDescriptor());
player.prepare();
player.start();

    AssetFileDescriptor afda;
    try {
        afda = getAssets().openFd("AudioFile.mp3");
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (afd != null) {
        player.setDataSource(afd.getFileDescriptor());
        player.prepare();
        player.start();
    } else {
        Log.i("Check This", "AudioFile.mp3 really exist?  afd is null ");
    }