iOS7 UIWebivew Youtube MPMediaPlayer not working - uiwebview

I need to play a video within an UIWebiview which comes from youtube.
Everything was working fine on older devices (5.1+)
Unfortunately on iOS7 the video is not playing. It starts loading but it'll never start playing. Even if the video has fully been loaded. Only if you stop and press play manually the video starts playing.
Here's some code:
- (void)viewDidAppear:(BOOL)animated;
{
[super viewDidAppear:animated];
if(!self.viewMoviePlayerView.superview) {
[self.viewMediaContentContainer addSubview:self.viewMoviePlayerView];
}
self.viewMoviePlayerView.isScrollingEnabled = NO;
[self.viewMoviePlayerView loadRequest:[NSURLRequest requestWithURL:
[NSURL URLWithString:
[NSString stringWithFormat:#"%#%#", #"http://www.youtube.com/embed/", self.currentSelectedItem.guid]]]];
...
}
Any suggestions on that?

Autoplay is not allowed on recent versions of iOS. You don't suppose to programmatically start downloading data that could cause a cost to the user. I found a way but it's a little tricky:
Show an html string in stead of an url
[webview loadHTMLString:<html> baseURL:[[NSBundle mainBundle] resourceURL]];
The html needs to be an embed player:
<html>
<body style='margin:0px;padding:0px;'>
<script type='text/javascript' src="http://www.youtube.com/iframe_api"></script>
<script type='text/javascript'>
function onYouTubeIframeAPIReady(){
ytplayer=new YT.Player('playerID',{events:{onReady:onPlayerReady, onStateChange:onPlayerStateChange}});
}
function onPlayerReady(a){ a.target.playVideo(); }
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.BUFFERING) {
event.target.playVideo();
}
}
</script>
<iframe id='playerID' type='text/html' width='320' height='240'
src="http://www.youtube.com/embed/xxxxxxxxxxx?enablejsapi=1&rel=0&playsinline=1&autoplay=1" frameborder='0'></iframe>
</body>
</html>
this works fine because the javascript in the iframe API it forces the player to start playing once it loads and every time it starts buffering. I strongly suggest you to read about the youtube API
Put your video id in the embed and thats it!

Related

Using playlistitem.insert via one link

I've been able to do video searches and read info using just one link that starts with googleapis.com but for the playlistitemsinsert to put videos into a playlist, I'm having trouble with the syntax. I'm trying to randomize the order of videos in a YouTube playlist. I understand you can shuffle play but its easier for me to remove the videos if they're played in chronological order, also its easier to go back to a video you recently watched.
I have all of the video ids in a column in excel I'm just not sure how to use vba to do a POST HTTP request to insert the videos into a playlist. Can you help me with the syntax on this?
Although not using excel as database This works using youtube api. and users youtube created play list . shuffles the play list into new order every time page is refreshes
Load YT-player API.
Load YT-playlist user playlist id "**.youtube.com/playlist?list=PLo16_*******"
To Shuffle playlist use > player.setShuffle(true);
To Start YT-player at video 1 in shuffled playlist use > player.playVideoAt(0)
working demo Responsive shuffled YouTube Playlist on Google sites
code jsfiddle.net
<!DOCTYPE html>
<html>
<!-- Responsive YouTube shuffled playlist player -->
<head>
<base target="_top">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag.
style = replaces size in the player script makes it responsive -->
<div style=" top: 0; left: 0; width: 100%; height: 100%; position: absolute;"
id='player'>
</div>
</body>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
playerVars: {
autoplay: 0,
loop: 1,
controls: 1,
showinfo: 1,
frameborder: 1,
'listType': 'playlist',
'list': "PLo16_DLriHp4A8BvkJFZfO_4KDVv7yGgy", // your YouTube playlist id here
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
// first shuffle play list
player.setShuffle(true);
// Onload and on refresh shuffles the users YT playlist but always starts playing
// the first video in the original list at its new index position
//ie. video (1) = video( new shuffled pos ?)
// to get over this we can start the player at the new shuffled playlist
//video 1(index=0) this changes every time it's refreshed
player.playVideoAt(0)
}
// 5. The API calls this function when the player's state changes.
// option to to add bits
function onPlayerStateChange(event) {
const player = event.target;
}
</script>
</html>

Playing audio in a safari web app in iOS 8.1

Bevour you mark this as "old", I've searched the whole www for a solution, but I can't find one.
I am making a WebApp that plays sound-effects. I've created a var audio = new Audio() element, and changed his src to the URL of the sound-effect. When I call the audio.play() function, it starts playing the sound effect. In normal safari it works, but when I set apple-mobile-web-app-capable to true in a meta-tag, the WebApp crashes. I tried also to use a hard coded audio tag, to create a audio tag using javascript and to populate a div with a audio-element and it didn't work.
var audio = new Audio;
function set(src){ audio.src = src; }
function play() { audio.play(); }
function pause() { audio.pause(); }
<button onClick="set('https://incompetech.com/music/royalty-free/mp3-royaltyfree/Thief%20in%20the%20Night.mp3')">Set sound</button>
<button onClick="play()">Play sound</button>
<button onClick="pause()">Pause sound</button><br /><br />
<b>Music:</b> "Thief in the Night" Kevin MacLeod (incompetech.com) Licensed under Creative Commons: By Attribution 3.0 License http://creativecommons.org/licenses/by/3.0/
Tnx in advance, Stephan

How to load YouTube API locally?

I need to load the YouTube API locally (Google Extension not allowing external scripts). So I downloaded the files into my directory:
https://www.youtube.com/player_api
https://s.ytimg.com/yts/jsbin/www-widgetapi-vfl4qCmf3.js
However, when I try to programmatically inject this via a content script, it gives the following error:
Uncaught ReferenceError: YTConfig is not defined
To the OP: You were almost there! I'd like to acknowledge Jonathan Waldman, MSDN author, for mentioning this technique during one of his training sessions...
To review, YTConfig is defined at:
http://www.youtube.com/iframe_api
And here:
http://www.youtube.com/player_api
The iframe_api is the preferred link to use.
To recap, the links you provide above each link to JS code:
https://www.youtube.com/player_api
https://s.ytimg.com/yts/jsbin/www-widgetapi-vfl4qCmf3.js
You also need a Div tag with an id of "player" and another Script tag that lets you specify which video you want to see. So there are four Script tags total:
For content in iframe_api
For content at https://s.ytimg.com/yts/jsbin/www-widgetapi-vfl4qCmf3.js
For the player Div tag:
<div id="player"></div>
For JavaScript that configures the video
This translates loosely to:
<script>...place the player Div here</script>
<script>...place code from www-widgetapi-vfl4qCmf3.js here...</script>
<script>...place code from iframe_api here...</script>
<script>...place the code below here</script>
Here is the code for the final Script tag (note that you will likely want to change the videoId property so that it points to the ID of the desired video on the YouTube site):
<script>
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
When I did the above, the page worked and I had all of the YouTube code local to my development machine.

Immediate play sound on button click in HTML page

In my HTML page I have 9 images for dialing numbers and one text box that shows the pressed numbers. I want each of those images to immediately play beep sound when users click on them. I tried to use embed with hidden property and navigate it's source to .wav sound.
It is working OK, but when I press the images one after another immediately, it cannot play sound and just bees once at the end.
Is there any faster way of playing a .wav sound on 'onclick' method?
If you only need to support recent browsers, then HTML 5 offers you the Audio object
to load/buffer your sound:
var snd = new Audio("file.wav");
to play the sound:
snd.play();
to re-cue it to the beginning (so that you can play it again):
snd.currentTime=0;
This answer https://stackoverflow.com/a/7620930/1459653 by #klaustopher (https://stackoverflow.com/users/767272/klaustopher) helped me. He wrote:
HTML5 has the new <audio>-Tag that can be used to play sound. It
even has a pretty simple JavaScript Interface:
<audio id="sound1" src="yoursound.mp3" preload="auto"></audio>
<button onclick="document.getElementById('sound1').play();">Play
it</button>
Here's how I implemented his advice so that clicking on the Font Awesome icon "fa-volume-up" (located on the Web page after "mule.") results in "donkey2.mp3" sound playing (note: mp3 doesn't play in all browsers).
<p>In short, you're treated like a whole person, instead of a rented mule. <audio id="sound1" src="assets/donkey2.mp3" preload="auto"></audio><a class="icon fa-volume-up" onclick="document.getElementById('sound1').play();"></a>
You can use embed element for play sounds, but you've to check the formats supported by the different browsers.
Embed element on MDN
<a onclick="playSound('1.mp3')">
<img src="1.gif">
</a>
<div id="sound"></div>
<script>
var playSound = function (soundFile) {
$("#sound").html("<embed src=\"" + soundFile + "\" hidden=\"true\" autostart=\"true\" />");
}
</script>
This code lets you put in a picture button; when click you get a sound. It works with Google Chrome and Microsoft Edge but I can't get it to work in Internet Explorer. I'm using html 5 codes; please copy and paste and add you own samples.
</head>
<body>
<script>
var audio = new Audio("/Sample.wav ");
audio.oncanplaythrough = function ( ) { }
audio.onended = function ( ) { }
</script> <input type="image" src="file://C:/Sample.jpg" onclick="audio.play ( )">
</body>
</html>
more on codes look at
http://html5doctor.com/html5-audio-the-state-of-play/
Example based on accepted answer (Tested in Chrome 70), but I didn't need to re-cue:
<button onclick="snd.play()"> Click Me </button>
<script>
var snd = new Audio("/Content/mysound.wav");
</script>
This is what I would do to play sound effects:
<html>
<body>
<audio id="sfx"><source src="mysound.mp3"></audio>
<button onclick="playsound()" id="button">Play a sound!</button>
<script> function playsound() {
var sfx = document.getElementById("sfx");
sfx.autoplay = 'true';
sfx.load();}
Or you can run this snippet:
function playsound() {
var mysound = document.getElementById("mysound");
mysound.autoplay = 'true';
mysound.load();
}
button {
color: blue;
border-radius: 24px;
border: 5px solid red;
}
body {
background-color: #bfbfbf;
}
<html>
<body>
<audio id='mysound'><source src="click.mp3"><!-- "click.mp3" isn't a sound effect uploaded to the snippet, because I don't think you can upload sfx to snippets. (I'm new to stackoverflow, so there might be a way) But if you actually use a sound effect in that folder that you're using, it works. --></audio>
<button id='btn' onclick='playsound()'>Play a sound!</button>
</body>
</html>

How to implement event handling in uiwebview?

I have taken a webview to show some HTML content in iPhone. Now I want to put some action on user events on the phone. I want to trace the tap event done on the screen by the user on that web view.
For this I have added a method
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
in my code.
But the problem is when I click outside the webview it works perfectly. Bt when I tap on the web view, it does not respond. So, please tell me what should I do to acheive tap event trace on that particular web view.
Help me out.
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
if ([[url lastPathComponent] isEqual:#"testmessage"]) {
NSLog(#"Nya");
return NO;
}
return YES;
}
HTML
<a href='testmessage'>Test</a>
or
<script>
function callObjc(){
document.location = 'testmessage';
}
</script>
<a onclick='callObjc();'>Test</a>

Resources