Playing sequential videos from array in Android Studio - android

I am trying to play a series of videos in an array in Android, however when the below code runs only the last element / video of the array plays.
How can I loop through the array and play the videos one after the other?
I get the feeling that the continuation of the loop happens immediately after the videoView.start() command so only the last one plays.
Here is an approximation of my code...
VideoView videoView = (VideoView) findViewById(R.id.videoView);
String file_location = "path/to/my/files/"; // external storage
String filepaths[] = {"1_A.mp4", "1_B.mp4"}; // array could have many more elements
for(String filepath: filepaths){
String path = file_location + filepath;
videoView.setVideoPath(path);
videoView.start();
}
I have tried adding the setOnCompletionListener and putting the continue inside the onCompletion but the error is "continue outside of loop"
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
continue;
}
});
How can I play each video sequentially with as little / no gap in between?

Don't use loop because you are supposed to set the path of the next video only after previous one finished so instead of loop create a field called currentPlayingIndex make it increment after each video finishes and then set path from that ....
Like this
private int currentPlayingIndex; // Keep this as gloabal variable
VideoView videoView = (VideoView) findViewById(R.id.videoView);
String file_location = "path/to/my/files/"; // external storage
String filepaths[] = {"1_A.mp4", "1_B.mp4"}; // array could have many more elements
String path = file_location + filepaths[currentPlayingIndex];
videoView.setVideoPath(path);
videoView.start();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
currentPlayingIndex++; //Increment index here
if(currentPlayingIndex < filepaths.length)
{
String newPath = file_location + filepaths[currentPlayingIndex];
videoView.setVideoPath(newPath);
videoView.start();
}else {
//Add logic here when all videos are played
}
}
});
Note: I am not sure how long it will take to switch between videos ...

The first: You need to play *.mp4 in the array of index 0.
Then when the video play complet,use the player play the *.mp4 in the array of index next.
My English expression may not be very good, I hope you can understand

Related

can't open video by videocapture in android studio

I'm trying use videocaptur to open video ,but without success.
I tried different video avi codec.
public void OnBtnStart(View view) {
String videopath = "android.resource://com.example.msi.mhltryagain/"+R.raw.testmjpeg;
VideoView videoview = (android.widget.VideoView) findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://com.example.msi.mhltryagain/"+R.raw.test);
videoview.setVideoURI(uri);
videoview.start();
VideoCapture cap = new VideoCapture(videopath);
boolean capGrab = cap.grab();
if (cap.isOpened()){
Toast.makeText(this,"is opened",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"isn't opened"+capGrab,Toast.LENGTH_SHORT).show();
}
}
The video path is certainly good because it correctly plays the test video. When i press the button is displayed "isnt'opened false". Why video can't by opened?

how to play an audio file based on a string variable on Android

so I'm building an interface on Android, where the user enters a word and then IF the word exists in the databank, then a button appears and when the user clicks on it "OnClick= buttonClicked" - the sound is played.
There are lots of examples that deal with a specific string names as parameters for the MediaPlayer, but I haven't seen any yet that deals with a string variable instead.
Here is what I mean:
If you want to play this specific audio that plays an airplane sound. You would create a method that handles the button click like that:
//play airplance.mp3 inside of res/raw:
public void buttonClicked(View view) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.airplane);
mp.start();
}
that works.
now, if instead of specifying the String name, i'd like to use a String variable myInput:
EditText editText = (EditText) findViewById(R.id.input);
String myInput = editText.getText().toString();
how should I define it in the media player? cause the following doesn't work:
public void buttonClicked(View view) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.myInput);
mp.start();
}
I also tried by using getResources().getIdentifier():
public void buttonClicked(View view) {
int resID=getResources().getIdentifier(myInput, "raw", this.getPackageName());
MediaPlayer mp = MediaPlayer.create(this, resID);
mp.start();
}
but also here the app crashes!
anybody can help? would very much appreciate that!
After long struggle I finally found the answer.
If myInput is the variable, then it is possible to use a String of the path , parse it to Uri and finally pass it into create(). I also used try-catch because I noticed that if the file is not found in the folder, then the app crashes. Here is the solution that worked for me:
String uriPath = "android.resource://" + getPackageName() + "/raw/" + myInput;
Uri uri = Uri.parse(uriPath);
mp = MediaPlayer.create(this, uri);
try {
mp.start();
}
catch (Exception e) {}

Android VideoView won't Play

So I'm trying to play a basic avi video in android, it seems to run fine on Windows Media Player, VLC, etc. so it doesn't look like it's requiring any complicated codecs. I have a video view in my app and that's it, and I have my video in my resources directory under:
res/raw/my_video.avi
This is the code I'm using to load my video:
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.my_video);
videoView.setVideoURI(video);
videoView.start();
And it does not work. I get a popup saying "Can't play this video" along with a logcat message:
03-10 01:42:12.102: E/(185): Failed to open file 'android.resource://com.securespaces.android.bootstrap/2130968576'. (No such file or directory)
03-10 01:42:12.102: E/MediaPlayer(9737): error (1, -2147483648)
03-10 01:42:12.142: E/MediaPlayer(9737): Error (1,-2147483648)
I'm running this on a Nexus 5 running 4.4.2 stock by the way. I'm following instructions that I found in other stack over flow questions here: How to play videos in android from assets folder or raw folder? with some minor tweaks so that I am using a VideoView that I grab from a layout file.
I'm really stumped as to why this isn't working. I've browsed through a few questions on this subject, but this feels like something that should be a duplicate. I'm running this on a Nexus 5 running 4.4.2 stock by the way.
To clarify the question, I'm wondering what I am doing wrong, or is there an alternative for just playing a simple AVI video?
Write your package name statically and check whether video plays or not.
Try out as below:
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
Uri video = Uri.parse("android.resource://com.securespaces.android.bootstrap/" + R.raw.my_video);
videoView.setVideoURI(video);
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
Implement this :
public static void getVideoFromRaw(String rawPath) {
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURL
Uri mVideo = Uri.parse(rawPath);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
mVideoview.requestFocus();
mVideoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
}
});
}
Thanks

best way to use video viewer. and play videos

Is there any way I can play a video that has been stored in the raw file, without the use of the uri. If not how do i properly set the uri. Let's say i have file named movie and I want to play it with a videoviewer. How would i do this? Also would i have to write the data to the sd card. or can i just play it from the raw.
Why cant I just do
uri uri.parse("android.resources://"+this.getApplicationContext().getPackageName()+movie);
This is what I have it as now if you could can you point out the errors to me so i know and i wont have to have this problem any more
VideoView videoview = (VideoView) findViewById(R.id.videoView1);
videoview.setKeepScreenOn(true);
videoview.setVideoPath("android.raw://com.example.movievp8");
MediaController controller = new MediaController(videoview.getContext());
videoview.setMediaController(controller);
videoview.start();
videoview.requestFocus();
Here is something that works for me:
videoResource = R.raw.some_video;
String uri = String.format("android.resource://%s/%d", context.getPackageName(), videoResource);
animationCanvas = (VideoView) ui.findViewById(R.id.animation_canvas);
animationCanvas.setVisibility(View.VISIBLE);
animationCanvas.setVideoURI(Uri.parse(uri));
animationCanvas.setOnCompletionListener(this);
animationCanvas.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
animationCanvas.start();
}
});

How to assign utube video url to android media player

I want that when i will pass the utube url to media player it will automatically load the video and play in it.
Example: http://www.youtube.com/watch?v=WAG8e_53le4
This type of url i want to play in android media player
If you want to play YouTube video in VideoView, you need rtsp url from youtube.
You can get the rtsp from Youtube Gdata feed. Although I find it quite strange that sometime the rtsp isn't available in the feed.
This works for me. It starts when the activity is created.
progressDialog = ProgressDialog.show(StevenBActivity.this, "", "Buffering video...", true);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
//video "DON'T DO IT" 1,315 views
video_url = "rtsp://v2.cache4.c.youtube.com/CjgLENy73wIaLwlPMIWk2hSzNRMYJCAkFEIJbXYtZ29vZ2xlSARSB3Jlc3VsdHNg6KnB9MbH8sVODA==/0/0/0/video.3gp";
//http://www.youtube.com/watch?v=NJNzBQvJpUI&list=UUy_BjNhdQh9-bfRuzuk53pg&index=9&feature=plcp
try {
final VideoView videoView =(VideoView)findViewById(R.id.videoView1);
mediaController = new MediaController(StevenBActivity.this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse(video_url);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
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.getMessage());
}
}

Categories

Resources