“无法创建媒体播放器”和“setDataSource 失败”错误。将 URI 用于数据源时未创建 mediaplayer

"Unable to create media player" and "setDataSource Failed" error. mediaplayer is not getting created when using URI for data source

提问人:hemant borse 提问时间:6/2/2023 最后编辑:James Zhemant borse 更新时间:6/2/2023 访问量:69

问:

当我的 CountDownTimer 完成时,我正在尝试播放声音,但是当我尝试为媒体播放器设置数据源时,它会引发异常,我正在使用 URI 设置数据源,并且我正在从 DataStore 获取该 URI

以下是获取 URI 的代码:

lifecycleScope.launch {
        changeSettings()
        totalTimeForTimer = if (dataStore.getFocusLengthFromDataStore() != null) {
            totalTimeForPomo * 60 * 1000
        } else {
            25 * 60 * 1000
        }
        URI = dataStore.getSoundUri().toUri()
    }

上面的代码在主活动的 oncreate 中运行。

以下代码是 CountDownTimer 的 onFinish():

override fun onFinish() {
            var mp = MediaPlayer()
            try {
                if (URI != null) mp.setDataSource(this@MainActivity, URI!!) //line 171
                else Toast.makeText(this@MainActivity, "null uri", Toast.LENGTH_SHORT).show()
                mp.prepareAsync()
                mp.setOnPreparedListener { mp.start() }
            } catch (e: java.lang.Exception) {
                e.printStackTrace()
            }
        }

当上述代码运行时,我在 logcat 中出现以下错误:

2023-06-02 09:21:46.275 28750-28750 MediaPlayerNative  com.hemant.pomoapp  E  Unable to create media player
2023-06-02 09:21:46.276 28750-28750 AndroidRuntime     com.hemant.pomoapp  D  Shutting down VM
2023-06-02 09:21:46.286 28750-28750 AndroidRuntime     com.hemant.pomoapp  E  FATAL EXCEPTION: main
    Process: com.hemant.pomoapp, PID: 28750
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:641)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978) 
    Caused by: java.io.IOException: setDataSource failed.: status=0x80000000
    at android.media.MediaPlayer.nativeSetDataSource(Native Method)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1205)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1192)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1109)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1018)
    at com.hemant.pomoapp.MainActivity$startClock$1.onFinish(MainActivity.kt:171)
    at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:233)
    at android.app.ActivityThread.main(ActivityThread.java:8068)
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978) 
2023-06-02 09:21:46.349 28750-28750 Process  com.hemant.pomoapp  I  Sending signal. PID: 28750 SIG: 9

错误主要是“无法创建媒体播放器”和“setDataSource 失败”

第 171 行在上面的代码中表示

安卓 android-studio nullpointerexception android-mediaplayer

评论

0赞 zaid khan 6/2/2023
stackoverflow.com/questions/32225687/......在这里查看答案

答: 暂无答案