安卓电视直播

chi*_*dev 7 streaming android live

我正在开发应用程序,我必须实现直播电视流媒体.我的谷歌搜索让我相信直到2.1 android才能实现直播.

这样对吗?

当我获得媒体播放器的音乐代码时,我可以通过设置以下方法来使用它的类型:

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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

直播电视流媒体需要额外的东西吗?请指导我.

Phi*_*llo 5

坚持"这是否足够":绝对不是.

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

这样做的影响是:

  1. N分钟长的节目将在播放开始前花费N分钟流式传输到设备.
  2. 长时间广播有可能填满所有可用存储空间.

MediaPlayer.setDataSource(FD FileDescriptor的)方法应该从任何来源读取数据,你可以得到FileDescriptor的,包括插座.

如何使用这个确切的细节会根据您所使用的协议,但本质上,你需要从广播源中读取数据,其转码到合适的形式,它管到FD.