I am using the below code to test on emulator and device, the result came out was, the emulator can show the MediaController just below the VideoView(default is at the bottom of the screen). However, when I tested in device, it does not show me the MediaController.
I would like to know why.
final VideoView videoView = (VideoView)findViewById(R.id.videoGuide1);
videoView.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp){
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener(){
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height){
MediaController mediaController = new MediaController(WatchVideo.this);;
videoView.setMediaController(mediaController);
mediaController.setAnchorView(videoView);
}
});
}
});
Try this It worked for me....
VideoView vv;
vv = (VideoView) findViewById(R.id.videoView);
vv.setVideoPath(filename);
vv.setMediaController(new MediaController(this));
vv.requestFocus();
vv.start();
}
}
Try to this way
MediaController controller;
Videoview videoview;
videoview=(VideoView)findViewById(R.id.videoview);
controller = new MediaController(this);
videoview.setVideoURI(Uri.parse(path));
videoview.setMediaController(controller);
controller.setMediaPlayer(videoview);
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
int duration = videoview.getDuration();
videoview.requestFocus();
videoview.start();
controller.show();
Log.d("Video view is prepared","------>");
}
});
Related
I have a videoview in my app which plays videos from my expansion file. Here is the code:
private VideoView _vv;
private static final Uri CONTENT_URI = Uri.parse("content://--PACKAGENAMEHIDDENFORSECURITY--.EHZipUriProvider");
private static final String VIDEO_NAME = "b1.mp4";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
_vv = (VideoView) findViewById(R.id.vv);
startVideoPlayback();
}
private void startVideoPlayback()
{
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(_vv);
_vv.setMediaController(mediaController);
_vv.setVideoURI(Uri.parse(CONTENT_URI + "/" + VIDEO_NAME));
_vv.start();
}
And the stacktrace:
java.lang.IllegalStateException
at android.media.MediaPlayer.prepareAsync(Native Method)
at android.widget.VideoView.openVideo(VideoView.java:335)
at android.widget.VideoView.access$2100(VideoView.java:71)
at android.widget.VideoView$7.surfaceCreated(VideoView.java:607)
at android.view.SurfaceView.updateWindow(SurfaceView.java:572)
at android.view.SurfaceView.access$000(SurfaceView.java:86)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1897)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
...
I have no idea why it does this. Can anyone help?
My layout is just a videoview.
Try this
try {
mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(url);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
});
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});
videoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
}
});
} catch (Exception e) {
}
Hi guys i'm developing a android application...
There is a listener for the "When the video is in playing"?
i know the listner
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
THIS IS A LISTENER.
i want a listener for: when the video is running... is possible?
public void PlayVideo(String dir){
final VideoView videoView =(VideoView)findViewById(R.id.videoView);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri=Uri.parse(dir);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
VideoInPlay = false;
videoView.start();
}
});
}
Thank you so much and sorry for my english!
There is a which returns a boolean value.
videoView.isPlaying();
It will return true if a video is playing
I can't get it working... Whenever I touch my video, nothing is happing... Although, I think, the MediaController should be visible...
I currently use following function to play my video:
private void playCachedVideo(final VideoView videoView, final Context c, File f)
{
//videoView.setZOrderOnTop(true);
videoView.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener()
{
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height)
{
MediaController mediaController = new MediaController(c);
mediaController.setMediaPlayer(videoView);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
}
});
}
});
videoView.setVideoURI(Uri.fromFile(f));
videoView.start();
}
The video plays fine, but I can't get any MediaController to show up...
PS: I'm showing the player in a DialogFragment
Try this:
mMediaController = new MediaController(getActivity());
replace c by getActivity().
You need to attach the controller to the VideoView and then call mediaController.show(timeInMillisec) in onPrepared() callback.
This show the controller on touch of videoview and will hide the controller after specified timeInMillisec.
public class VideoViewActivity extends Activity implements MediaPlayer.OnPreparedListener{
private VideoView mVideoView = null;
MediaController mediaController = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview_layout);
mVideoView = (VideoView) findViewById(R.id.VideoView);
mVideoView.setOnPreparedListener(this);
mediaController = new MediaController(mVideoView.getContext());
mediaController.setMediaPlayer(mVideoView);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoPath("URL");
}
#Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
mediaController.show(2000);
}
}
I am trying to build an android app that will loop a video!
The problem is that it never loops! It plays the video only once!
During debugging i realized that the "myVideoView.setOnCompletionListener" is being executed but the video doesn't play!
I also try "mp.reset()" inside the CompletionListener.
Maybe i am missing something in a different file, such as the Manifest?
Any thoughts?
Here is my code:
final VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
myVideoView.requestFocus();
myVideoView.start();
}
});
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
myVideoView.setVideoURI(Uri.parse(SrcPath));
}
});
Try onPreparedListener instead of onCompletionListener:
myVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
Hope this helps.
Try this.
myVideoView = (VideoView) findViewById(R.id.videoView1);
myVideoView.setVideoPath(video_path);
myVideoView.setMediaController(new MediaController(this));
myVideoView.start();
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
myVideoView.start();
}
});
By that after completion of your video start again...
Try this.
videoView.setVideoPath(tempFileMessageUri.toString());
ctlr = new MediaController(context);
ctlr.setMediaPlayer(videoView);
videoView.setMediaController(ctlr);
videoView.requestFocus();
videoView.start();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
videoView.start();
}
});
public class video extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView = (VideoView) findViewById(R.id.surface);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.wildlife);
Intent tostart = new Intent(Intent.ACTION_VIEW);
startActivity(tostart);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.start();
}
}
here is my code to play the video. But the video stays in my screen and it does not play. Is there any solution ?
video_player_view.xml
<?xml version="1.0" encoding="utf-8"?>
<VideoView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/VideoPlayerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Java code:
public class VideoPlayerActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player_view);
String video_file_path = ?;//give the path of your video
VideoView video_view = (VideoView) this.findViewById(R.id.VideoPlayerView);
MediaController mc = new MediaController(this);
video_view.setMediaController(mc);
video_view.setVideoPath(video_file_path);
video_view.requestFocus();
// start video
video_view.start();
video_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
endActivity();
}
});
}
public void endActivity() {
this.finish();
}
}
Why are you using this code?
Intent tostart = new Intent(Intent.ACTION_VIEW);
startActivity(tostart);
If it is not necessary then remove it video will be played with sound. Otherwise please write what you exactly want to do with video playback.