VideoView on Android, 2 issues - android

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

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.

YouTubePlayer not load Ad video with cueVideo()

I'm using YouTubePlayer to play YouTube video and use cueVideo(videoId) to load video, Which is working fine if video not contain Ad but video contain Ad then cueVideo(videoId) will not load video.
Also seen some discussion regards such problem which are suggested use loadVideo(videoId) instead of cueVideo(videoId) but as per my requirement i shown image until video not buffer and when video buffered hide image and show YouTubePlayer, So have to use cueVideo(videoId) instead of loadVideo(videoId).
Does any one having such issue ? thanks in advance for any suggestion or help.
Below is my code to load video :
youTubePlayer.cueVideo(videoId);
Preface: YouTube does not allow any View to be displayed over its video player.
My guess is that your "loading image", since it is displayed over the video player, is covering the ad that starts rolling for some videos. You can check this by reading the logs and keeping an eye for a warning thrown by the YouTube SDK.
I would suggest to use the YouTubePlayer.PlayerStateChangeListener callback, that offers the following methods:
abstract void onAdStarted()
abstract void onVideoStarted()
abstract void onLoaded(String videoId)
By using these methods, you can guarantee that your "loading image" is properly hidden just before the video or the ads starts playing.

Rooted Android streaming video client for local ip camera without any delays

I have an ip camera and I want to include the video stream in an Android activity. To do that I included a VideoLayout with id video and I added this code to the onCreate method in the Activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.video);
Uri video = Uri.parse("rtsp://192.168.1.225/axis-media/media.amp");
videoView.setVideoURI(video);
videoView.start();
}
It works but there is an annoying delay of 10 seconds between the acquisition of the image from the camera and the displaying to the phone. I want the delay lesser than 2 seconds. I know the problem is related the fixed size of the buffer used for caching images during RTSP session. If I use vlc I can view the stream well by setting network-caching=0. Is there a way to do that in Android? The software destination is a ROOTED Android 4.0.3. Can I resolve by taking advantage of the root? I read that I can set the media.stagefright.cache-params of the build.prop file but I don't know if it is useful and how to set it..
Please help me! Thank you!

VideoView won't play while context is set to WebView

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.

Android : how to stream/play any music file on HTML page in android browser

There is one HTML page, streaming a music file and It's working on window but i need to run this html file on android.
also i need to control all the feature of music by javascript function e.g. play,pause,stop, volume up & down.
Can someone give me any idea?
Note : i am new to android but good hand in java. Just going through android tutorial and i came to know this feature can be done using webview. is it ?
Its not clear from the post if its a requirement that you have to play it on a html page or you just want to play the .mp3 file linked from some online page.
However, You can look at this Streaming Audio tutorial as an initial reference. The MediaPlayer API would be your goto place in android for anything to do with Audio/ Video. WebView is mainly for embedding a browser view within your android App.
Here is an example quite close to the thing you're going to do...
public class MyJSInterface{
private MediaPlayer mp = new MediaPlayer();
....
public void play(String url){
this.mp.setDataSource(url);
this.mp.prepare();
this.mp.start();
// AlterDialog etc.
}
public void stop(){
this.mp.stop();
}
....
}
HTML from your website:
function _play(url){
window.myappname.play(url);
}
function _stop(url){
window.myappname.stop();
}
thing you may need
Android Media

Categories

Resources