2011-02-10 65 views
7

我正在开发应用程序,我必须实现直播电视流。我的谷歌搜索引导我相信直播2.1 android之前,直播是不可能的。安卓直播电视

是不是?

至于我,我可以使用它的类型设置以下方法来获取媒体播放器的音乐straming代码:

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

有需要的直播电视流的任何额外的东西?请给我指导。

+0

流媒体电视是什么意思?你使用什么协议。 – dongshengcn 2011-02-17 02:21:31

回答

5

地址“是否足够”:绝对不是。

您将所有数据从URL保存到设备,然后回放。如果你可以保证它是一个小剪辑,这是有效的,但是“直播电视流”意味着我们正在讨论以实时速率发送的未知长度的流。

这种影响是:

  1. A N-分钟长的方案将取N-分钟回放开始之前流式传输至所述装置。
  2. 长时间广播有可能填满所有可用存储空间。

MediaPlayer.setDataSource(FileDescriptor fd)方法应该从任何可以获取FileDescriptor的源读取数据,包括套接字。

关于如何使用它的具体细节将根据您使用的协议而有所不同,但基本上您需要从广播源读取数据,将其转码为合适的格式,然后将其传送到fd。