简体   繁体   English

MediaPlayer无法正常播放音频

[英]MediaPlayer not playing audio properly

I'm trying to play an audio in background from a WebView. 我正在尝试从WebView播放背景音频。 This audio is given by a URL. 此音频由URL提供。 I approached this by overriding the url loading. 我通过覆盖网址加载来解决这个问题。 And it starts playing the audio, but many times the Media Player just stops. 它开始播放音频,但很多时候媒体播放器停止播放。 This happens around 30% of times, and this audio files are never longer than 30 seconds. 这种情况发生在大约30%的时间,并且此音频文件永远不会超过30秒。

I tried with MP3, OGG and WAV, and it happens with any of them. 我尝试过使用MP3,OGG和WAV,它们都适用于任何一种。

I also tried but first downloading the file and then playing it, instead of stream it, but doesn't work either. 我也试过但是首先下载文件然后播放它,而不是流式传输,但也不起作用。

This is a piece of code... to show you how it works: 这是一段代码......向您展示它是如何工作的:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.endsWith(".ogg")){
        AudioLoader audioLoader = new AudioLoader(url);
        audioLoader.start();
        return true;
    }
    else if (url.endsWith(".wav")){
        Uri tempPath = Uri.parse(url);
        MediaPlayer 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 audioLoader = new AudioLoader(url);
        audioLoader.start();
        return true;
    }else{
        return super.shouldOverrideUrlLoading(view, url);
    }
}

I've checked the audio file saved by "AudioLoader", and it's totally fine. 我检查了“AudioLoader”保存的音频文件,它完全没问题。 And the WAV case is using my first attemp, play it with streaming. WAV案例正在使用我的第一次尝试,用流媒体播放。

Also tried SoundPool and AsyncPlayer... nothing works!! 还试过SoundPool和AsyncPlayer ......什么都行不通!!

So... this far I don't think is a communication, codec or buffer issue. 所以......到目前为止,我认为这不是通信,编解码器或缓冲区问题。 My only tips are these log entries, which repeats for every time tha the problem happens, with all formats and all approaches: 我唯一的提示是这些日志条目,每次问题发生时都会重复,包括所有格式和所有方法:

12-31 09:41:49.284: WARN/AudioFlinger(59): write blocked for 160 msecs, 20 delayed writes, thread 0xd7a8
12-31 09:41:49.554: WARN/TimedEventQueue(59): Event 6 was not found in the queue, already cancelled?

Does anybody please have some clue? 有人请问一些线索吗? Or I'm just facing a bug/missfunction. 或者我只是面临一个错误/失误。

Have a happy 2011, specially if you're able to help me :P 祝你2011年快乐,特别是如果你能帮助我的话:P

Regards, Manuel. 此致,曼努埃尔。

You are creating audioLoader and/or MediaPlayer objects in a local way (those objects are locals for the function shouldOverrideUrlLoading). 您正在以本地方式创建audioLoader和/或MediaPlayer对象(这些对象是函数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: 尝试将AudioLoader和MediaPlayer对象声明为全局对象:

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

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

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