MediaMetadataRetriever on streaming source

2020-02-10 04:56发布

I am having a tough time displaying the current track playing from a streaming source. Using MediaMetaDataRetriever class returns a NULL, meaning it failed. I wrote a simple method but now I'm inclining to think this only works on media files like MP3, WMA, AVI, etc.

public void metaData(){
        mmr.setDataSource(urlStation);
        String metaD = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST);
        Toast.makeText(MainActivity.this, "Artist = " + metaD, Toast.LENGTH_LONG).show();

Any good and easy recommandations would be greatly appreciated. Thank you for your time!

3条回答
姐就是有狂的资本
2楼-- · 2020-02-10 05:33

MediaMetadataRetriever doesn't work with URL's on certain versions of Android, see this issue. I created FFmpegMediaMetadataRetriever as a workaround, I suggest trying it out.

查看更多
\"骚年 ilove
3楼-- · 2020-02-10 05:35

According to the same issue listed in the other answer, you just need to use the setDataSource method with these arguments:

setDataSource(String uri, Map<String, String> headers)

So for example,

public void getStreamMetaData(String url){
  MediaMetadataRetriever mmr = new MediaMetadataRetriever();
  mmr.setDataSource(url, new HashMap<String, String>());
  //now do whatever you want

}

I tested it with a streaming mp4 and it worked for me. Also just a note, you'll want to use the setDataSource method in an AsyncTask or on another thread, since it may take a while to run according to the docs.

查看更多
家丑人穷心不美
4楼-- · 2020-02-10 05:44

This method works better:

public void setDataSource (String uri, Map<String, String> headers)

(with the url of your stream and an empty map e.g setDataSource("https://somewebsite.com/somefile.mp3", new HashMap<String,String>());)

查看更多
登录 后发表回答