无法在MediaPlayer中播放具有特殊字符的URL

时间:2014-04-09 15:13:13

标签: java android mp3 android-mediaplayer urlencode

我正在尝试使用特殊键字符url播放MediaPlayer但无法播放并获取错误代码。虽然我也用直接输入Url播放它但仍然出错。

 //// Input
String urlStr = "http://mydomain/php/playfile.php?src=7580&dst=7711&calldate=2014-04-09%2015:23:19&amaflags=3&duration=294&billsec=289&lastapp=Dial&mp3file=exten-7711-7580-20140409-152319-1397042599.3211.mp3"

URL localurl = new URL(urlStr);
            URI uri = new URI(localurl.getProtocol(), localurl.getUserInfo(),
                    localurl.getHost(), localurl.getPort(), localurl.getPath(),
                    localurl.getQuery(), localurl.getRef());
            localurl = uri.toURL();


            Log.d("localurl ", localurl.toString());
            Log.d("localurl.getQuery()", localurl.getQuery());

            String queryStr = localurl.getQuery();
            String pathStr = localurl.getPath();

            Log.d("localurl.getPath()", localurl.getPath());
            Log.d("uri.toASCIIString()", uri.toASCIIString());

            String encodeUrlStr = URLEncoder.encode(queryStr, "UTF-8");

            Log.d("encodeUrlStr", encodeUrlStr);


            String urlToplay = String.format("http://%s%s?%s", MasterView.mPreferences.getString("DomainName", ""),pathStr,encodeUrlStr);

            Log.d("url to play", urlToplay);



//// Output
http://mydomain/php/playfile.php?src%3D7580%26dst%3D7711%26calldate%3D2014-04-09%252015%3A23%3A19%26amaflags%3D3%26duration%3D294%26billsec%3D289%26lastapp%3DDial%26mp3file%3Dexten-7711-7580-20140409-152319-1397042599.3211.mp3

并在MediaPlayer上播放音频流网址

 try {


                mediaplayer = new MediaPlayer();
                mediaplayer.reset();
                mediaplayer.setDataSource(urlToplay);
                mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                // mediaplayer.prepare();

                mediaplayer
                        .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            public void onPrepared(MediaPlayer mp) {
                                mediaplayer.start();
                            }
                        });
                mediaplayer.prepareAsync();

            } catch (Exception e) {

                Log.i("Exception", "Exception in streaming mediaplayer e = "
                        + e);
                e.printStackTrace();
            }

1 个答案:

答案 0 :(得分:1)

我认为你的媒体播放器设置存在问题。 我将如何做到这一点:

 mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(MP3_URL);
    mediaPlayer.prepareAsync();
    mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
                mediaPlayer.stop();
                mediaPlayer.reset();
        }
});
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

    @Override
    public void onPrepared(MediaPlayer mp) {
      mediaPlayer.start();
            }
    });