【问题标题】:how to play a video using the videoView and an inputStream如何使用 videoView 和 inputStream 播放视频
【发布时间】:2013-06-23 23:05:47
【问题描述】:

似乎 videoView 只支持几种播放视频的方法,但它们都不支持最通用的播放形式,这很奇怪(因为我认为所有其他方法都使用它)。

我的问题:如何设置 videoView 来播放 inputStream(任何类型的 inputStream,甚至是我自己定制的)?

是否可以不将数据实际复制到文件然后播放它或通过某种技巧“管道”通过数据?

我认为音频缺少相同的东西,但我不确定。

【问题讨论】:

    标签: android inputstream android-videoview playback


    【解决方案1】:

    试试这个:

    public static String getDataSource(InputStream inputStream) throws IOException {
            if (!URLUtil.isNetworkUrl(path)) {
                return path;
           } else {
               URL url = new URL(path);
               URLConnection cn = url.openConnection();
               cn.connect();
                InputStream stream = inputStream;
                if (stream == null)
                    throw new RuntimeException("stream is null");
                File temp = File.createTempFile("mediaplayertmp", "dat");
                temp.deleteOnExit();
                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);
                try {
                    stream.close();
                    out.close();
                } catch (IOException ex) {
                  //  Log.e(TAG, "error: " + ex.getMessage(), ex);
                }
                return tempPath;
            }
        }
    

    【讨论】:

    • 您的代码通过读取整个视频并将其写入文件来工作。它应该可以工作,但我希望的是直播。我什至写过它:“是否可以不将数据实际复制到文件然后播放它或有某种技巧来“管道”数据通过?”
    • 也许可以打开一个写入文件的新线程,而我们设置使用普通线程从文件中读取videoView?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多