How to Play Video from URL in Android?
I wrote code for play video from URL. But 开发者_C百科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
精彩评论