Anton Balashov
Anton Balashov

Reputation: 1030

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.

Upvotes: 7

Views: 16378

Answers (5)

Renaud Boulard
Renaud Boulard

Reputation: 709

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.

Upvotes: 10

Dan Brough
Dan Brough

Reputation: 2995

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

Upvotes: 3

Sandeep Rana
Sandeep Rana

Reputation: 3441

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);

Upvotes: 9

penkzhou
penkzhou

Reputation: 1210

You could use the follow code to do that(Maybe you should download the mp3 file first):

MediaMetadataRetriever metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource("LOCAL MP3 FILE PATH"); 
byte[] picArray = metaRetriver.getEmbeddedPicture(); 
Bitmap songImage = BitmapFactory .decodeByteArray(picArray, 0, picArray.length); 
String albumName = metaRetriver .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
String artistName = metaRetriver .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
String songGenre = metaRetriver .extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE)); 

source:http://mrbool.com/how-to-extract-meta-data-from-media-file-in-android/28130#ixzz3s8PUd9E7

Upvotes: 0

Smileek
Smileek

Reputation: 2782

You probably should use MediaMetadataRetriever

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(getActivity(), uri);
String artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);

Upvotes: 0

Related Questions