Streaming video with videoview - android

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.

Related

Videoview not working in samsung device only for some video

I used Videoview in my app. Its working fine in all device. But for there is some link, like given one, which doesn't work in Samsung device only.
For other all device even with very low configuration, it's working fine.
Please don't share videoview example etc. Please give a logical answer.
Video Link: Video Link Here
private void play(){
pd = new ProgressDialog(activity);
pd.setMessage(getString(R.string.buffering_video));
pd.show();
Uri uri = Uri.parse(video_url);
videoview.setVideoURI(uri);
videoview.requestFocus();
videoview.start();
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
//close the progress dialog when buffering is done
pd.dismiss();
}
});
}

android VideoView can't play this video error

I was trying to play a video by streaming it from internet.
videoView = findViewById(R.id.videoView);
bufferProgress = findViewById(R.id.bufferProgress);
videoURL = "https://firebasestorage.googleapis.com/v0/b/sample2-e4836.appspot.com/o/Video%2FFourthVideo.mp4?alt=media&token=6106b76c-be43-4fb6-b29f-64df699bec3d";
Uri uri = Uri.parse(videoURL);
videoView.setVideoURI(uri);
videoView.requestFocus();
bufferProgress.setVisibility(View.VISIBLE);
videoView.start();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
mediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mediaPlayer, int i, int i1) {
bufferProgress.setVisibility(View.INVISIBLE);
mediaPlayer.start();
}
});
}
});
this code I'm using right now in my onCreate.
At first it was working good.
But then I just changed my videoURL with diffrent link.
& since then I'm getting this error, not just for one I tried many different links but the problem was same.
But for first link again there is no problem for playing video.
after that I revoked those links of videos. and now I'm getting an error that Unfortunately this app has stopped!
note:
other activities of my app works fine.
try any links as your wish.

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

Android video streaming from url

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

HTML5 Video Android VideoView

I am trying to play a video that is passed from a WebView in a VideoView. It works, except VideoView does not want to read it. I keep getting the error:
"Sorry, this video cannot be played."
Here is the code for the VideoView:
public class VideoHandler extends Activity {
WebView myWebView;
VideoView myVideoView;
WebChromeClient chromeClient;
WebViewClient wvClient;
Intent in;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player);
myVideoView = (VideoView) findViewById(R.id.videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(myVideoView);
String video = (MNWVMainPage.myWebView.getUrl());
myVideoView.setMediaController(mediaController);
myVideoView.setVideoPath(video);
myVideoView.start();
myVideoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
setContentView(R.layout.mnwv_main);
}
});
}
}
Why wont this load the video?
From just reading from your code, I can see no error.
Can you check a few things:
Does the video align to the format list here?
Have you open the camera/ another VideoView? Even if you release them, the buffer seems to need some time to be actually released.

Categories

Resources