MediaPlayer does not play sound correctly

I am trying to play audio in the background from a WebView. This sound is provided at the URL. I approached this by overriding URL loading. And it starts playing audio, but many times Media Player just stops. This happens in about 30% of cases, and this audio file never exceeds 30 seconds.

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

I also tried, but downloaded the file first and then played it, not a stream, but it doesn’t work.

This is part of the 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 checked the audio file saved with "AudioLoader" and it is completely perfect. And the WAV case uses my first approach, reproduces it with streaming.

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

... , , . - , :

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?

- ? /.

2011 , : P

, .

+3
1

audioLoader / MediaPlayer ( shouldOverrideUrlLoading). , , , , .

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

Source: https://habr.com/ru/post/1782901/


All Articles