Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExoPlayer: get songs metadata from HTTP stream

I use the following code to play a music stream through ExoPlayer:

        exoPlayer = ExoPlayer.Factory.newInstance(numRenderers, minBufSize, maxBufSize);
        String url = Helper.getPr().getString("url", "http://mp3.nashe.ru:80/ultra-128.mp3");
        Uri uri = Uri.parse(url);
        Log.i(TAG, "Going to open " + url);
        Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
        DataSource dataSource = new DefaultUriDataSource(getApplicationContext(), USER_AGENT);
        ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri, dataSource, allocator, BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE);
        audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
        exoPlayer.addListener(this);
        exoPlayer.sendMessage(audioRenderer, MediaCodecAudioTrackRenderer.MSG_SET_VOLUME, volume);
        exoPlayer.prepare(audioRenderer); 
        exoPlayer.setPlayWhenReady(true);

I can't find any info on how to get metadata like artist and name of the current song. Is it possible to get the metadata and if yes, how?
Thanks a lot.

like image 399
Anton Balashov Avatar asked Nov 19 '15 11:11

Anton Balashov


3 Answers

There are many types of metadata in many types of media, It's depending on your stream. But currently Exoplayer itself, only parse metadata from HLS streams (HTTP Live Streaming) they get ID3 data from the stream.

As you can see on there github repository issue,this is the current state of metadata in Exoplayer lib (August 2015): https://github.com/google/ExoPlayer/issues/704

If it's your stream case, I will recommend you to download the Exoplayer demo on github (https://github.com/google/ExoPlayer/tree/release-v2/demos). One of the examples in the demo display stream ID3 metadata on LogCat.

If it's not your case, nothing will help you in ExoPlayer lib right now.

But there is an alternative solution, which I have used for a radio stream application, and it work well:

IcyStreamMeta to get meta data from online stream radio :

Getting metadata from SHOUTcast using IcyStreamMeta

but not sure it will work with simple mp3 file.

like image 126
Renaud Boulard Avatar answered Sep 22 '22 18:09

Renaud Boulard


I am late But with Exoplayer-2. You can use an extension created by @saschpe. https://github.com/sandeeprana011/android-exoplayer2-ext-icy

For example see this app: https://play.google.com/store/apps/details?id=com.zilideus.jukebox_new

gradle

implementation 'saschpe.android:exoplayer2-ext-icy:1.0.1'

So it includes an Extra Header "Icy-Metadata" with value 1 and on the response, it extracts the metadata from the stream data.

Usage:

 IcyHttpDataSourceFactory factory = new IcyHttpDataSourceFactory.Builder(Util.getUserAgent(this, getResources().getString(R.string.app_name)))
            .setIcyHeadersListener(this)
            .setIcyMetadataChangeListener(this).build();
DefaultDataSourceFactory datasourceFactory = new DefaultDataSourceFactory(getApplicationContext(), null, factory);

ExtractorMediaSource mediaSource = new ExtractorMediaSource.Factory(datasourceFactory)
        .setExtractorsFactory(new DefaultExtractorsFactory())
        .createMediaSource(uri);
Exo.resetPlayer();
Exo.getPlayer(this).prepare(mediaSource);
like image 30
Sandeep Rana Avatar answered Sep 22 '22 18:09

Sandeep Rana


In the case of Icy metadata, exoplayer has built in support as of version 2.10. see: https://stackoverflow.com/a/56333429/1898380

like image 40
Dan Brough Avatar answered Sep 23 '22 18:09

Dan Brough