My application contains a video with custom UIButtons to manage the video.
By using the Youtube iFrame api I have managed to play the youtube video in UIWebView and hide all its default controls(for fullscreen, volume, etc.).
Now I want to control the video through custom buttons.
How do i do that?
- fullscreen UIButton: to make the video fullscreen
- Mute/Unmute button: to mute/unmute the video
refer the screen from my other question:
objective-c: play video by removing the default fullscreen, etc functionality
How do i solve this? Code for video in UIWebview:
NSString *htmlString =#"<!DOCTYPE html><html> <body><div id=\"player\"></div><script>var tag = document.createElement('script');tag.src = \"https://www.youtube.com/iframe_api\";var firstScriptTag = document.getElementsByTagName('script')[0];firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);var player;function onYouTubeIframeAPIReady() {player = new YT.Player('player', {height: '196',width: '309',videoId: 'GOiIxqcbzyM',playerVars: {playsinline: 1, controls: 0}, events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}function onPlayerReady(event) {event.target.playVideo();}var done = false;function onPlayerStateChange(event) {if (event.data == YT.PlayerState.PLAYING && !done) {setTimeout(stopVideo, 6000);done = true;}}function stopVideo() {}</script></body></html>";
_webViewVideo.delegate = self;
static NSString *youTubeVideoHTML = #"<iframe webkit-playsinline width=\"309\" height=\"200\" src=\"https://www.youtube.com/embed/GOiIxqcbzyM?feature=player_detailpage&playsinline=1\" frameborder=\"0\"></iframe>";
[_webViewVideo loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] resourceURL]];
This combination does the video to hide controls
NSDictionary *playerVars = #{
#"controls" : #0,
#"playsinline" : #1,
#"autohide" : #1,
#"showinfo" : #0,
#"modestbranding" : #1,
#"rel":#0
};
To make the video full screen on button click, try saving the current video elapsed time and by changing playerVars to show controls and trigger full screen button action programmatically(the video plays in full screen) and the seek to method to play the video from elapsed time.
For mute/unmute , try reducing the current phone volume programmatically to mute and set it on unmute
i dont think apple provides the api to change the volume.
only user can do it using the hardware controls
Related
I am trying to show a youtube video but sometimes the YTPlayerView is being displayed in full screen and sometimes not. I want it to be always in full screen. How can I achieve it?
ytView = [[YTPlayerView alloc] initWithFrame:self.view.bounds];
ytView.backgroundColor = self.view.backgroundColor;
ytView.delegate = self;
NSDictionary *playvarsDic = #{ #"controls" : #1, #"playsinline" : #0, #"autohide" : #1, #"showinfo" : #1, #"autoplay": #1, #"modestbranding" : #1 };
[ytView loadWithVideoId:firstImage.Source playerVars: playvarsDic];
Manually set playerView.webView.allowsInlineMediaPlayback = false in playerViewDidBecomeReady to force the player to go into webview.
To play youtube video in full screen you need to add one player property to play video in full screen.
fullscreen property: fs = 1 or 0
Update your dictionary like below:
NSDictionary *playvarsDic = #{ #"fs" : #1,
#"controls" : #1,
#"playsinline" : #0,
#"autohide" : #1,
#"showinfo" : #1,
#"autoplay": #1,
#"modestbranding" : #1 };
fullscreen property: fs
Setting this parameter to 0 prevents the fullscreen button from
displaying in the player. The default value is 1, which causes the
fullscreen button to display.
Check all player properties on this link: https://developers.google.com/youtube/player_parameters?playerVersion=HTML5
Hope this will help you to show your youtube video always in full-screen mode!
My problem was, changing quality during video playing.
When i used setPlaybackQuality in onReady event function, quality was changed, but during video playing it was not possible. Although onPlaybackQualityChange event returns quality which i wanted but in fact didn't changed video quality.
player[playaID] = new YT.Player('content_'+playaID, {
height: playaHeight,
width: playaWidth,
videoId: playaVID,
playerVars: {
'autoplay':0,
'controls':0,
'showinfo':0,
'fs': 1,
'rel': 1,
'modestbranding':0
},
events: {
'onReady': onPlayerReady(playaID),
'onStateChange':onPlayerStateChange(playaID,event),
'onPlaybackQualityChange':onPlayerQualityChange(playaID,event)
}
});
SOLLUTION:
If you want to change quality during video playing:
1: you must stop video player.stopVideo()
2: set quality - player.setPlaybackQuality('hd720')
3: play video - player.playVideo()
below is mine function for setting quality.
var setQuality=function(playaID,q){
var curSeek = player[playaID].getCurrentTime(); // current video time
var status = player[playaID].getPlayerState(); // video status
if(status!=-1 && status!=5){ // if not started, does not to be stoped
player[playaID].stopVide();
}
player[playaID].setPlaybackQuality(q);
player[playaID].seekTo(curSeek);
if(status!=1){
player[playaID].paseVideo(); // if was paused
}else{
player[playaID].playVideo(); // if was playing
}
}
I used YouTubePlayerSupportFragment to play a YouTube video, but it plays the video for 2 or 3 second and stops automatically. I can't find what's wrong.
Below is my code for fragment translation
mYoutubePlayerFragment = new YouTubePlayerSupportFragment();
mYoutubePlayerFragment.initialize(
"key",
mListnnerYouTube);
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.fragment_youtube_player,
mYoutubePlayerFragment);
fragmentTransaction.commit();
and my onInitializationSuccess
#Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer player,
boolean wasRestored) {
// TODO Auto-generated method stub
activeplayer = player;
activeplayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
if (!wasRestored) {
// player.cueVideo("nCgQDjiotG0");
player.loadVideo("nCgQDjiotG0");
}
I used YouTubePlayerSupportFragment in fragment
error message
YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is obscured by com.viewpagerindicator.CirclePageIndicator{420b9758 V.ED.... ......I. 0,412-720,465 #7f0a005c app:id/indicator}. The view is inside the YouTubePlayerView, with the distance in px between each edge of the obscuring view and the YouTubePlayerView being: left: 0, top: 412, right: 0, bottom: 0..
You're most likely displaying something on top of the player view, which is forbidden by the YouTube SDK.
Check the logs, you should see a warning from the YouTube SDK that specifies why the video playback has been stopped. If a View is covering part of the player, the log will specify its ID and its position on the screen.
When playing embedded youtube videos, I don't want to display a poor resolution thumbnail when completed. Instead I'd like to have a high-resolution image of the last frame. Is this possible?
Either I can pause on the last frame or maybe there's a way to create a high-resolution thumbnail of the last frame?
You can use the YouTube Player API to detect when playback has ended, and at that point do some DOM manipulation to swap out the player and swap in an img tag pointing to one of the video's thumbnails. There normally would not be a thumbnail for the last frame in the video, though, so you would have to choose a different thumbnail.
If you just want to disable the related videos that are shown when a video ends, you can use the rel=0 player parameter, but that will leave you with a black screen.
Video player gives a callback "onStateChange" with the status YT.PlayerState.PAUSED before calling the callback with status YT.PlayerState.ENDED, you can try here.
I am also doing the same thing. I am just displaying an overlay to cover the video with replay button, But still it appears at the end of the video and then overlay appears (some glitch is observed)
I ran into this same problem and after spending a bunch of time playing around with it, the best solution for me was to just pause the video a fraction of second before the end, thus keeping it on that frame. Something like this:
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
playerVars: { 'autoplay': 1, 'controls': 0,'loop': 1, 'showinfo': 0, 'disablekb': 1, 'version': 3, 'vq': 'large', 'wmode':'opaque','rel': 0 },
videoId: 'YOURVIDEOID',
events: {
'onReady': onPlayerReady,
'onStateChange': onStateChange,
'onytplayerStateChange': onStateChange }
});
}
var done = false;
function onStateChange(event){
var videoDuration = player.getDuration();
if (event.data == YT.PlayerState.PLAYING && !done) {
// pause 0.1 seconds before the end
setTimeout(pauseVideo, (videoDuration-0.1)*1000);
done = true;
}
}
function pauseVideo() {
player.pauseVideo();
}
After the video starts (which we catch using onStateChange), we can get the video duration from the YouTube Player API (using player.getDuration), and then use setInterval to time a function that pauses the video 0.1 seconds (or whichever value we choose) before the end.
After running into to this issue too I ended up with this, which has been working pretty consistent for me so far:
var player;
var curTimer;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
videoId: 'The Video ID',
playerVars: { 'rel':0, 'enablejsapi':1, 'autohide':1, 'wmode':'opaque'}
});
player.addEventListener('onReady', 'onPlayerReady');
player.addEventListener('onStateChange', 'onPlayerStateChange');
}
function onPlayerStateChange(event) {
var videoDuration = event.target.getDuration();
var curTime = event.target.getCurrentTime();
if ( curTime ) {
/** Video Already Started */
/** Get video time remaining. */
videoDuration = videoDuration-curTime;
/** Remove old 'setTimeout' function */
removeTimeout(curTimer);
}
/**
Note: The '.25' is time subtracted to stop video on last frame.
Adjust this as needed.
*/
/** Add/Re-Add 'setTimeout' function with video time remaining. */
curTimer = setTimeout(pauseVideo, (videoDuration-.25)*1000);
}
function removeTimeout(elTimer){
/** Remove old timer */
clearTimeout(elTimer);
}
function stopVideo() {
player.stopVideo();
}
Explanations and/or notes are within the comments.
var thumb = true;
function onStateChange(event) {
if (thumb && event.data == YT.PlayerState.PLAYING) {
thumb = false;
player.pauseVideo();
return;
}
if (event.data != YT.PlayerState.ENDED) {
return;
}
player.setPlaybackQuality('highres');
player.seekTo(player.getDuration() - 1, true);
player.playVideo();
thumb = true;
}
In my Blackberry app, I want to display a screen with a video and a lebel field. I am using the following code for video display and auto play.
VerticalFieldManager vfm = new VerticalFieldManager();
Player player = javax.microedition.media.Manager.createPlayer("file:///SDCard/BlackBerry/videos/sample.3gp");
player.realize();
videoControl = (VideoControl) player.getControl("VideoControl");
Field videoField = (Field) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,"net.rim.device.api.ui.Field");
VolumeControl volume = (VolumeControl) player.getControl("VolumeControl");
volume.setLevel(30);
player.start();
vfm.add(videoField);
..................
......................
.............
add(vfm);
It is working well. But, when the video is over, it is showing a black screen, but I want to display the first screen when the display is over and video can be replayed if user wants.
What change is needed in my code to achieve this feature?