I have an activity that has a button, when I click this button another activity opens and plays a video defined by me from sdcard (or phone).
The problem: I want to change which video will play always when I wish.
Example: first I pre-define to play a video about Mario. Later, I want that the same button plays a video about Luigi, then I do some method that allow me to make this trade. Or any user that use the app.
Anybody could help me how can I do this?
Here's the basic code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video_player = (VideoView) findViewById(R.id.video_frame);
media_Controller = new MediaController(this);
dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;
int width = dm.widthPixels;
video_player.setMinimumWidth(width);
video_player.setMinimumHeight(height);
video_player.setMediaController(media_Controller);
video_player.setVideoPath("/sdcard/videoplay.mp4");
video_player.start();
}
In button click dynamatically change video url as per your wish store string url.
//first activity
Intent intent = new Intent(getActivity(), VideoFullScreenActivity.class);
intent.putExtra("URL", url);
startActivity(intent);
//video activity
if (intent.hasExtra("URL")) {
urlVideo = intent.getStringExtra("URL");
}
video = (VideoView) findViewById(R.id.videoView);
media = new CustomMediaController(this);
media.setAnchorView(video);
video.setMediaController(media);
video.setVideoURI(Uri.parse(urlVideo));
video.requestFocus();
video.seekTo(current);
video.start();
Related
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
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);
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.
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();
}
});
...
mVideoView = (VideoView) findViewById(R.id.video);
mVideoView.setKeepScreenOn(true);
final Intent intent = getIntent();
mHeadline = intent.getStringExtra("headline");
String liveVideoUrl = intent.getStringExtra("live_video_url");
mVideoView.setVideoPath(liveVideoUrl);
mMediaController = new MediaController(this);
mVideoView.setMediaController(mMediaController);
mVideoView.start();
...
I need to capture when the user presses the pause/play buttons. The VideoView encapsulates Media Player hence how do I capture the play/pause events? Thanks.
Create a class that extends VideoView and use this class in your layout which is assigned to mVideoView.