Lazy Wolf
Lazy Wolf

Reputation: 23

MediaPlayer android stream radio starts not immediately or does not start at all

I use this code:

mediaPlayer.setDataSource("http://some online radio");
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.prepareAsync();

and onPrepared method is:

if (mediaPlayer != null) {
        mediaPlayer.start();
    }

In general, the problem is: then i run this code, playback does not start right away, but about 10 seconds later. + some streams do not start at all, on the emulator it works a little better, than on the device, but still. It depends on concrete radio, some are better, other very bad.

I assume that the matter is in the preparation and buffering. It can be possible to make an InputStream from this stream and write to some temporary file / buffer, and read/play this file in the MediaPlayer, but how to implement it is, not yet clear .. Help please

If you just do mp.prepare, and then mp.start - the result is the same

On a PC in Chrome, all the radio streams that I tried to use immediately start playing

Sorry for my english, thank you.

Upvotes: 0

Views: 1014

Answers (2)

Lazy Wolf
Lazy Wolf

Reputation: 23

If somebody get here for same reason (just in case), solution is ExoPlayer

Something like this:

 LoadControl loadControl = new DefaultLoadControl();


        bandwidthMeter = new DefaultBandwidthMeter();
        extractorsFactory = new DefaultExtractorsFactory();

        trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);

        trackSelector = new DefaultTrackSelector(trackSelectionFactory);
        defaultBandwidthMeter = new DefaultBandwidthMeter();
        dataSourceFactory = new DefaultDataSourceFactory(this,
                Util.getUserAgent(this, "mediaPlayerSample"), defaultBandwidthMeter);


        mediaSource = new ExtractorMediaSource(Uri.parse(radioURL), dataSourceFactory, extractorsFactory, null, null);

        player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
        player.prepare(mediaSource);
        player.setPlayWhenReady(true);

But u better check actual version of examples here: https://github.com/google/ExoPlayer

Upvotes: 1

Jorge Martín
Jorge Martín

Reputation: 278

It seems like audio bitrate might be the key here: https://stackoverflow.com/a/12850965/3454741. There is no easy way to get around this, it seems.

If the streaming does not start at all it might be due to some error, which you could catch using MediaPlayer.setOnErrorListener().

Upvotes: 0

Related Questions