gpt4 book ai didi

java - 无法在 MediaPlayer 中播放带有特殊字符的 URL

转载 作者:行者123 更新时间:2023-12-01 13:09:54 26 4
gpt4 key购买 nike

我尝试使用特殊键字符 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上播放音频流url

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

最佳答案

我认为您的媒体播放器设置有问题。我将这样做:

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

关于java - 无法在 MediaPlayer 中播放带有特殊字符的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22966849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com