开发者

MediaPlayer not playing audio properly

I'm trying to play an audio in background from a WebView. This audio is given by a 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.

I tried with MP3, OGG and WAV, and it happens with any of them.

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(".w开发者_高级运维av")){
        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. And the WAV case is using my first attemp, play it with streaming.

Also tried SoundPool and AsyncPlayer... nothing works!!

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

Regards, Manuel.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜