VideoView won't play while context is set to WebView - android

Basically I'm setting up a VideoView to play music(more functionality than MediaPlayer) while I display a .gif in a webview. I'm not sure if I need multiple threads or something, but whenever I try to run both at once it either crashes, or doesn't play the music. Is it true that since my "Content View" is set to the WebView that I can't access objects(like the videoview) on my main.xml?
Code:
setContentView(R.layout.main);
musicPlayer = (VideoView)findViewById(R.id.player);
//run gif
view = new GifWebView(this, "file:///android_res/raw/kk.gif");
setContentView(view);
//start player
musicPlayer.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + musicList[0]));
musicCounter =0;
musicPlayer.setOnCompletionListener(TrackRepeater);
musicPlayer.requestFocus();
musicPlayer.start();
The player works if I don't change context to the WebView obviously, not sure what to do here. Cheers.

Please elaborate on your reasons to use a VideoView to play music.
more functionality than MediaPlayer
VideoView is essentially a wrapper for SurfaceView and MediaPlayer. The extra functionality in VideoView should be only the stuff related to playing the video on the screen, which you aren't using. What specific reasons do you have for using that over MediaPlayer?
But I think you are right that by changing the content of your activity you are breaking the VideoView. You need to figure out a way to do it without calling setContentView() multiple times. There are many possibilities here are a two:
Get a reference to the parent view of your main.xml layout and call .addView() on it to add the GifWebView instead of calling setContentView() with it.
Add the declaration of the GifWebView to your xml layout and get the reference to it the same way as you have the VideoView.

Related

Making video load faster in videoview

I play a video in a videoview from an URL...everything works fine and even the video plays
But the only problem is that the video takes almost 10 seconds to start playing which might be kind of annoying to the user
I have tried different URLs and its the same, the videos are 360p and 6sec long
Is it the default media player that is slow?
I have the stack overflow but could not find a suitable answer and ever searched for various 3 rd party videos libraries but could not find one
Even tried google's exoplayer library but the documentation is not that good in my view
Is there any solution how to overcome this problem?
my code
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String videeourl = "http://techslides.com/demos/sample-videos/small.3gp";
VideoView videoView = (FastVideoView)findViewById(R.id.video);
videoView.setMediaController(new MediaController(this));
videoView.setVideoPath(videeourl);
videoView.start();
}
}
Consider using Exoplayer. You can find the open source project here:
https://github.com/google/ExoPlayer
It uses Dynamic Adaptive Streaming over HTTP (DASH),breaks long content into HTTP segments.
You can follow this tutorial step-by-step to integrate ExoPlayer (ExoPlayer is the video player running in the Android YouTube app.) to your app, it's not that complicated as you thought.
https://codelabs.developers.google.com/codelabs/exoplayer-intro/index.html#0
By the way, there are a lot of good assignments in Google CodeLabs, you should check it out.

ExoMedia and controls

I have an issue with ExoMedia implementation. Here is the repository of this very good video player library based on ExoPlayer.
https://github.com/brianwernick/ExoMedia
It solved my issues with streaming videos on some Android devices.
My only problem is, I can't change / control default media controllers. Original code was:
private VideoView contentView;
contentView.setVideoURI(Uri.parse("http://127.0.0.1:54852/"));
MediaController mediaControllerView = new MediaController(this);
mediaControllerView.setAnchorView(contentView);
contentView.setMediaController(mediaControllerView);
It worked nice with nice default media controller.
It appeared that after changing it to VideoView to EMVideoView which uses ExoPlayer disabled setMediaController method.
private EMVideoView contentView;
contentView.setVideoURI(Uri.parse("http://127.0.0.1:54852/"));
MediaController mediaControllerView = new MediaController(this);
mediaControllerView.setAnchorView(contentView);
*contentView.setMediaController(mediaControllerView);* <-- no such method
EMVideoView doesn't have it implemented. I can only set to use default controls using
contentView.setDefaultControlsEnabled(true);
If I set to false, no controls are shown, and unable to control playing. I couldn't find the way, how to add MediaController to EMVideoView.
The main reason of changing it is controllers used in EMVideoView have fully transparent background, and it's not visible on some videos at all. The standard ones from VideoView have nice grey background, and the best would use them.

VideoView on Android, 2 issues

Trying to get a simple video clip playing in a loop. I got the video to play just fine. However, there are two problems I'm having.
If the user clicks the Home button, effectively hiding the app. Then they go back to it, the video is gone and doesn't reload.
Everything as far as initializing the video only takes place in the typical onCreate for the view that holds it. Where should I be calling the video from to start it to ensure that it always actually starts?
I can only get the device to find the video when pulling it from the web. I see in the logs that when I try to reference the clip from the RAW folder, it cannot find it.
VideoView myVideoView = (VideoView)findViewById(R.id.lighterView);
myVideoView.setVideoURI(Uri.parse("android.resource://com.android.AndroidVideoPlayer/"+R.raw.[videoclip]));
myVideoView.requestFocus();
myVideoView.start();
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer myVideoView) {
myVideoView.setLooping(true);
}
});
The above chunk of code works fine if I use a URL to my website. But locally, it cannot find the clip.
Instead of just load/play video in onStart() method I suggest you to look at some well written code about VideoView, because the best way to connect your video to your interface is manage your VideoView(that implements surfaceView) callBack.
You can find a good example at the following link;)
http://alvinalexander.com/java/jwarehouse/android/core/java/android/widget/VideoView.java.shtml

is there a way to play streaming youtube video from my activity?

Somewhere in my Android app I would like to play a youtube video,
in my activity, in a small view.
I'm trying to do this using VideoView, but no luck so far..
I wrote a simple activity, here is my code so far:
public class MainView 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);
/*working good but IT'S NOT youtube*/ //Uri video = Uri.parse("http://commonsware.com/misc/test2.3gp");
/*NOT WORKING*/ //Uri video = Uri.parse("http://www.youtube.com/watch?v=hLQl3WQQoQ0&ob=av2e");
/*NOT WORKING (RTSP)*/ //Uri video = U-ri.parse("http://m.youtube.com/watch?gl=IL&hl=en&client=mv-google&feature=grec_mobile&v=6WHRxXY67UA");
/*NOT WORKING (RTSP)*/Uri video = Uri.parse("http://m.youtube.com/watch?gl=IL&hl=en&client=mv-google&v=wWub_aXPCVg");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
}
is it possible?
is there any other SDK component that can do that?
any ideas?
Thanks,
Amitos80
Google are releasing android components for YouTube "in the coming months", so hopefully by the end of 2012.
What I am doing in the meantime is using a WebView with loadData(), and passing in an IFrame as stated on the YoUTube API page. The Iframe is quite limited (most parameters don't even work, like controls=0), so if you need more flexibility like controlling the view then use the JavaScript API and then embed the JavaScript in the HTMl you pass into loadData.
I tried the Media Controller and couldn't get it to work, even though YouTube use it in there own app.
The other thing to try which i didn't get around to. Is to use the google search apis. You can specify a YouTube video and it will give you the URL's to the thumbnails, videos etc. This way you can get to the raw files (www.youtube.com/...../..../myfile.3gp) rather than calling a webpage. These might then be loadable in the MediaController.
If you know what video you want to view, you could just create an Intent with the url in it and use that to launch the Youtube app. You can see this SO Post for more details.
you can simply use youtube rtsp link in videoview as:
String myurl="rtsp://v4.cache4.c.youtube.com/CjYLENy73wIaLQk_ezfHQ9mvqRMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYN-__7O28NSPUAw=/0/0/0/video.3gp";
Uri video = Uri.parse(myurl);
mVideoView = new VideoView(this);
mVideoView.setVideoURI(video);
myMediaController = new MediaController(this);
it works simply for me.

How to add ProgressDialog in VideoView?

Is there anyway I can add a ProgressDialog in VideoView in Android while the video is not showing up yet. Because all I see is black screen until the video plays. I have struggling for almost a week trying to find the solution, please help me I would really appreciate it a lot. Thanks in advance!
Here is the code I am using:
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://www.yourvideos.mp4");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
best way is to use async task... preExecute method use showing dialog... doInBackground for setting the videoUri then in onPostExecute cancel the dialog... but if u had need to stop the Loading dialog as video starts playing, implement onPreparedListener and Register for
the video view
if you dont to want use async task then show the dialog before u set the path to videoview. then cancel it on "OnPrepared" listeners "onPrepared()" method. remember u should register with videoview...
I didn't test this but you might add the dialog, and populate it with the percentage fetched by http://developer.android.com/reference/android/widget/MediaController.MediaPlayerControl.html#getBufferPercentage()
use something like a timer to update it every few ms

Categories

Resources