0

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.

Lazy Wolf
  • 23
  • 1
  • 5

2 Answers2

1

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

Lazy Wolf
  • 23
  • 1
  • 5
  • I am author of original question, and using ExoPlayer instead of default android media player is totaly resolve asked problem. FFmpegMediaPlayer, from accepted answer, has an issue https://github.com/wseemann/FFmpegMediaPlayer/issues/133, which is not solved at this moment. Or u tell me too put this as a comment, instead of this answer? – Lazy Wolf Sep 13 '18 at 20:13
  • perhaps improve the answer to include code illustrating how ExoPlayer is to be used instead of the default? - "solution is ExoPlayer" is hardly an answer anyone can use, iMHO – blurfus Sep 13 '18 at 20:23
0

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().

Community
  • 1
  • 1
Jorge Martín
  • 258
  • 2
  • 12
  • Thanks for your answer. its sad. what do u think about loading a queue of temporary mp3 files, and playing them, using on completionListener. But how to make every file one size (512kb for example)? read(byte[]) can read something from 0 to whole bute array size, but i don't want any of zero size files. – Lazy Wolf Mar 15 '17 at 16:56
  • I'm not sure if using MP3s would fix this, you could try. I don't really understand your question about "making them one size"... Also, [the answer just above the one I linked](http://stackoverflow.com/a/30494887/3454741) recommends using FFmpegMediaPlayer, which seems to behave better. – Jorge Martín Mar 15 '17 at 17:02
  • it was so close to work. On emulator FFmpegMP is working allright, but on my xiaomi method stop causing fatal error... – Lazy Wolf Mar 15 '17 at 19:01