1

这是我的代码:(例如,假设我将其放在 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();
}

我尝试将每个给出未处理 IO 异常错误的语句放在 try catch 语句中。但是在这样做之后,应用程序会出现 ANR,无法启动,并且 logcat 会给出类似“无法初始化 MainActivity”的信息......

经过进一步研究,我发现此代码对其他人都有效,也许我没有添加任何东西..

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

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

这是修改后的代码和它产生的运行时错误的 logcat。基本上,我放在 try catch 框中的所有语句都会给出“未处理的异常类型 IO”编译时错误。

将这3条错误语句放在try catch中编译时没有问题,但导致activity无法启动(见代码下方的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 中(见上面的代码))

4

1 回答 1

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 ");
    }
于 2013-08-13T10:46:55.890 回答