MediaMetaDataRetriever.setDataSource返回null

时间:2017-11-11 16:13:17

标签: java android stream radio mediametadataretriever

我正在尝试开发一个应用程序(无线电流)应用,我想获取该广播中播放的歌曲的详细信息 MediaMetadataRetriever API 但是当我设置我的URL时,我得到了这个错误:

  

java.lang.IllegalArgumentException异常                                                                              在   android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:71)                                                                              在   maa.myapp.MainActivity $ PlayerTask.onPostExecute(MainActivity.java:447)                                                                              在   maa.myapp.MainActivity $ PlayerTask.onPostExecute(MainActivity.java:419)

这是我的代码:

MediaPlayer mediaPlayer;
    String stream = "https://plaza.one/mp3";
    boolean prepared =false;
    boolean started =false ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaPlayer = new MediaPlayer();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            AudioAttributes attribs = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();
            mediaPlayer.setAudioAttributes(attribs);

        } else {
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }


        new PlayerTask().execute(stream);
}
    public class PlayerTask extends AsyncTask<String,Void,Boolean> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loadingRL.setVisibility(View.VISIBLE);
        }
        @Override
        protected Boolean doInBackground(String... strings) {
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(strings[0]);
                prepared=true;
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return prepared;
        }
        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);

            /*get details : */
            MediaMetadataRetriever mMediaMetaDataRetriever = new MediaMetadataRetriever();
            mMediaMetaDataRetriever.setDataSource(stream,new HashMap<String, String>());
            String titleName = mMediaMetaDataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
            Toast.makeText(MainActivity.this, "Title : "+titleName, Toast.LENGTH_SHORT).show();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

@Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);

        MediaMetadataRetriever mmr = new MediaMetadataRetriever(); 

        if (Build.VERSION.SDK_INT >= 14){
          mmr.setDataSource(stream, new HashMap<String, String>()); 
        }else {
          mmr.setDataSource(stream); 
        }

        String titleName = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));
        Toast.makeText(MainActivity.this, "Title : "+titleName, Toast.LENGTH_SHORT).show();
    }

如果这不起作用,那么您应该确保您使用的链接实际上返回一个文件。

尝试在onCreate内执行上述操作,问题可能与您的AsyncTask有关。

答案 1 :(得分:0)

最后它适用于&#39; com.vodyasov:amr:0.5&#39;

Uri uri = Uri.parse(stream);
            OnNewMetadataListener listener = new OnNewMetadataListener() {
                @Override
                public void onNewHeaders(String stringUri, List<String> name, List<String> desc, List<String> br, List<String> genre, List<String> info) {}
                @Override
                public void onNewStreamTitle(String stringUri, String streamTitle) {
                    songinfo.setText("Song Name : "+streamTitle);
                }
            };
            AudiostreamMetadataManager.getInstance()
                    .setUri(uri)
                    .setOnNewMetadataListener(listener)
                    .setUserAgent(UserAgent.WINDOWS_MEDIA_PLAYER)
                    .start();