7

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.

Matt
  • 17,290
  • 7
  • 57
  • 71
Anton Balashov
  • 940
  • 1
  • 12
  • 20
  • 1
    I faced of with getting metadata from stream, but I didn't work with audio. You have to look at com.google.android.exoplayer.audio.AudioTrack there's initialize method and in this method lib works with audioTrack maybe it will help you. – Dima Nov 24 '15 at 10:49
  • Any way you can share the code? im looking for a way to upgrade my app from mediaplayer to exoplayer? thank you – alexistkd Jan 20 '16 at 16:35
  • @Alexistkd, I used IcyStreamMeta from answer below. – Anton Balashov Jan 20 '16 at 20:03

5 Answers5

10

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.

Renaud Boulard
  • 709
  • 6
  • 9
9

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);
Sandeep Rana
  • 3,251
  • 2
  • 24
  • 38
  • 1
    @Navdroid Basically this function Returns a user agent string based on the given application name and the library version. So, You need not to worry about what User-Agent string should i use. Just Call that function and it will automatically create a User Agent String for you to use. To check what it returns. Try to run this statement in Debugger. – Sandeep Rana Jul 31 '18 at 16:24
  • thanks .. this is working great . Could you also let me know how could I refresh the info that I get in IcyHeaderListener – Navdroid Jul 31 '18 at 17:25
  • Only Name is retrieved using this library, if i want to get also cover photo of playing song which library i used? – Asad Mukhtar Sep 19 '18 at 10:40
  • http://www.smackfu.com/stuff/programming/shoutcast.html I think this could help you. "If the client sends the Icy-MetaData:1 header this means the client supports ICY-metadata. The server should respond icy-metaint: 8192. 8192 is the number of bytes between 2 metadata chunks. It is suggested to use this value as some players might have issues parsing other values. In these chunks StreamTitle='title of the song'; send the new song title. There also is a StreamURL field which should be able to also send album art links or more info. The exact implementation is however unknown." – Sandeep Rana Sep 20 '18 at 20:22
  • @SandeepSinghRana I was able to get the metadata using this extension but I receive the metadata 30-40 seconds earlier ie the current song plays and I get the metadata of the next song while I still listen to current song and after 30-40 seconds I listen to the next song playing. – vinay kumar Nov 21 '18 at 18:44
  • @AsadMukhtar If you want to get the album art/cover photo us should use ITMS(I tuens) or LastFM's API. – vinay kumar Nov 21 '18 at 18:48
3

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

Dan Brough
  • 2,745
  • 1
  • 24
  • 24
0

You probably should use MediaMetadataRetriever

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(getActivity(), uri);
String artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
Smileek
  • 2,702
  • 23
  • 26
0

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

penkzhou
  • 1,200
  • 13
  • 30