VideoView - MediaController not showing up - android

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

Related

How can Videoview display fullscreen when mediacontroller hides?

This is my activity and it's working.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playing);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent intent = getIntent();
String link = intent.getStringExtra("link");
VideoView videoview = (VideoView) findViewById(R.id.videoView2);
MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(videoview);
Log.i("Link",link);
Uri video = Uri.parse(link);
videoview.setMediaController(mediacontroller);
videoview.requestFocus();
videoview.setVideoURI(video);
videoview.start();
}
This is the screen when playing:
But when the controller hides, the video screen is not fullscreen.
How can Videoview display fullscreen when mediacontroller hides?
I think you can create your myMediaController below to triger onHide of mediaController and refresh videoview
class myMediaController extends MediaController{
private View anchorView = null; //this ref to videoview from setAnchorView(View view)
public myMediaController(Context context, boolean useFastForward) {
super(context, useFastForward);
}
#Override
public void setAnchorView(View view) {
super.setAnchorView(view);
anchorView = view;
}
#Override
public void hide() {
super.hide();
anchorView.invalidate(); //refresh with and height of video view on screen
anchorView.refreshDrawableState();
}
}

Device not display MediaController

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","------>");
}
});

How to display progress bar while MediaController loads video (live streaming) and not playing it?

Here's my activity code:
public class VideoActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView) findViewById(R.id.activity_video_videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri
.parse("http://www.ebookfrenzy.com/android_book/movie.mp4");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
}
While video is buffering and not played I want to display progress bar. How can I do it?
Set an OnPreparedListener and dismiss the dialog there:
Declare the progress dialog in your activity:
private static ProgressDialog progressDialog;
Then in onCreate method:
...
progressDialog = ProgressDialog.show(this, "", "Loading...", true);
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
progressDialog.dismiss();
videoView.start();
}
});
Check the new Lines Added should do what you intend.
public class VideoActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView) findViewById(R.id.activity_video_videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri
.parse("http://www.ebookfrenzy.com/android_book/movie.mp4");
videoView.setMediaController(mediaController);
// New Line
ProgressDialog pDialog = ProgressDialog.show(this, "", "Loading Video");
videoView.setVideoURI(video);
//New Line Added
pDialog.dismiss
videoView.start();
}
}

MediaController Play On Video Click

I have a simple app where a video loads from a url and when you click on the video, the MediaController pops up.
This works fine, but I'm wondering if there is a way I can just skip that step and have it so that clicking the video automatically triggers the 'play' function of the MediaController without having to ever see it.
These videos are short and I don't need to be able to control them other than just pressing play.
Here is my code:
private static final String MOVIE_URL ="http://mywebsite.com/videos/testvideo.mkv";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vid = (VideoView) findViewById(R.id.videoView);
Uri video = Uri.parse(MOVIE_URL);
vid.setMediaController(new MediaController(this));
vid.setVideoURI(video);
vid.seekTo(1);
vid.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View view)
{
vid.start();
vid.requestFocus();
}
}
);
}
EDIT
I needed to use onTouchListener instead of onClickListener because it's a videoview. This works perfectly!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vid = (VideoView) findViewById(R.id.videoView);
Uri video = Uri.parse(MOVIE_URL);
// vid.setMediaController(new MediaController(this));
vid.setVideoURI(video);
vid.seekTo(1);
vid.setOnTouchListener(
new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event) {
vid.start();
vid.requestFocus();
return false;
}
}
);
}
Add a PreparedListener to listen for video if prepared . When the VideoView is prepared to start the play
vid.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
vid.start();
vid.requestFocus();
}
});
vid.setVideoURI(video);
When setVideoURI, the instance of MediaPlayerwill be created and register for MediaPlayer setOnPreparedListener . So that when MediaPlayer is prepared the VideoView onPrepared listener will get called.

Android: VideoView can't play video

im trying to get a basic video player working for an android application im working on but whenever i try to play the video i get an error saying "cant play video". Im certain the video is in supported formats and ive tried converting it between several of the supported formats on top of that so i assume its an issue with my code:
public class VideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
VideoView videoView = (VideoView)findViewById(R.id.VideoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoPath("TravelAppTopic4/res/raw/canadavid.mp4");
videoView.start();
}
thanks in advance for any help
edit:im testing on both a virtual device and an S3 to the same result.
public class VideoScreen extends Activity{
private MediaController controller;
Context _ctx = VideoScreen.this;
private Thread thread;
ProgressBar progressBar = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.phone_video_screen);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
VideoView video = (VideoView) findViewById(R.id.videoView1);
controller = new MediaController(VideoScreen.this);
video.setMediaController(controller);
video.setVideoPath(_url);
video.requestFocus();
video.start();
progressBar.setVisibility(View.VISIBLE);
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
progressBar.setVisibility(View.GONE);
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
}
}
try
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw. canadavid));

Categories

Resources