android VideoView can't play this video error - android

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.

Related

How to add subtitles to VideoView?

I am wondering how to successfully add subtitles to VideoView. I do not know how to do it. I do not know how to use addSubtitleSource, addTimedTextSource methods properly and if they are correct.
Android documentation says that addTimedTextSource is used only with MediaPlayer (which VideoView isn't) but there are some information about track i.e. getTrackInfo and others. I do not know where to start. There are some solutions in the internet but I do not understand those code snippets and I think they do not give an easy solutions.
Does anybody can help me?
Let's have a simple VideoView with video attached. What's next?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
videoView = findViewById(R.id.video_view1);
MediaController mMedia = new MediaController(this);
mMedia.setMediaPlayer(videoView);
mMedia.setAnchorView(videoView);
videoView.setMediaController(mMedia);
String path1 = VIDEO_URL;
Uri uri = Uri.parse(path1);
videoView.setVideoURI(uri);
videoView.start();
}
EDIT:
I've added the listener to VideoView and then OnTimedTextListener to MediaPlayer mp. Is this MediaPlayer mp attached to my VideoView? Is this the correct way to create MediaPlayer which will correspond with the VideoView?
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
try {
mp.addTimedTextSource("android.resource://"+getPackageName()+"/"+ R.raw.sub,MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() {
#Override
public void onTimedText(MediaPlayer mp, TimedText text) {
text.getBounds();
text.getText();
}
});
I do not know how to use getBounds() and getText() methods.
EDIT 2:
Now I have made something like this...
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
try {
mp.addTimedTextSource("android.resource://" + getPackageName() + "/"+R.raw.sub,
MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
} catch (IOException e) {
e.printStackTrace();
}
mp.selectTrack(2);
mp.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() {
#Override
public void onTimedText(MediaPlayer mp, TimedText text) {
txtDisplay.setText(text.getText());
}
});
mp.start();
}
});
I put the "android.resource://" + getPackageName() + "/"+R.raw.sub path to the file but it gives me an error E/TaskPersister: File error accessing recents directory (directory doesn't exist?). Later I moved it to the assets folder but the result was the same. What's wrong?
I've added the mp.selectTrack(2); with index = 2 because I read somewhere that this is obligatory and for the videos getTrackInfo returns 2 in most cases.
In the end I thought that txtDisplay.setText() method will be OK. Is it?
Please tell me, is only 1 the issue here or more?
You could use ExoPlayer library for this purpose. Here is a medium post that explains how you could add an SRT subtitle to ExoPlayer tracks.

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

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.

Play video one after another

I need to play two video one after another(as a pair), the first video is as intro video and the second video is as main video, So what I actually need is that after finishing intro video the main video will start...say intro-1 & main-1, intro-2&main-2, intro-3& main3...so on.
the problem that I am getting is that i cnt move to intro video again after completing the main video.Only the main video is played again and again
Here is that code:
videoView.setVideoPath(introPath);
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(final MediaPlayer mp) {
videoView.setVideoPath(mainPath);
MediaController mc = new MediaController(DisplayVideo.this);
videoView.requestFocus();
videoView.start();
}
}
any help will really be appreciated Thanks
Create a list of the video paths, something like:
List<String> videoPathes = new ArrayList<String>();
videoPathes.add(path1);
videoPathes.add(path2);
// etc..
and some index, for example:
int i = 0;
In the onCompletionListener, set the next path this way:
public void onCompletion(final MediaPlayer mp) {
i = (i + 1) % videoPathes.size();
videoView.setVideoPath(videoPathes.get(i));
// the rest ...
}

VideoView not playing on some devices

I have fragment which contains CameraPreview as background and in one of the corners i have 100x100dp video view which must play mp4 h.264 format video,
but for some reason some of the devices play other won't the problem that no error thrown it just not showing .
This is the function to prepare the video.
private void setupVideoView(){
String path = "android.resource://"+ getActivity().getPackageName()+"/"+R.raw.model_2;
Uri uri = Uri.parse(path);
mVideoView.setVideoURI(uri);
mVideoView.seekTo(1);
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
}
then after couple seconds i need to start the video, then i call
if(mVideoView != null){
mVideoView.start();
}
i had read about which codecs and format android accept and my video is perfect suit this requirements as i said above its mp4 h.264 format , any idea?

Categories

Resources