2011-02-10 60 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。