我正在开发必须实现直播电视流的应用程序。我的Google搜索使我相信,直到2.1 Android才能进行实时流式传输。

这样对吗?

当我得到mediaplayer流音乐的代码时,可以通过设置以下方法来使用它的类型:
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);
            }
        }
    }

直播电视流是否需要其他东西?

最佳答案

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

您正在将所有数据从URL保存到设备,然后进行播放。如果您可以保证它是一个小 fragment ,则此方法有效,但是“实时电视流”意味着我们正在谈论以实时速率发送的未知长度的流。

其影响是:

  • 一个N分钟的程序将在播放开始之前花N分钟的时间流式传输到设备。
  • 长时间广播可能会耗尽所有可用的存储空间。

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

    具体使用方法的具体细节将根据所使用的协议(protocol)而有所不同,但是从本质上讲,您需要从广播源中读取数据,将其转码为合适的格式,然后将其通过管道传输到fd。

    关于android - Android 2.1之前的Android TV的电视直播,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4954142/

    10-09 13:01