I am creating by myself a controller for a VideoView (I know it's easier to use a Controller, but it's a school homework), so the app appearance is the following (a VideoView with a SeekBar and 3 Buttons)
In the onCreate method I've written this code
VideoView vwVideo = (VideoView) findViewById(R.id.videoView);
vwVideo.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.baby);
SeekBar skbPosition = (SeekBar) findViewById(R.id.seekBarVideo);
skbPosition.setMax(vwVideo.getDuration());
but getDuration() returns -1, maybe because the video hasn't been loaded yet (*.mp4, 4.47 MB, 00:01:44), so I decided to set the maximum value of the SeekBar when the button "Play" is clicked but I don't know if this is the best way to solve the problem.
Is there a better solution?
If you want to get the duration of the video you can do this by using MediaMetadataRetriever.
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//use setDataSource() functions to set your data source
retriever.setDataSource(context, Uri.fromFile(videoFile));
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInMillisec = Long.parseLong(time);
Update me if any problem.
Related
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
i'm making and android application that stream a video from external source and display it in a videoview in my activity.
The stream working fine, but i'm unable to save a frame on my sd card. this is my code where vv is videoview:
int currentPosition = vv.getCurrentPosition(); //in millisecond
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(viewSource);
Bitmap bmFrame = mediaMetadataRetriever.getFrameAtTime(currentPosition * 1000); //unit in microsecond
if(bmFrame == null){
Toast.makeText(MainActivity.this,
"bmFrame == null!",
Toast.LENGTH_LONG).show();
}else{
AlertDialog.Builder myCaptureDialog =
new AlertDialog.Builder(MainActivity.this);
ImageView capturedImageView = new ImageView(MainActivity.this);
capturedImageView.setImageBitmap(bmFrame);
LayoutParams capturedImageViewLayoutParams =
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
capturedImageView.setLayoutParams(capturedImageViewLayoutParams);
myCaptureDialog.setView(capturedImageView);
myCaptureDialog.show();
this is my video test url:
public String viewSource = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov";
i try to set viewSourc for 'setdatasource' of the mediametadataretriver like the url to my video, but it return always null bitmap...
where is the problem?
Thankyou
This class has a lot of problems and I think one of them is the frame retrieving from a external source. I would suggest you to use this external library. You only have to add it into your project and next use the class like you do with the mediametadataretriever class.
Hope it´s useful
I have video player, video is from url. It works- I can start and stop playing video. I would like to have a possibility to move progres in MediaController. I have set the MediaController, but it doesn't work. When I use a video from file i can seek/move the video progress, but with the video from url it doesn't work.
There is my code:
videoFrame = (FrameLayout) findViewById(R.id.videoField);
mediaController = new MediaController(VideoPage.this);
videoPlayer = new VideoView(context);
videoFrame.addView(videoPlayer);
videoPlayer.setVideoURI(Uri.parse(url));
mediaController.setMediaPlayer(videoPlayer);
videoPlayer.setMediaController(mediaController);
videoPlayer.requestFocus();
videoPlayer.start();
You have to check if you can seek forward:
if(videoPlayer.canSeekForward()){
...
}
and this is just possible, when you get a duration for the videostream, but some streams don't have a duration, for example if you stream tv content or a live stream.
int duration = videoPlayer.getDuration()
How to play more than one video from one activity in my android application? I have the following code:
VideoView vd = (VideoView) findViewById(R.id.surface_view);
String movieurl = Environment.getExternalStorageDirectory() + "/content1/bio_brain.mp4";
mc = new MediaController(this);
vd.setMediaController(mc);
vd.setVideoPath(movieurl);
vd.start();
MediaPlayer can play one video at a time. It has an OnCompletionListener callback you can register to get notified when playback stops. Then you can play the second video.
I am using the following code to record audio:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivityForResult(Intent.createChooser(intent, "Select audio source"), CardList.ACTIVITY_RECDAUDIO);
When the result comes back I do the following:
Uri u = intent.getData();
String audioUri = u.getPath();
InputStream in = new BufferedInputStream(this.getContentResolver().openInputStream(u));
I would like to know how long the recording is in seconds. Is it possible to query this somehow? If all else fails I can play the clip programatically and time it, but I would prefer a more direct method if possible. Thanks!
I don't know if this is fast enough for you. But in case you don't know - you don't have to actually play it. It is enough to create MediaPlayer instance and set the path of the file and the call getDuration().
MediaPlayer mp = MediaPlayer.create(yourActivity, Uri.parse(path));
int duration = mp.getDuration();
I do this by using my input stream: is.available() on InputStream is gives me the length.