can anyone explain this code of media controller? - android

can anyone explain this code of media controller?
videoview = (VideoView) findViewById(R.id.player);
videoview.setVideoURI(Uri.parse("android.resource://"+ getPackageName()+ "/" +R.raw.sample));
videoview.start();
MediaController controller = new MediaController(this);
controller.setMediaPlayer(videoview);
videoview.setMediaController(controller);

The code gets a VideoView by its id, sets the uri of the file to play to an embedded resource and starts playback.
Then it attaches a MediaController to the view which renders the progress bar and some controls to play / pause the video.

Related

"Can't play this video" Error using URL with VideoView and Mediacontroller

So I'm trying to do my homework, but the teacher's given me 0 information about this. There is an example of how to play a video from a file in the res/raw folder, but there's nothing about online URLs. Please help me, I just want a simple player. I'll attach a picture detailing exactly what is up. I'll also add the code, since it's not that much and I really have no clue what could be wrong. Error says this:
W/MediaPlayer: Couldn't open http://techslides.com/...
java.io.FileNotFoundException: No content provider: http://techslides.com/demos/sample-videos/small.mp4
And this is the code:
VideoView video;
String url = "http://techslides.com/demos/sample-videos/small.mp4";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video = (VideoView) findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
mc.setAnchorView(mc);
video.setVideoPath(url);
video.setMediaController(mc);
video.start();
}
I will finally add to this that I've tried several different URLs, including some https ones and some http.
EDIT:
So, I tried fixing it and it ended up looking like this:
video = (VideoView) findViewById(R.id.videoView);
final MediaController mc = new MediaController(this);
mc.setAnchorView(mc);
video.setVideoPath(url);
video.setMediaController(mc);
video.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp){
video.start();
}
});
But it still gives me the same error when the emu is open. "Can't play this video". On the other hand, I got a bunch of new errors:
E/MediaPlayerNative: error (1, -2147483648)
E/MediaPlayer: Error (1,-2147483648)
D/VideoView: Error: 1,-2147483648
I'm not really familiarized with this technology, and the teacher hasn't given us any notions whatsoever as to what should or shouldn't be in the code for it to work. Just an example of a locally stored video playing in Android Studio with VideoView... that doesn't work when applied to online URLs.
So I've ended up fixing it myself. The problem wasn't in the code, for anyone wondering I've ended up using this simple format:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video = (VideoView) findViewById(R.id.video);
Uri uri = Uri.parse("http://techslides.com/demos/sample-videos/small.mp4");
video.setMediaController(new MediaController(this));
video.setVideoURI(uri);
video.requestFocus();
video.start();
}
The problem was the AVD itself. I had a Pixel 1 running Android 9, and that for some reason didn't work. I've installed a Nexus 5 with Oreo and it works flawlessly.
This may help you
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
// perform set on prepared listener event on video view
simpleVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// do something when video is ready to play, you want to start playing video here
}
});
Try to start video only when its fully ready to play. Since mp4 will take some time to download.. so it might be in inconsistent state when you started video.
Hope this will help.

How to play YouTube video in Android player?

I want to play YouTube videos on Android without using YouTube app. It should play with Android player. We have tried using YouTube app, but we need to play YouTube videos directly with Android player, not using YouTube application.
Is it possible? If it is, how to do it?
try this.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse("your url in rtsp format");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
Its simple just create video view then add a new media controller to it, set the video URL in the video view and start the video it will work.
Add the below Code into your MainActivity.java file.
#Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.videodisplay);
String link="http://www.youtube.com/watch?v=JSnB06um5r4";
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(link);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
}
}
You better try it on offline file to make sure that the video viewer is working fine (the video is compatible with device) then play it online

display time on android media controller

Am I missing something? Once I play a video using videoview, I cannot see the current time of the video on the media controller. (Running app on a ICS device, there is no time; however on a Honeycomb device there is time)
Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.videoView);
// Use a media controller so that you can scroll the video contents
// and also to pause, start the video.
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView
.setVideoURI(Uri
.parse("rtsp://v6.cache5.c.youtube.com/CiILENy73wIaGQmCZld_oqDeJhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"));
videoView.start();
}
Actually I noticed the time is there but it's so dim and I cannot see it. Is there any way that I can change the theme of the media controller?
The answer is kind of general to all of the controllers such a mediacontroller, dialog, toast, etc.
you can use ContextThemeWrapper:
instead of:
MediaController mediaController = new MediaController(this);
you can use:
MediaController mediaController = new MediaController(new ContextThemeWrapper(this,R.style.CustomTheme));

How to Play Video from URL in Android?

I wrote code for play video from URL. But I am getting UNABLE TO PLAY VIDEO message.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView = (VideoView) findViewById(R.id.video);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse("http://www.youtube.com/watch?v=qvtCk1wZ7LM&feature=player_detailpage");
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.start();
}
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/video"></VideoView>
You could simple fire an Intent to play the video by another app.
Here, Youtube app will detect a Youtube Video, and will prompt the dialog to play the video on your behalf.
String url = "http://www.youtube.com/watch?v=qvtCk1wZ7LM";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
But if you really want to integrate a video player inside your app, I strongly recommand you to take a look at the Youtube API.
Its simple just create video view then add a new media controller to it, set the video URL in the video view and start the video it will work.
Add the below Code into your MainActivity.java file.
#Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.videodisplay);
String link="http://s1133.photobucket.com/albums/m590/Anniebabycupcakez/?action=view& current=1376992942447_242.mp4";
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(link);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
}
}
You better try it on offline file to make sure that the video viewer is working fine (the video is compatible with the device) then play it from YouTube online.
Here is a simple and easy way to play the video.
xml file...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="2px" android:paddingRight="2px"
android:paddingTop="2px" android:paddingBottom="2px"
android:layout_width="fill_parent" android:orientation="vertical">
<VideoView android:layout_height="fill_parent"
android:layout_width="fill_parent" android:id="#+id/VideoView"></VideoView>
</LinearLayout>
java file.....
public class VideoPlayerController extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse("mp4 video link");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
}
some file format like youtube video file format cant open in video view.
videoview only support system file format can opend it like mp4 . 3gp . other...
for open fly video format you must use surfeview and create class that can handl this video type
and for get video from link use inputstream (dont use string)
other easy way to show youtube video is just use Intent.View,url(your video uri)
https://developer.android.com/guide/appendix/media-formats.html
Use Exo-Player , it is better choice to play videos and having lot of functionallity --Follow Exo Player Docs

Help with capturing pause/resume with VideoView with MediaController after video starts

...
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.

Categories

Resources