Android play a video with controller - android

I'm using the following code to play a .mp4 video on my android device. I have 3 problems,
using this code when I press the back button on the device the sound would still continoue to play
It doesn't play the video, only plays the sound!
I don't know how to get controller for the video so the user can stop the video, go back or forth on the video.
VideoView videoHolder = new VideoView(this);
videoHolder.setMediaController(new MediaController(this));
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaPlayer mediaPlayer = new MediaPlayer();
Uri myUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.video);
videoHolder.setVideoURI(myUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
videoHolder.requestFocus();
videoHolder.start();

1) Override onBackPressed in your activity:
#Override
public void onBackPressed() {
mediaPlayer.stopPlayback();
super.onBackPressed();
}
2) Is it a valid video format? Are you using emulator or a real device? If you use an emulator, try a real device instead. I recall similar issues before.
But I also notice: did you put your video in the drawable folder?
R.drawable.video
I don't think that will work, you cannot play videos from there. Use the resources/raw folder instead. btw: Audio can be played from assets folder directly, while video cannot; video can only played from the raw folder or if you copy it to the apps files folder (getFilesDir()) - or sdcard of course.
3) Why are you using a VideoView AND a MediaPlayer, instead of just a VideoView? You should get rid of the mediaPlayer and just use the VideoView. This line
videoHolder.setMediaController(new MediaController(this));
looks correct and sets the media controller, with which the user can seek in the video.
Programatically you can also use seekTo() to go back and forth in a video.

VideoView videoView =(VideoView)findViewById(R.id.vv01);
//specify the location of media file
Uri myUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.login_footage);
//Setting MediaController and URI, then starting the videoView
videoView.setVideoURI(myUri);
videoView.requestFocus();
videoView.start();
This worked for me

Related

Why i can not play gif animation in android videoView?

I'm beginner in android and want to play gif animation in android video view so my gif file in this image show:
and write this code for play that:
VideoView videoView = (VideoView)findViewById(R.id.video_view);
videoView.setVideoPath("raw/newline.gif");
videoView.start();
but when run that app,i get error,can not play this video,why?
Try this code:
Uri uri= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.newline);
videoView.setVideoURI(uri);
videoView.start();
Your mistake is that you can't just specify a path to resource file. In this case you have to use Uri.
But AFAIK VideoView can't play gif. So check out this and this solutions.

Cannot play video, android

I have used an mp4 video with a 400x760 resolution at 30fps and integrated it into an app using VideoView and MediaController
MediaController mediac;
VideoView video1;
mediac = new MediaController(this);
mediac.setAnchorView(video1);
video1 = (VideoView) findViewById(R.id.videoView1);
video1.setMediaController(mediac);
video1.setVideoPath("android.resource://video.test/raw/vid");
It plays fine on my ZTE Blade but on an Evo, GS2 and a Galaxy S , it says soryy, cannot play this video
Try with
video_view.setVideoURI(Uri.parse(path));
Sometimes passing directly as a string path doesn't work on some devices. The code which is working fine for me :
path = Environment.getExternalStorageDirectory() + "/file_name";
// Add controls to a MediaPlayer like play, pause.
MediaController mc = new MediaController(this);
video_view.setMediaController(mc);
// Set the path of Video or URI.
video_view.setVideoURI(Uri.parse(path));
// Set the focus.
video_view.requestFocus();
video_view.start();

Add and Access a video file in android project

I just wanted to run a predefined video file when a button is clicked. I have added this video file into res/raw folder in myVideApp project. Now I need to pass this path to videoView.setVideoPath() in order to play the video.
How can I access the stored video file's actual path in android. Note: I don't want to open the file. just want the actual location of the file to pass to video view.
I tried "path = this.getResources().getString(R.raw.bbc);" but its not working since it gives the path relative to the current project. but videoview needs absolute path.
Thank you,
Regards,
Robo.
Following Snippet will help you.
getWindow().setFormat(PixelFormat.TRANSLUCENT);
videoView = new VideoView(this);
videoView.setMediaController(new MediaController(this));
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/" + R.raw.bbc); //Don't put extension
videoView.requestFocus();
setContentView(videoView);
videoView.start();
Following are steps to access video file and to play video
Get Video Control
create media controller
Get Video path from local resorce
Set media controller to video
set path of video in video control
Set Focus
Start Video
.
VideoView videoView = (VideoView)findViewById(R.id.videoViewGuide);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri = Uri.parse("android.resource:// " + getPackageName() + "/" + R.raw.Guide_Video_01);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();

android MediaPlayer for video. problems starting

how do i reference a video and play it using mediaPlayer and VideoViewer, when the file is in my raw folder
iv tried
videoview.setVideoPath("android.raw://com.example.movievp8");
as well as
MediaPlayer video1 = MediaPlayer.create(this, R.raw.movievp8);
once I have referenced it should i just hit video1.start();
also does anyone know where i could get like a full sample code on how to use media player for videos, just the basics.
Your code
MediaPlayer video1 = MediaPlayer.create(this, R.raw.movievp8);
video1.start();
should works (but you will only have audio).
One way to do it is using a VideoView (where your video will be displayed) and a MediaController (to have some predefined buttons such as Play, pause, stop, etc.)
VideoView vv = (VideoView) findViewById(R.id.videoview);
MediaController controller = new MediaController(vv.getContext());
vv.setMediaController(controller);
vv.setVideoURI(Uri.parse("android.resource://" + context.getPackageName() + "/" + videoID));
vv.start();
Remember that the simulator doesn't support video. You'll need to use a real device for testing video.

To play video files using MediaPlayer class in android

I want to play video files in my application which are part of my application.
for that i create 'raw' folder in 'res' and write following code in my activity.
MediaPlayer mp = MediaPlayer.create(VideoPlayer.this,R.raw.jeevrangala);
mp.start();
now i am testing it on emulator, but it does not displaying any thing.
is any one have solution to play video files in raw folder. please let me know.
Initial i was trying to play video files from raw folder. but cant run it. so i use another way to do this
VideoView video=(VideoView) findViewById(R.id.videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
//Uri uri = Uri.parse("android.resource://play.vedio/"+R.raw.dobeernotdrugs);
video.setKeepScreenOn(true);
video.setVideoPath("android.resource://one.two/raw/"+resource);
video.start();
video.requestFocus();
resource is file name which you want to play and one.two is package name your path may as like
"android.resource://package_name/raw/file_name"
You can use this code to run video on emulator.. for me it is working
VideoView videoHolder = (VideoView) findViewById(R.id.video_view);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.samplevideo);
videoHolder.setVideoURI(video);
videoHolder.start();
And please turn on your graphics acceleration for the emulator..

Categories

Resources