JohnA
JohnA

Reputation: 345

Android: prepareAsync() during audio streaming delay

I'm trying to play audio from this url: http://mtl2.liveatc.net/lbbg Here is my code:

public void setConnection(String url) {
    String url = http://mtl2.liveatc.net/lbbg;
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    new Player().execute(url);
}

private class Player extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... strings) {

        try {
            mediaPlayer.setDataSource(strings[0]);
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                }
            });

            mediaPlayer.prepareAsync();

        } catch (Exception e) {
            Log.e("MyAudioStreamingApp", e.getMessage());
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){

            @Override
            public void onPrepared(MediaPlayer mp) {
                progressBar.setVisibility(View.INVISIBLE);
                mp.start();
            }
        });

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressBar.setVisibility(View.VISIBLE);
    }
}

And everything work, but with really huge delay. If I open this url in browser, it took near 20 seconds to start streaming, but when I run it in my app, it took up to 5-7 minutes.

UPD also add my log:

07-24 01:12:52.820 25918-25961/com.example I/OpenGLRenderer: Initialized EGL, version 1.4
07-24 01:12:52.843 25918-25961/com.example E/HAL: hw_get_module_by_class: module name gralloc
07-24 01:12:52.843 25918-25961/com.example E/HAL: hw_get_module_by_class: module name gralloc
07-24 01:12:59.171 25918-25918/com.example E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
07-24 01:12:59.171 25918-25918/com.example E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
07-24 01:12:59.171 25918-25918/com.example E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
07-24 01:12:59.171 25918-25918/com.example E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
07-24 01:12:59.188 25918-25931/com.example D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 6.0.1)
07-24 01:12:59.191 25918-26002/com.example D/MediaHTTPConnection: proxy null port 0
07-24 01:12:59.192 25918-26002/com.example I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
07-24 01:15:42.557 25918-25925/com.example W/art: Suspending all threads took: 31.418ms
07-24 01:17:48.107 25918-25918/com.example D/MediaPlayer: setSubtitleAnchor in MediaPlayer

UPD2: My "solution": I couldn't find solution for this problem so I just decided to use next thing: I placed small WebView there I need play button, and received full audio player that load and play this stream in 10-15 seconds.

Upvotes: 3

Views: 832

Answers (2)

You Kim
You Kim

Reputation: 2795

Check your thread priority.

Thread.currentThread().getPriority();

In my experience, some old(Android 5~6) devices (ex:LG.. ) had a such issue when mediaPlayer is prepared or started in worker thread like AsyncTask. In such case, set priority higher or use Main thread. In my case, moved prepareAsync to Main thread.

UPDATED

I tested the code and found that priority is not an issue. I think Android mediaPlayer is not a good option for "that" url. I used to choose ffmpeg or google/exoplayer for streaming projects.

Response Header

HTTP/1.1 200 OK
Server: nginx/1.12.1
Content-Type: audio/mpeg
Connection: close
icy-br: 16
ice-audio-info: ice-samplerate=11025;ice-bitrate=16;ice-channels=1
icy-br: 16
icy-description: LBBG Burgas, Bulgaria
icy-genre: ATC
icy-name: Burgas Tower, Approach
icy-private: 0
icy-pub: 1
icy-url: http://www.liveatc.net
...

Media Info

Input #0, mp3, from 'http://mtl2.liveatc.net/lbbg':
Duration: N/A, start: 0.000000, bitrate: 16 kb/s
Stream #0.0: Audio: mp3, 11025 Hz, mono, s16, 16 kb/s

Upvotes: 1

Ranjith KP
Ranjith KP

Reputation: 810

Without using task,You can do it simply :

MediaPlayer mediaPlayer = new MediaPlayer();

mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
    public boolean onError(MediaPlayer mp, int what, int extra) {
        mp.reset();
        return false;
    }
});

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

try {
    mediaPlayer.setDataSource("http://yoururl");
    mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
} catch (IllegalStateException e) {
} catch (IOException e) {
}

Try this simple method and check the delay is still exist or not.

Upvotes: 0

Related Questions