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
Related
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
i have issues in android video playing. am geting videos from server and set to videoview but videos is playing in backgroung am not able to see that videos ,please refer my code what i have tried.
XML FILE
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<VideoView
android:id="#+id/eventDetailsPage_videosView"
android:layout_width="160dp"
android:layout_height="100dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#drawable/ic_eventvideo_image" />
</LinearLayout>
Java Class
Uri uri=Uri.parse(mEventDetailsUtil1.getAttachmentsUrls().get(0).getAttach_url());
VideoView mVideosView = (VideoView) findViewById(R.id.eventDetailsPage_videosView);
MediaController mc = new MediaController(this);
mc.setAnchorView(mVideosView);
mc.setMediaPlayer(mVideosView);
mVideosView.setMediaController(mc);
mVideosView.setVideoURI(uri);
mVideosView.requestFocus();
mVideosView.start();
the video is playing when opening the activity , but i don't want like that i need when i click on VideoView i need to open video full screen.
Try this :
VideoView mVideoview;
mVideoview = (VideoView) findViewById(R.id.VideoView);
mPlayServer = (Button) findViewById(R.id.mServerbutton);
mPlayServer.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playVideo(SERVER_VIDEO_URL);
}
});
private void playVideo(SERVER_VIDEO_URL)
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURL
mVideo = Uri.parse(SERVER_VIDEO_URL);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
mVideoview.requestFocus();
mVideoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
// Called when video is completed
public void onCompletion(MediaPlayer mp) {
}
});
}
To show in fullscreen use video view in new xml file with height and width as fill parent and start new activity with Intent.
Hope this helps.
Android does not support the resizing of videos, you may want to look into a Third Party library that you can utilize. By default android does not have this. I got the same problem while applying controls to video player.
I use following code to play video from a url. It works fine except that It first downloads video and then plays that video
Uri uri = Uri.parse(URL);
video.setVideoURI(uri);
video.start();
But I want to stream the live video instead of downloading it and then play
I have work on YouTube Video streaming and that was without downloading video.
This will only work if you will play YouTube video .
You have to use YouTube API and using that you can easily manage your task.
You have to enable YouTube service and using Developer Key you can access YouTube API.
How to use YouTube API ? You can Find Here Sample code for same.
Out of that sample example i have use
PlayerViewDemoActivity.java & YouTubeFailureRecoveryActivity.java and that XMl view as per the needs.
Play Video
public class PlayerViewDemoActivity extends YouTubeFailureRecoveryActivity
{
String youtube_id = "_UWXqFBF86U";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.playerview_demo);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view); // Replace your developer key
youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored)
{
if (!wasRestored)
{
player.cueVideo(youtube_id);
}
}
#Override
protected YouTubePlayer.Provider getYouTubePlayerProvider()
{
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
}
Hope this will be help you,Let me know if you have any query.
There are some requirements for streaming to work.
The file might not be encoded "correctly"
http://developer.android.com/guide/appendix/media-formats.html
For video content that is streamed over HTTP or RTSP, there are additional requirements:
For 3GPP and MPEG-4 containers, the moov atom must precede any mdat atoms, but must succeed the ftyp atom.
For 3GPP, MPEG-4, and WebM containers, audio and video samples corresponding to the same time offset may be no more than 500 KB apart. To minimize this audio/video drift, consider interleaving audio and video in smaller chunk sizes.
You might be on an older version of Android that doesn't support it
HTTP progressive streaming was only added in 2.2, HTTPS only supported 3.0+, Live streaming is only supported in even later versions.
Use Like this:
Uri uri = Uri.parse(URL); //Declare your url here.
VideoView mVideoView = (VideoView)findViewById(R.id.videoview)
mVideoView.setMediaController(new MediaController(this));
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
Another Method:
String LINK = "type_here_the_link";
VideoView mVideoView = (VideoView) findViewById(R.id.videoview);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse(LINK);
mVideoView.setMediaController(mc);
mVideoView.setVideoURI(video);
mVideoView.start();
If you are getting this error Couldn't open file on client side, trying server side Error in Android. and also Refer this.
Hope this will give you some solution.
For showing buffering you have to put progressbar of ProgressDialog ,... here i post progressDialog for buffering ...
pDialog = new ProgressDialog(this);
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(mVideoView);
Uri videoUri = Uri.parse(videoUrl);
mVideoView.setMediaController(mediacontroller);
mVideoView.setVideoURI(videoUri);
} catch (Exception e) {
e.printStackTrace();
}
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
finish();
}
});
My code below to streaming video:
VideoView vv = (VideoView)this.findViewById(R.id.screen_video);
Uri uri = Uri.parse(URL);
vv.setVideoURI(uri);
vv.start();
It works.
But if the URL video's format not support by android phone or pad.
It show a dialog, and not show the screen.
But it still streaming with black screen.
I want to get the error message, and access as an exception.
But I don't know how to get it?
Another problem is that the streaming may crash cause by low speed wifi.
How to check it to wait while low speed wifi?
try this code,it work,
public class PlayVideo extends Activity
{
private String videoPath ="url";
private static ProgressDialog progressDialog;
String videourl;
VideoView videoView ;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_video);
videoView = (VideoView) findViewById(R.id.videoView);
progressDialog = ProgressDialog.show(PlayVideo.this, "", "Buffering video...", true);
progressDialog.setCancelable(true);
PlayVideo();
}
private void PlayVideo()
{
try
{
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(PlayVideo.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videoPath );
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
progressDialog.dismiss();
videoView.start();
}
});
}
catch(Exception e)
{
progressDialog.dismiss();
System.out.println("Video Play Error :"+e.toString());
finish();
}
}
}
It depends on which Android Version you are developing your application on. There are certain devices which do not support running .mp4 file. Go through Android Media support for more information. Check if you can play any .3gp files or not.
You shouldn't play the video instantly. Add OnPrepared listener to the video view and start the video playing after it. With MediaPlayer you could keep track of the buffering state and stop the video for a while when its not yet downloaded. Please have a look at this guide.
Try Intent to avoid that error. or put try catch in your block.
VideoView can only Stream 3gp videos but i recommend this code to stream your video
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.main);
String videourl = "http://something.com/blah.mp4";
Uri uri = Uri.parse(videourl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);
}
Or Click here to watch Android Video Streaming Tutorial.
I am newbie in Android, and I tried a lot with my code to stream video in emulator, but I am getting exception "Sorry The application Hellovideodemo(...) has stopped unexpectly.
Here is the code:
public class HelloVideoDemo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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("http://dev2010.excoflare.com/zencart/abhishek/BlackBerry_Social_Meeting/av/testing.mp4");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
}
Any help will really be appreciated. Thanks.
Have you declared the Internet permission as a child of the Android Manifest?
<uses-permission android:name="android.permission.INTERNET" />
setcontentview that function should have r.id.videoview not main try i guess it should work