简体   繁体   English

MediaPlayer 从 Uri 加载声音不起作用

[英]MediaPlayer loading sound from Uri not working

I have a simple voice recorder app.我有一个简单的录音机应用程序。 When the user records something it gets saved on the external storage and after that it gets played from there.当用户录制某些内容时,它会保存在外部存储中,然后从那里播放。

Everything works fine except on a Huawei P8 Lite smartphone with Android 6.除了配备 Android 6 的华为 P8 Lite 智能手机外,一切正常。

Here is how I play the sound:这是我播放声音的方式:

    Thread th=new Thread(new Runnable() {
        @Override
        public void run() { 
            try {
                MediaPlayer mp = new MediaPlayer();
                mp.setDataSource(view.getContext(), uri);
                mp.prepare();
                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.release();
                    }
                });

                mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                    }
                });

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //send message to handler
        }
    });
    th.start();

The Uri does exist at this moment and also the file does exist in the destination.此时 Uri 确实存在,并且该文件确实存在于目标中。 Why is my mediaPlayer not playing anything?为什么我的 mediaPlayer 没有播放任何内容? The permissions are given at this point where it tries to create the MediaPlayer so it should not be a permission issue.权限是在它尝试创建 MediaPlayer 时给出的,因此它不应该是权限问题。

All I get in the logcat is:我在 logcat 中得到的只是:

W/MediaPlayer-JNI: MediaPlayer finalized without being released 
I/MediaPlayer: setDataSource(34, 0, 576460752303423487) 
I/MediaPlayer: [HSM] stayAwake true uid: 10255, pid: 22592 
E/MediaPlayer: error (-19, 0) 
E/MediaPlayer: Error (-19,0)

Like I said it works fine on all other devices I tested (Android 10 & Android 9)就像我说的,它在我测试的所有其他设备上都能正常工作(Android 10 和 Android 9)

EDIT编辑

Here is what I tried: I created Emulators with every API level from API 18 to 30 and it works fine on every emulator.这是我尝试过的:我使用从 API 18 到 30 的每个 API 级别创建了模拟器,并且它在每个模拟器上都可以正常工作。 I reseted my Huawei P8 Lite smartphone to factory settings.我将华为 P8 Lite 智能手机重置为出厂设置。 I checked if the sound file is saved properly and if I can open and play it manualy when I navigate in the folder where it gets saved (it works perfectly so it has nothing to do with the code that saves the sound) I really run out of ideas.我检查了声音文件是否正确保存,以及当我在保存它的文件夹中导航时是否可以手动打开并播放它(它工作得很好,所以它与保存声音的代码无关)我真的用完了的想法。

Hope someone can help, Thanks!希望有人能帮忙,谢谢!

You need to player prepare asynchronously.您需要异步播放器准备。 player. prepare() player. prepare() replace with below code: player. prepare()替换为以下代码:

player.prepareAsync()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM