Impossible to autoplay youtube videos in an Android webview by using youtube iframe API. Does someone knows if that is possible?
As already mentioned in Mobile Considerations, functions and parameters such as autoplay, playVideo(), loadVideoById() won't work in all mobile environments due to the restrictions wherein HTML5 <video> element only allows playback to take place if it's initiated by a user interaction.
You may, however, try the given suggestions in the following SO posts and might work for you:
Youtube Api android autostart
You can use loadVideo() method which loads and plays the specified video. And, there is also the cueVideo() method, which adds the video to the playlist, but does not automatically start playing the video.
Android webview html5 video autoplay not working on android 4.0.3
You can usually work around it by triggering the play() on another event (eg the onloaded event).
Hope that helps!
Related
I (actually my app team) embed my YouTube videos on the app.
The videos are played well in the app, but video views aren't counted.
We use YouTube IFrame API and android webview.
We checked the views are counted in PC and mobile, so we think IFrame API is okay.
We think the view-count problem is from Android webview.
Doesn't Android webview count YouTube video views originally?
I don't set autoplay for video watching.
Thank you.
You need to disable autoplay and let users play the video through the native play button since: "YouTube only counts playbacks that are initiated through the native play button." REF
This question is basically the same as this one which has a solution for HLTML5 videos, except that changing the chrome://flags to set gesture requirement for media playback to disabled has no influence on a YouTube embedded player.
I have built a webapp I use to learn music, and loop specific parts of Youtube music videos to learn how to play the song. Here is an example. This works fine on a computer, but using Chrome (59) on Android (7.1.1) I have to tap the video each time it starts again to put the sound back on. Not handy when trying to use my hands to learn playing the music ;)
The code I'm using to embed the player and interact via Javascript can be found here.
What's the best way to detect if a browser supports autoplay of embedded media files?
Using the youtube javascript API, on iOS (and possibly Android) devices, if you call playVideo() prior to the user tapping on the video, the video is put in a bad state and is basically useless. You might do this when you want to autoplay the video after the page loads, or you might have your own play/pause button.
There are lots of discussions on how to get autoplay to work (see below), but another way to deal with this is to detect when it shouldn't be called. The obvious answer would be to detect a specific device or OS using the user agent string, but I'm wondering if there's a better way to do it.
Related posts:
YouTube iFrame API hangs in buffering state after the playVideo() command on iPad
Can you autoplay HTML5 videos on the iPad?
Autoplay an Audio File on Mobile Safari
How can I autoplay media in iOS >= 4.2.1 Mobile Safari?
I need to play a video with fullscreen when a user plays it.
Unfortunately, as far as I understand, if the HTML5 Video tag is used, Android plays it in-frame. So that tried webkitEnterFullScreen() and it seems to work(kind of...) but user has to click the play button again. Overall performance feels a bit clumsy and not sure if old OS like 2.0+ could handle it.
As an alternative method, I'm now trying to play video using direct link rather than the Video tag. For example, [a href="video.mp4"]Click to play[/a]. I think that it works well but the only problem is that it asks to choose a application either 'Video Player' or 'Browser'.
So my question is
How can I define using javascript to play the video using Video Player, so that the selection dialog won't prompt?.
What is the native video player for Android? For example, iOS uses Quicktime and it is possible to embed video using Quicktime object. And are there any equivalent method for Android?
When the Video tag is used, how to play video simultaneously with fullscreen?
try following code :
String path1="/path/to/video/file.3gp";
Uri uri=Uri.parse(path1);
VideoView video=(VideoView)findViewById(R.id.VideoView01);
video.setVideoURI(uri);
video.start();
You have to realize something very important: There is no native video player for android.
There is a dozens players for android and it is not your decision but the users decision if he should use player A or B. Don't try to force Android users to iOS behavior, it really doesn't give a better impression.
In my Android app I'd like the user to tap an image once, have a youtube video play automatically and when the video is done the user is immediately returned to the app. What's the best way to do this in Android?
I tried using intents. This works in that the video comes up on what I think is a youtube web page. However playing the video requires another tap. I'd like to avoid this if possible.
I tried the whole MediaPlayer, prepareAsync, setOnPreparedListener and never got it to work. For some reason onPrepared was never called. No exceptions were thrown. I'm using the emulator to test and I'm new to Android so I'm not sure if the behavior will be different on physical devices.
I got this working well on iOS by getting creative with webviews. I'm hoping it's more straightforward on Android. The docs sure make it sound straight forward.
Cheers!
Update: Everything below is still correct, but the official YouTube API for Android is now available.
By far, the easiest way to play a YouTube video on Android is to simply fire an Intent to launch the native Android YouTube app. Of course, this will fail if you are not on a certified Google device, that doesn't have the complement of Google apps. (The Kindle Fire is probably the biggest example of such a device). The problem with this approach is that the user will not automatically wind up back at your app when the video finishes; they have to press the Back button, and at this point you've probably lost them.
As a second option, you can use the MediaPlayer API to play YouTube videos. But there are three caveats with this approach:
1) You need to make a call to YouTube's GData webservice API, passing it the ID of the video. You'll get back a ton of metadata, along with it the RTSP URL that you should pass to MediaPlayer to play back an H.264-encoded stream. This is probably the reason why your attempt to use MediaPlayer failed; you probably weren't using the correct URL to stream.
2) The GData/MediaPlayer approach will only play back low-resolution content (176x144 or similar). This is a deliberate decision on the part of YouTube, to prevent theft of content. Of course, this doesn't provide a very satisfactory experience. There are back-door hacks to get higher resolution streams, but they aren't supported on all releases of Android and using them is a violation of YouTube's terms of service.
3) The RTSP streams can be blocked by some internal networks/firewalls, so this approach may not work for all users.
The third option is to embed a WebView in your application. There two approaches you can take here:
1) You can embed a Flash object and run the standard desktop Flash player for YouTube. You can even use the Javascript API to control the player, and relay events back to the native Android app. This approach works well, but unfortunately Flash is being deprecated on the Android platform, and will not work for Android 4.1 and later.
2) You can embed a <video> tag to play YouTube via HTML5. Support for this varies between various releases of Android. It works well on Android 4.0 and later; earlier releases have somewhat spotty HTML5 <video> support. So, depending upon what releases of Android your application must support, you can take a hybrid approach of embedding HTML5 on Android 4.x or later, and Flash for all earlier versions of Android.
There are several threads here on StackOverflow about using HTML5 to play YouTube video; none of them really describe the entire process you must follow in one place. Here's links to a few of them:
Android - How to play Youtube video in WebView?
How to embed a YouTube clip in a WebView on Android
Play Youtube HTML5 embedded Video in Android WebView
All of this will get dramatically easier in the weeks/months to come; at Google I/O 2012, they presented/demoed a new YouTube API for Android that will support direct embedding of YouTube content in your application, with full support back to Android 2.2 (about 95% of the Android userbase as of this writing). It can't arrive fast enough.