I have an application that has a list of videos with a play button. When I click on video, a separate activity is called through intent. When the video is finished, it remains there and it doesn't go back to the original application unless I press back button. Is there any way in which the application returns back to the main activity after the video playback is finished? Here is the code for my video creation
public void onCreate(Bundle onSavedInsance) {
super.onCreate(onSavedInsance);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.videoview);
Intent i = getIntent();
Bundle extras = i.getExtras();
filename = extras.getString("videofilename");
mVideoView = (VideoView) findViewById(R.id.VideoView);
mc = new MediaController(this);
mVideoView.setMediaController(mc);
mc.setAnchorView(mVideoView);
mVideoView.setMediaController(mc);
mVideoView.setVideoPath(filename);
//mVideoView.setVideoPath(video.getAbsolutePath());
mVideoView.requestFocus();
mVideoView.start();
}
If you are using a VideoView, you can add a listener to determine when the video has finished playing:
mVideoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
finish();
}
});
Related
Hi All I have got a video set as a background in my app. When the app starts the video plays at the main menu and everything works great. Now when I select to go to next activity the video stop and the next activity starts and when the user is then finished with this activity and presses the back button to go to go to the main menu the video should play again, however it doesn't. Hope some one can help me with this. here is my code:
public class MainActivity extends Activity {
VideoView animation;
private MediaController mc;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = MediaPlayer.create(this, R.raw.leftbanktwo);
mp.setLooping(true);
VideoView animation = (VideoView) findViewById(R.id.imageAnimation);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.cartoon);
mc = new MediaController(this);
animation.setMediaController(mc);
animation.requestFocus();
animation.setVideoURI(uri);
animation.start();
}
This is because your onCreate() method is not called again when user back to first Activity. If you want it to work like you described put the code which starts video in onResume() method.
Also, I would recommend to check out Activity Lifecycle.
Try This
#Override
protected void onResume() {
super.onResume();
if(mp!=null){
mp.reset();
mp.start();
}
}
#Override
protected void onPause() {
super.onPause();
if(mp!=null && mp.isPlaying()){
mp.pause();
}
}
I am new to android and want to play videos one after another simultaneously so that it looks like continuous video.
I have found this link on stackoverflow very helpful. how-to-play-videos-one-after-another-simultaneously
When I am using this then though I am able to play videos one after another,
But switching from one segment to other results in pause the video for a second before playing next one. So It don't looks like continuous videos due to this.
Please help me to resolve my problem.
Here is my Code.
public class VideoActivity extends Activity{
VideoView videoView, videoView1;
MediaController mc;
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
videoView = (VideoView) findViewById(R.id.VVSimpleVideo);
setup();
videoView.setOnCompletionListener(completionListener);
}
public void setup() {
String _path = "/mnt/sdcard/Video/"+count+".mp4";
videoView.setVideoPath(_path);
videoView.start();
count++;
}
private OnCompletionListener completionListener=new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
setup();
}
};
}
Now I have tried to play by taking two instances of VideoView class.
Tried to play the first video by first player and second video by second player,
Third video from first player and fourth video from second player and so on.
But still I am not able to play the video smoothly and the same problem exist.
Here is my code with double player.
public class VideoActivity extends Activity{
VideoView videoView, videoView1;
MediaController mc;
int count = 0;
String _path;
String _path1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
videoView = (VideoView) findViewById(R.id.VVSimpleVideo);
_path = "/mnt/sdcard/Video/"+count+".mp4";
videoView.setVideoPath(_path);
videoView.start();
//setup();
videoView.setOnCompletionListener(completionListener);
videoView1 = (VideoView) findViewById(R.id.VVSimpleVideo);
videoView1.setOnCompletionListener(completionListener1);
count++;
_path1 = "/mnt/sdcard/Video/"+count+".mp4";
videoView1.setVideoPath(_path1);
}
public void setup() {
videoView.start();
count++;
_path1 = "/mnt/sdcard/Video/"+count+".mp4";
videoView1.setVideoPath(_path1);
}
public void setup1() {
videoView1.start();
count++;
_path = "/mnt/sdcard/Video/"+count+".mp4";
videoView.setVideoPath(_path);
}
private OnCompletionListener completionListener=new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
setup1();
}
};
private OnCompletionListener completionListener1=new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
setup();
}
};
}
You're not really using two VideoViews at all, you're assigning both VideoView and VideoView1 to R.id.VVSimpleVideo, so they are the same object. That means each time you hit onCompletion, it's setting itself up all over again, instead of setting up one and playing the other.
Try creating two separate VideoView objects in your layout. Have one set to VISIBLE and one set to GONE/INVISIBLE, and swap when you want to change.
I can't guarantee it will make it "seamless", though, since that's an almost impossible task. Even most desktop media players aren't truly seamless. It just depends on what tolerance you have for seams.
My code below to streaming video:
VideoView vv = (VideoView)this.findViewById(R.id.screen_video);
Uri uri = Uri.parse(URL);
vv.setVideoURI(uri);
vv.start();
It works.
But if the URL video's format not support by android phone or pad.
It show a dialog, and not show the screen.
But it still streaming with black screen.
I want to get the error message, and access as an exception.
But I don't know how to get it?
Another problem is that the streaming may crash cause by low speed wifi.
How to check it to wait while low speed wifi?
try this code,it work,
public class PlayVideo extends Activity
{
private String videoPath ="url";
private static ProgressDialog progressDialog;
String videourl;
VideoView videoView ;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_video);
videoView = (VideoView) findViewById(R.id.videoView);
progressDialog = ProgressDialog.show(PlayVideo.this, "", "Buffering video...", true);
progressDialog.setCancelable(true);
PlayVideo();
}
private void PlayVideo()
{
try
{
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(PlayVideo.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videoPath );
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
progressDialog.dismiss();
videoView.start();
}
});
}
catch(Exception e)
{
progressDialog.dismiss();
System.out.println("Video Play Error :"+e.toString());
finish();
}
}
}
It depends on which Android Version you are developing your application on. There are certain devices which do not support running .mp4 file. Go through Android Media support for more information. Check if you can play any .3gp files or not.
You shouldn't play the video instantly. Add OnPrepared listener to the video view and start the video playing after it. With MediaPlayer you could keep track of the buffering state and stop the video for a while when its not yet downloaded. Please have a look at this guide.
Try Intent to avoid that error. or put try catch in your block.
VideoView can only Stream 3gp videos but i recommend this code to stream your video
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.main);
String videourl = "http://something.com/blah.mp4";
Uri uri = Uri.parse(videourl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);
}
Or Click here to watch Android Video Streaming Tutorial.
I am trying to play a video that is passed from a WebView in a VideoView. It works, except VideoView does not want to read it. I keep getting the error:
"Sorry, this video cannot be played."
Here is the code for the VideoView:
public class VideoHandler extends Activity {
WebView myWebView;
VideoView myVideoView;
WebChromeClient chromeClient;
WebViewClient wvClient;
Intent in;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player);
myVideoView = (VideoView) findViewById(R.id.videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(myVideoView);
String video = (MNWVMainPage.myWebView.getUrl());
myVideoView.setMediaController(mediaController);
myVideoView.setVideoPath(video);
myVideoView.start();
myVideoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
setContentView(R.layout.mnwv_main);
}
});
}
}
Why wont this load the video?
From just reading from your code, I can see no error.
Can you check a few things:
Does the video align to the format list here?
Have you open the camera/ another VideoView? Even if you release them, the buffer seems to need some time to be actually released.
I have an application that makes a list of videos with play button. When I click on the play button, a separate activity is started using intent. I just want that when the video playback is finished, the activity should automatically finish itself and go back to the main Activity. Here is my code for creating videoview.
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
Intent i = getIntent();
Bundle extras = i.getExtras();
filename = extras.getString("videofilename");
mVideoView = (VideoView)findViewById(R.id.videoview);
path=filename;
if (path == "") {
Toast.makeText(
ViewVideo.this,
"no video selected,
Toast.LENGTH_LONG).show();
} else {
mVideoView.setVideoPath(path);
mc = new MediaController(this);
mVideoView.setMediaController(mc);
mVideoView.requestFocus();
mVideoView.start();
}
}
any suggestions???
Register an OnCompletionListener to the videoView, in the listener implement the call to finish().
Edit (to answer to comment):
use the method setOnCompletionListener:
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion (MediaPlayer mp) {
// your code to clean up and finish the activity...
}
});
You can set a MediaPlayer.OnCompletionListener on the VideoView using VideoView.setOnCompletionListener, you'll then be able to finish the containing activity when the video finishes playing.