Android: Multiple Video Error
I have four videos that are loading on one activity from a url. Now the problem is that when i start the activity all videos plays at the same time. What actually i want is to stop
Solution 1:
You can simply create a buttons and on button click listeners you can add following code :
button1.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
playVideo1(your video url)
}
});
button2.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
playVideo2(your video url)
}
});
publicstaticvoidplayVideo1(String urlPath) {
VideoView mVideoview; // Added this line
mVideoView =(VideoView) findViewByid(R.yourvideoviewid);
try {
// Start the MediaControllerMediaController mediacontroller = newMediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURLUri mVideo = Uri.parse(urlPath);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
mVideoview.requestFocus();
mVideoview.setOnPreparedListener(newOnPreparedListener() {
// Close the progress bar and play the videopublicvoidonPrepared(MediaPlayer mp) {
mVideoview.start();
}
});
mVideoview.setOnCompletionListener(newOnCompletionListener() {
publicvoidonCompletion(MediaPlayer mp) {
}
});
}
Hope this answers your question.
Solution 2:
You are starting both the videos in onCreate
Method , So it is starts playing both the video at the same time .
Do as below ,
button1.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
video1.start();
}
});
button2.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
video2.start();
}
});
And also that in the code you posted the video
object is initialized 2 times .
It should be video2
for second video , As per your code
Post a Comment for "Android: Multiple Video Error"