Invoke the Inbuilt video player inside a layout - android

I need to invoke the inbuilt the default video player inside an inner layout and my layout
looks like some what below.
onclick of the button1 i want to invoke the video player inside the layout(in red color)
by the below code i can play the video(it opens a new view) but I want to show the video inside the layout only
Uri video = Uri.parse("/sdcard/video/123.mp4");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setDataAndType(video, "video/*");
startActivity(intent);
Any help is always appreciated,Thanks

Set up VideoView control in red area and pass sdcard path to that VideoView on Button cliked.
public class MainActivity extends Activity {
Button b1;
VideoView myVideoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView)findViewById(R.id.myvideoview);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myVideoView.setVideoPath("Your sdcard video path");
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
});
}
also add permission for read sdcard(storage) in Manifest.xml
For more information about play video from sdcard
http://android-coding.blogspot.in/2011/03/using-videoview-to-play-mp4-from-sdcard.html

Related

Videoview is not playing .3gp file

I am trying to play .3gp file from a live RTSP. following is my code,
public class MainActivity extends Activity
{
// String path = "rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp";
String path = "rtsp://217.146.95.166:554/live/ch12zqcif.3gp";
VideoView videoView = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView =(VideoView)findViewById(R.id.videoView);
videoView.setVideoURI(Uri.parse(path));
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();
}
#Override
public void onBackPressed()
{
super.onBackPressed();
if ( videoView != null )
{
videoView.pause();
videoView = null;
}
}
}
I have given INTERNET Permission in my AnroidManifest.xml file.
When I load the first path variable with Youtube one, it is working file and loading the video but the second RTSP url is not working.
What could be the problem for this..?
May be the problem is with you is your URL please verify the rtsp url. Here is the example for how to playvideo with the videoview.
Cheers

Playing Video file from SD card

I m trying to play a video from SD card, I hav PUSH-ed a video into mnt/sdcard folder from DDMS view in eclipse.But the emulator screen shows blank without any error. Pls help.
public class Video extends Activity {
private MediaController mc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
videoPlayer("mnt/sdcard","bikekid",true);
}
public void videoPlayer(String path, String fileName, boolean autoplay){
//get current window information, and set format, set it up differently, if you need some special effects
getWindow().setFormat(PixelFormat.TRANSLUCENT);
//the VideoView will hold the video
VideoView videoHolder = new VideoView(this);
//MediaController is the ui control howering above the video (just like in the default youtube player).
videoHolder.setMediaController(new MediaController(this));
//assing a video file to the video holder
videoHolder.setVideoURI(Uri.parse(path+"/"+fileName));
//get focus, before playing the video.
videoHolder.requestFocus();
if(autoplay){
videoHolder.start();
}
}
}/// end class
Use
.getAbsolutePath()
instead of "mnt/sdcard"
Edit:
use this
File file = new File(Environment.getExternalStorageDirectory(),"bikekid");
Uri mUri = Uri.fromFile(fie);
videoHolder.setVideoURI(mUri);

HTML5 Video Android VideoView

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.

How to go back after playing video

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.

Returning to application after videoplayback

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();
}
});

Categories

Resources