流媒体和播放mp4视频

时间:2011-02-10 13:03:32

标签: android video-streaming

我想播放mp4视频。所以我试过下面的方法

 private void playVideo() {
        try {

            final String path = s;
            Log.v(TAG, "path: " + path);
            if (path == null || path.length() == 0) {
            Toast.makeText(VideoPlay1.this, "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) {
                    MediaController mediaController = new MediaController(this);
                    mediaController.setAnchorView(mVideoView);  
                  Uri video = Uri.parse(getDataSource(path));
                    Log.e("Uri video",video.toString());
                    mVideoView.setMediaController(mediaController);
                    mVideoView.setVideoURI(video);    
                     mVideoView.setVideoPath(getDataSource(path));
                    mVideoView.setOnPreparedListener(new OnPreparedListener() {

                        public void onPrepared(MediaPlayer arg0) {
                            dialog.dismiss();
                            mVideoView.start();
                        }
                    });
                    mVideoView.requestFocus();
                return;
            }
            current = path;

            MediaController mediaController = new MediaController(this);
            Uri video = Uri.parse(getDataSource(path));

            Log.e("Uri video",video.toString());
            mVideoView.setMediaController(mediaController);
            mVideoView.setVideoURI(video);    
             mVideoView.setVideoPath(getDataSource(path));
                mVideoView.setOnPreparedListener(new OnPreparedListener() {

                    public void onPrepared(MediaPlayer arg0) {
                        dialog.dismiss();
                        mVideoView.start();
                    }
                });
            mVideoView.requestFocus();
   }
        } catch (Exception e) {
            Log.e(TAG, "error123: " + e.getMessage(), e);
            if (mVideoView != null) {
                mVideoView.stopPlayback();
            }
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
                && keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            Log.d("CDA", "onKeyDown Called");
            onBackPressed();
        }
        return super.onKeyDown(keyCode, event);
    }

    public void onBackPressed() {

        Log.d("CDA", "onBackPressed Called");
        mVideoView.stopPlayback();
        Intent setIntent = new Intent(VideoPlay1.this,VideoPage.class);
        startActivity(setIntent); 
        finish();
        return;
    }
    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[128];
            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;
        }
    }

上述工作需要更多时间才能播放视频,请告诉我是否还有其他方法。

谢谢。

最诚挚的问候。

2 个答案:

答案 0 :(得分:1)

很抱歉,将视频流式传输到移动设备需要时间。您受到连接的限制,而不是代码

答案 1 :(得分:0)

我知道已经有一个已接受的答案,但可能有其他标准可能会有所帮助,尤其是文件的编码。

默认情况下,MP4视频最后都有MOOV ATOM。 “moov原子,也称为电影原子,定义了电影的时间刻度,持续时间,显示特征,以及包含电影中每个轨道信息的子体。” (http://www.adobe.com/devnet/video/articles/mp4_movie_atom.html)。如果您在开头使用moov atom对视频进行编码(可以使用FFMPEG执行此操作),则可以改善您的流媒体体验。