如何在服务器上直播来自android的mp4视频 - 仅适用于KitKat?

时间:2014-10-15 11:10:29

标签: android ffmpeg video-streaming mp4

如何从服务器流式传输Android中的MP4视频?这是我的代码,但它只适用于KitKat。

 private void play() {
            try {
                final String path = ""
                Log.v(TAG, "path: " + path);
                if (path == null || path.length() == 0) {
                    Toast.makeText(getApplicationContext(), "File URL/path is empty",
                            Toast.LENGTH_LONG).show();

                } else {
                    // If the path has not changed, just start the media player
                    if (path.equals(current) && mVideoView != null) {

                        mVideoView.start();
                        mVideoView.requestFocus();
                        return;
                    }
                    current = path;
                    mVideoView.setVideoPath(getDataSource(path));

                    mVideoView.start();
                    mVideoView.requestFocus();

                }
            } catch (Exception e) {
                Log.e(TAG, "error: " + e.getMessage(), e);
                if (mVideoView != null) {
                    mVideoView.stopPlayback();
                }
            }
        }

        private String getDataSource(String path) throws IOException {
            if (!URLUtil.isNetworkUrl(path)) {
                return path;
            } else {
                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");
                temp.deleteOnExit();
                String tempPath = temp.getAbsolutePath();
                FileOutputStream out = new FileOutputStream(temp);
                byte buf[] = new byte[256];
                do {
                    int numread = stream.read(buf);
                    if (numread <= 0)
                        break;
                    out.write(buf, 0, numread);
                } while (true);
                try {
                    stream.close();
                } catch (IOException ex) {
                    Log.e(TAG, "error: " + ex.getMessage(), ex);
                }

                return tempPath;
            }
        }         

0 个答案:

没有答案