gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:54:39 24 4
gpt4 key购买 nike

这是我的代码:
(例如,假设我将其放在OnCreate中以在 Activity 启动时播放声音。)

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”的信息。

经过进一步研究,我发现此代码可用于其他所有人,也许我没有添加任何内容。

此代码有什么问题?如何纠正呢?

(或者,是否有更好,更有效或更简便的方法来播放文件?我只需要播放/暂停功能。)
(我有一个[scrollable tab + swipe]导航应用程序,我想在每个选项卡的屏幕上播放不同的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中(请参见上面的代码))

最佳答案

更换

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 ");
}

关于android - 未处理的IO异常?使用媒体播放器播放mp3文件-Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18206434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com