MediaPlayer not playing audio properly

走远了吗. 提交于 2019-12-05 21:52:01

You are creating audioLoader and/or MediaPlayer objects in a local way (those objects are locals for the function shouldOverrideUrlLoading). So, once out of the function, when the garbage collector try to collect all non-referenced objects, it will destroy your objects and then the sound will stopped.

Try declaring AudioLoader and MediaPlayer objects as global objects:

private AudioLoader audioLoader; 
private MediaPlayer player; 
public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".ogg")){
            audioLoader = new AudioLoader(url);
            audioLoader.start();
            return true;
        }
        else if (url.endsWith(".wav")){
            Uri tempPath = Uri.parse(url);
            player = MediaPlayer.create(interfazWeb, tempPath);
            if (player != null){
                player.start();
            } else {
                Log.e(TAG, "No se puede abrir el audio:" + url);
            }
            return true;
        }
        else if (url.endsWith(".mp3")){
            audioLoader = new AudioLoader(url);
            audioLoader.start();
            return true;
        }else{
            return super.shouldOverrideUrlLoading(view, url);
        } 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!