Android 2.1之前的Android电视直播
我正在开发一个应用程序,我必须在其中实现直播电视流。我的谷歌搜索让我相信,在2.1版Android之前,流媒体直播是不可能的 是这样吗 当我得到mediaplayer的流媒体音乐代码时,我可以通过设置以下方法使用它的类型:Android 2.1之前的Android电视直播,android,streaming,live,Android,Streaming,Live,我正在开发一个应用程序,我必须在其中实现直播电视流。我的谷歌搜索让我相信,在2.1版Android之前,流媒体直播是不可能的 是这样吗 当我得到mediaplayer的流媒体音乐代码时,我可以通过设置以下方法使用它的类型: private void setDataSource(String path) throws IOException { if (!URLUtil.isNetworkUrl(path)) { mp.setDataSource(path)
private void setDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
mp.setDataSource(path);
} else {
Log.i("enter the setdata","enter the setdata");
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
mp.setDataSource(tempPath);
try {
stream.close();
Log.i("exit the setdata","exit the setdata");
}
catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
}
}
mp.setAudioStreamType(2)
但我想知道的是,对于流式传输来说,这样的代码和保存文件是否足够,方法如下:
private void setDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
mp.setDataSource(path);
} else {
Log.i("enter the setdata","enter the setdata");
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
mp.setDataSource(tempPath);
try {
stream.close();
Log.i("exit the setdata","exit the setdata");
}
catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
}
}
private void setDataSource(字符串路径)引发IOException{
如果(!URLUtil.isNetworkUrl(路径)){
mp.setDataSource(路径);
}否则{
Log.i(“输入设置数据”、“输入设置数据”);
URL=新URL(路径);
URLConnection cn=url.openConnection();
cn.connect();
InputStream=cn.getInputStream();
if(流==null)
抛出新的RuntimeException(“流为null”);
File temp=File.createTempFile(“mediaplayertmp”、“dat”);
字符串tempPath=temp.getAbsolutePath();
FileOutputStream out=新的FileOutputStream(临时);
字节buf[]=新字节[128];
做{
int numread=stream.read(buf);
如果(numread回答“是否足够”:绝对不行
您正在将URL中的所有数据保存到设备上,然后播放。如果您可以保证它是一个小片段,则此操作有效,但“直播电视流”意味着我们正在谈论以实时速率发送的长度未知的流
其影响是:
在播放开始之前,一个N分钟长的程序需要N分钟才能流到设备上
长时间广播有可能填满所有可用存储空间
该方法应该从任何可以获取文件描述符的源读取数据,包括套接字
如何使用它的具体细节会因您使用的协议而异,但基本上您需要从广播源读取数据,将其转换为合适的格式,然后通过管道将其传输到fd。流式电视是什么意思?您使用的是什么协议。安卓1.x有一个移动电视应用程序(例如。只有在您拥有SFR订阅时才在法国有效。请分享您如何解决此问题?