gpt4 book ai didi

android - 如何在 android 中使用 .pls 文件进行流式传输?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:00:40 25 4
gpt4 key购买 nike

我想使用此 url http://playerservices.streamtheworld.com/pls/VIRGINRADIO_DUBAIAAC.pls 为我的 android 应用程序播放 .pls 文件 我知道无法直接使用 MediaPlayer 播放 .pls 文件。所以我使用 Pls 解析器解析了这个文件,并将每个 url 设置为媒体播放器。但它不起作用。还显示错误 error ( 1, -2147483648).

public class PlayListParser {

private BufferedReader reader;

public PlayListParser(String url) {
try {
URL plsFileUrl = new URL(url.trim());
URLConnection urlConnection = plsFileUrl.openConnection();
// InputStream input = new BufferedInputStream(urlConnection.openStream());
InputStream iStream = urlConnection.getInputStream();
this.reader = new BufferedReader(new InputStreamReader(iStream));
// this.reader = new BufferedReader(new FileReader(file), 1024);
} catch (MalformedURLException e) {
Log.e("PlayListParser", "Got MalformedURLException = " + e.getMessage());
} catch (IOException e) {
Log.e("PlayListParser", "Got IOException = " + e.getMessage());
}
}

public List<String> getUrls() {
LinkedList<String> urls = new LinkedList<String>();
while (true) {
try {
String line = reader.readLine();
if (line == null) {
break;
}
String url = parseLine(line);
if (url != null && !url.equals("")) {
urls.add(url);
}
} catch (IOException e) {
e.printStackTrace();
}
}
return urls;
}

private String parseLine(String line) {
if (line == null) {
return null;
}
String trimmed = line.trim();
if (trimmed.indexOf("http") >= 0) {
return trimmed.substring(trimmed.indexOf("http"));
}
return "";
}
}


PlayListParser playListParser = new PlayListParser(URL_PLS_STREAMING);
List<String > playList = playListParser.getUrls();
if(playList != null && ! playList.isEmpty()){
for(String url : playList){
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(playList.get(0));//"http://stream2.streamq.net:8020/");
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

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

}
});
//mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
Log.e("MainActivity","Got IllegalArgumentException = " + e.getMessage());

} catch (IllegalStateException e) {
Log.e("MainActivity","Got IllegalStateException = " + e.getMessage());

} catch (IOException e) {
Log.e("MainActivity","Got IOException = " + e.getMessage());

}
}
}

如何播放这个 .pls 文件?我找不到关于它的好引用。我还想播放、暂停和倒带这些文件。

提前致谢

最佳答案

从.pls文件获取URL

这将返回类似 http://stream2.streamq.net:8020 的 URL

package com.direct.radio.global;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedList;
import android.content.Context;
import android.util.Log;

public class GetStreamingUrl {

private static String LOGTAG = "GetStreamingUrl";
private Context mContext;

public GetStreamingUrl(Context context) {
Log.i(LOGTAG, "call to constructor");
this.mContext = context;

}

public LinkedList<String> getStreamingUrl(String url) {

Log.i(LOGTAG, "get streaming url");
final BufferedReader br;
String murl = null;
LinkedList<String> murls = null;
try {
URLConnection mUrl = new URL(url).openConnection();
br = new BufferedReader(
new InputStreamReader(mUrl.getInputStream()));
murls = new LinkedList<String>();
while (true) {
try {
String line = br.readLine();

if (line == null) {
break;
}
murl = parseLine(line);
if (murl != null && !murl.equals("")) {
murls.add(murl);

}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(LOGTAG, "url to stream :" + murl);
return murls;
}

private String parseLine(String line) {
if (line == null) {
return null;
}
String trimmed = line.trim();
if (trimmed.indexOf("http") >= 0) {
return trimmed.substring(trimmed.indexOf("http"));
}
return "";
}
}

Activity_Player.java 或 Service_Player.java

你可以根据需要编写这段代码,定义这个方法

LinkedList<String> urls;

private LinkedList<String> fGetPlayableUrl(String mPls) {
GetStreamingUrl oGetStreamingUrl = new GetStreamingUrl(Activity_Splash.this);
urls = oGetStreamingUrl.getStreamingUrl(mPls);
return urls;
}

这里,fGetPlayableUrl(String mPls) 传递.pls URL。现在您有了流媒体 URL。

MediaPlayer  mMediaPlayer = new MediaPlayer();

现在,将 URL 传递给

mMediaPlayer.setDataSource(urls.toString());
mMediaPlayer.prepareAsync();
mMediaPlayer.start();

关于android - 如何在 android 中使用 .pls 文件进行流式传输?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16931555/

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