HTML5 Video Android VideoView - android

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.

Related

Android VideoView not playing mp4 files

I have already seen most of the questions here but none of them helps.
Following is Streaming url it works perfectly on VLC and browser but it can't be played in android app.
Here is my code
public class VideoDemo extends Activity {
private VideoView video;
private MediaController ctlr;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
/* File clip = new File(Environment.getExternalStorageDirectory(),
"90.mp4");*/
/* if (clip.exists()) {*/
video = (VideoView) findViewById(R.id.video);
//video.setVideoPath(clip.getAbsolutePath());
video.setVideoURI(Uri.parse("http://103.50.152.102:9096/LubrizolWebPrj/service/getEnqVideo/90"));
video.requestFocus();
video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
video.start();
}
});
//}
}
}
MP4 is just a container - the video and audio stream inside it will both be encoded in different formats.
Android natively only supports certain types of formats. There is a list here:
http://developer.android.com/guide/appendix/media-formats.html
Make sure the video and audio encoding type is supported. Just because it says "mp4" doesn't automatically mean it should be playable.
Though if your format is not supported, Try using YoutubeVideoView

Videoview is not playing .3gp file

I am trying to play .3gp file from a live RTSP. following is my code,
public class MainActivity extends Activity
{
// String path = "rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp";
String path = "rtsp://217.146.95.166:554/live/ch12zqcif.3gp";
VideoView videoView = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView =(VideoView)findViewById(R.id.videoView);
videoView.setVideoURI(Uri.parse(path));
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();
}
#Override
public void onBackPressed()
{
super.onBackPressed();
if ( videoView != null )
{
videoView.pause();
videoView = null;
}
}
}
I have given INTERNET Permission in my AnroidManifest.xml file.
When I load the first path variable with Youtube one, it is working file and loading the video but the second RTSP url is not working.
What could be the problem for this..?
May be the problem is with you is your URL please verify the rtsp url. Here is the example for how to playvideo with the videoview.
Cheers

Play video one after another simultaneously smoothly [without visible switching from one to another]

I am new to android and want to play videos one after another simultaneously so that it looks like continuous video.
I have found this link on stackoverflow very helpful. how-to-play-videos-one-after-another-simultaneously
When I am using this then though I am able to play videos one after another,
But switching from one segment to other results in pause the video for a second before playing next one. So It don't looks like continuous videos due to this.
Please help me to resolve my problem.
Here is my Code.
public class VideoActivity extends Activity{
VideoView videoView, videoView1;
MediaController mc;
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
videoView = (VideoView) findViewById(R.id.VVSimpleVideo);
setup();
videoView.setOnCompletionListener(completionListener);
}
public void setup() {
String _path = "/mnt/sdcard/Video/"+count+".mp4";
videoView.setVideoPath(_path);
videoView.start();
count++;
}
private OnCompletionListener completionListener=new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
setup();
}
};
}
Now I have tried to play by taking two instances of VideoView class.
Tried to play the first video by first player and second video by second player,
Third video from first player and fourth video from second player and so on.
But still I am not able to play the video smoothly and the same problem exist.
Here is my code with double player.
public class VideoActivity extends Activity{
VideoView videoView, videoView1;
MediaController mc;
int count = 0;
String _path;
String _path1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
videoView = (VideoView) findViewById(R.id.VVSimpleVideo);
_path = "/mnt/sdcard/Video/"+count+".mp4";
videoView.setVideoPath(_path);
videoView.start();
//setup();
videoView.setOnCompletionListener(completionListener);
videoView1 = (VideoView) findViewById(R.id.VVSimpleVideo);
videoView1.setOnCompletionListener(completionListener1);
count++;
_path1 = "/mnt/sdcard/Video/"+count+".mp4";
videoView1.setVideoPath(_path1);
}
public void setup() {
videoView.start();
count++;
_path1 = "/mnt/sdcard/Video/"+count+".mp4";
videoView1.setVideoPath(_path1);
}
public void setup1() {
videoView1.start();
count++;
_path = "/mnt/sdcard/Video/"+count+".mp4";
videoView.setVideoPath(_path);
}
private OnCompletionListener completionListener=new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
setup1();
}
};
private OnCompletionListener completionListener1=new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
setup();
}
};
}
You're not really using two VideoViews at all, you're assigning both VideoView and VideoView1 to R.id.VVSimpleVideo, so they are the same object. That means each time you hit onCompletion, it's setting itself up all over again, instead of setting up one and playing the other.
Try creating two separate VideoView objects in your layout. Have one set to VISIBLE and one set to GONE/INVISIBLE, and swap when you want to change.
I can't guarantee it will make it "seamless", though, since that's an almost impossible task. Even most desktop media players aren't truly seamless. It just depends on what tolerance you have for seams.

Mute a playing video by VideoView in Android Application

I want to mute a playing Video by VideoView in my Android Application.
I couldn't find any method to do so in VideoView Class.
Any idea how to do this?
I have found a method "setVolume" in MediaPlayer Class, But I am unable to find any working code to play video by MediaPlayer class.
So I believe I can set volume 0 by this method.
Therefore I am looking for any working code to play video using MediaPlayer Class or how to control volume using VideoView Class.
Below is the code for playing video using VideoView , which I am using.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
String _path = "/mnt/sdcard/Movies/video5.mp4";
videoView.setVideoPath(_path);
videoView.requestFocus();
videoView.start();
}
If you want to get access to the MediaPlayer of a VideoView you have to call MediaPlayer.OnPreparedListener and MediaPlayer.OnCompletionListener, then you can call MediaPlayer.setVolume(0f, 0f); function to set the volume to 0.
Do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
String _path = "/mnt/sdcard/Movies/video5.mp4";
videoView.setVideoPath(_path);
videoView.setOnPreparedListener(PreparedListener);
videoView.requestFocus();
//Dont start your video here
//videoView.start();
}
MediaPlayer.OnPreparedListener PreparedListener = new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer m) {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
m.setVolume(0f, 0f);
m.setLooping(false);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
videoview.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setVolume(0, 0);
}
});
I have done this using MediaPlayer class.
I have used setVolume function of MediaPlayer class to set the volume to 0.
also I have realised that dont use AudioManager class, because using AudioManager if a set volume to 0, then it set volume to 0 for all the instance of MediaPlayer and VideoView. But if you will use setVolume() method of MediaPlayer then it will just Mute the volume of that instance only.
Also set volume to 0 is bot easy using VideoView because VideoView is a wrapper over MediaPlayer class and just allow to access few function of MediaPlayer.
Also I have read on some blog that though you can reference MediaPlayer instance using VideoView instances, but its very complex and its not recommended to do so.
Hope this would be helpful to other new readers how try to do similar things.

Streaming video with videoview

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.

Categories

Resources