streaming and playing mp4 video
i want to play a mp4 video. so i tried below method
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));
开发者_C百科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;
}
}
the above is working it takes more time to play the video so please tell me if there is another way.
Thank you.
Best Regards.
Sorry, but streaming a video to a mobile device takes time. You are limited by the connection, not the code
I know there's an accepted answer already, but there's probably additional criteria that might help, especially the encoding of the file.
An MP4 video usually by default has the MOOV ATOM at the end. "The moov atom, also referred to as the movie atom, defines the timescale, duration, display characteristics of the movie, as well as subatoms containing information for each track in the movie." (http://www.adobe.com/devnet/video/articles/mp4_movie_atom.html). If you encode your video with the moov atom at the beginning ( you can do this with FFMPEG ), your streaming experience may be improved.
精彩评论