I'm having an issue making the Volume to 0 when the deviceready is triggered. Currently my codes are
<div id="dvVideo">
<video webkit-playsinline playsinline id="videoPlayback" poster="#">
<source src="#" type="video/mp4">
</video>
</div>
and scripts are
var vidcont = document.getElementById('dvVideo');
document.addEventListener('deviceready', function() {
vidcont.style.display = "none";
vid.src = "http://linktovideo.com/test.mp4";
vid.load();
vid.play();
vid.volume = 0.0;
});
What am I trying to achieve is that the video should preload before a button is clicked to play the video and it has to be hidden. What I'm currently doing is playing the video in the background, while the it is displayed as none, then when the button is pressed, it will displayed as block and video.currentTime is set to 0.0. Which makes the video seems to load fast.
Is there any alternative way to do this? I'm thinking of the preload="metadata" etc. but can't seem to get it to work. Can someone explain on the preload thing?
Edit: It works fine on any browsers but I just can't make it work on Android.
I'm writing a media player for my android application using angularjs, I get the songs from web API, address: http://10.96.254.34:8885/api/apimusic/download?songId=X, with "X" is the id of the song I want to stream. You can try add "1", "2" or "3" at "X" to listen to the song.
My problem is, when I use write the code for the audio player:
<div class="item item-thumbnail-left">
<h2>{{song.SongName}}</h2>
<p>{{song.Singer}}</p>
<audio id="audio" autoplay="true" preload="auto" tabindex="0" controls="" type="audio/mpeg">
<source type="audio/mpeg" src="http://10.96.254.34:8885/api/apimusic/download?songId=1">
Sorry, your browser does not support HTML5 audio.
</audio>
</div>
The song can be fetched flawlessly, but when I change it to:
http://10.96.254.34:8885/api/apimusic/download?songId={{song.SongId}}, It doesn't work
Any ideas to help me out of this? Please help :(
Try to use in source:
data-ng-src
or
ng-src
instead of using a simple src.
Its because of Strict Contextual Escaping (SCE), which is by default enabled in Angularjs.
passing expression to url inside href or src is not allowed, so instead prepare your url with expression and then assign it to src. for ex:
Instead of
ng-src="http://yourdomain.com/a={{id}}"
do
in Controller:
$scope.url = "http://yourdomain.com/a="+$scope.id;
in View
ng-src="{{url}}"
And make sure of CORS issue as well. If you are calling audio url from different domain.
need some help.
I'm building an app with HTML5 and using 'Intel XDK' to convert it to an .apk for android. The only issue I'm having is 'Looping' audio files.
I'm using a pretty simple code:
<audio id="myAudio" autoplay loop>
<source src="sounds/mysound.mp3" type="audio/mpeg"/>
<source src="sounds/mysound.ogg" type="audio/ogg"/>
</audio>
When I test the app the sound plays, but only once, no loop.
Thanks in advance!
You can achieve this in Java script in HTML by:
<script language="JavaScript" type="text/javascript">
var mp3snd = "songlocation.mp3";
var oggsnd = "home-sound.ogg";
document.write('<audio loop autoplay="autoplay">');
document.write('<source src="'+mp3snd+'" type="audio/mpeg">');
document.write('<source src="'+oggsnd+'" type="audio/ogg">');
document.write('<!--[if lt IE 9]>');
document.write('<bgsound src="'+mp3snd+'" loop="1">');
document.write('<![endif]-->');
document.write('</audio>');
//-->
</script>
It will achieve the same effect that you want (playing in background) while still being in HTML 5.
Installing this plugin might also help.
(Update for issue with XDK Android Sound Loop)
So I've made a compromise that works! Loops when app is running,but it won't run when the phone is locked.. Tested using Intel XDK Android build.
I created an internal IFRAME, that refreshes itself when the sound is over. Example below is a 1 minute sound:
On the page that audio is required:
<div id="audio_stream" style="position: absolute; margin-left: -1000px;">
<iframe src="iframes/audiotrack.html"></iframe>
</div>
This is the IFRAME code: (location: iframes/audiotrack.html)
<!--First, include refresh in meta-->
<meta http-equiv="refresh" content="60;url=audiotrack.html" />
<!--now enter audio that you wish to loop-->
<div id="audio_stream">
<audio controls autoplay="autoplay">
<source src="../sounds/FX/audiotrack.mp3" type="audio/mpeg"/>
</audio>
</div>
So it works, but not in the background. Anyone have any idea on how to keep this playing when the phone is locked?
I have been at this for days and have had no luck with trying to trouble shoot. So I have multiple live streams up on my site and I wanted to put this in an app but everytime I upload to phonegap and download on my phone the video wont play.
Neither the Flash or HTML5 version of the app plays. I simply copied the code I used on the site to place in my index.html document of my app but for some reason it fails. The message I get reads something like (going off memory) Either there was a problem with the network or server or this file is not supported). Although I know that isnt the fact because it works when I view the site from my phone, just not the app.
Markup looks something like this:
<head>
<script type="text/javascript" src="http://www.my-domain.com/jwplayer/jwplayer.js"></script>
</head>
<body>
div id='mediaplayer2'>
<script type="text/javascript"> jwplayer('mediaplayer2').setup({
'id': 'playerID', 'width': '388', 'height': '218',
'provider': 'rtmp',
'streamer': 'rtmp://0.0.0.0/some-directory',
'autostart': 'true',
'stretching': 'exactfit',
levels: [{
bitrate: "800",
file: "my-file-name",
width: "1280"
}],
'modes': [
{type: 'flash', src: 'http://www.my-domain.com/jwplayer/player.swf'},
{
type: 'html5',
config: {
levels: [ {'file': 'http://0.0.0.0/some-directory/playlist.m3u8'} ],
'provider': 'video'
}
}
]
});
</script>
<video
id="mediaplayer2"
controls="1"
autoplay="1"
height="218"
preload="none"
src="http://0.0.0.0/some-directory/playlist.m3u8"
width="388">
</video>
</div>
</body>
What do I need to do different to get this to work in an app like it does in my browser? Im developing in Dreamweaver and even when I hit "Live" it streams perfectly, just not in app.
Thanks in advance
Use Vitamio Player it can play all kind of streaming also its open source,. u just need to include this as library into ur project and make sure to use vitamio videoview or mediaplayer,. k no worry take a look at this Sample to include Vitamio into ur project
Hope its Helps you,.
Regards
Rajeev
I had developed a mobile page by asp.net to play mp4 video.
I know iOS had disabled the autoplay function to minimize user bandwidth, so how can i
autoplay HTML5 mp4 video on Android ?
I had already put autoplay in HTML5 code, but it doesn't work.
The following is my code:
<video autoplay controls id='video1' width='100%' poster='images/top_icon.png' webkitEnterFullscreen poster preload='true'>
<source src='http://192.xxx.xxx.xx/XXXXVM01.mp4' type='video/mp4; codecs='avc1.42E01E, mp4a.40.2' >
</video>
Moreover, I had fixed the problem that user click on the image overlay can play the video.
Thanks Karthi
here are the code:
<script type="text/javascript">
$(document).ready(function() {
$("#video1").bind("click", function() {
var vid = $(this).get(0);
if (vid.paused) { vid.play(); }
else { vid.pause(); }
});
});
</script>
Thanks
Joe
You can add the 'muted' and 'autoplay' attributes together to enable autoplay for android devices.
e.g.
<video id="video" class="video" autoplay muted >
I used the following code:
// get the video
var video = document.querySelector('video');
// use the whole window and a *named function*
window.addEventListener('touchstart', function videoStart() {
video.play();
console.log('first touch');
// remove from the window and call the function we are removing
this.removeEventListener('touchstart', videoStart);
});
There doesn't seem to be a way to auto-start anymore.
This makes it so that the first time they touch the screen the video will play. It will also remove itself on first run so that you can avoid multiple listeners adding up.
Android actually has an API for this! The method is setMediaPlaybackRequiresUserGesture(). I found it after a lot of digging into video autoplay and a lot of attempted hacks from SO. Here's an example from blair vanderhoof:
package com.example.myProject;
import android.os.Bundle;
import org.apache.cordova.*;
import android.webkit.WebSettings;
public class myProject extends CordovaActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
// Set by <content src="index.html" /> in config.xml
super.loadUrl(Config.getStartUrl());
//super.loadUrl("file:///android_asset/www/index.html");
WebSettings ws = super.appView.getSettings();
ws.setMediaPlaybackRequiresUserGesture(false);
}
}
I don't think autoplay works on Android, but getting a video to play can be annoyingly tricky. I suggest giving this article a read: Making HTML5 Video work on Android phones.
In Android 4.4 and above you can remove the need for a user gesture so long as the HTML5 Video component lives in your own WebView
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setMediaPlaybackRequiresUserGesture(false);
To get the video to autoplay, you'd still need to add autoplay to the video element:
<video id='video' controls autoplay>
<source src='http://192.xxx.xxx.xx/XXXXVM01.mp4' type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' >
</video>
Important Note: Be aware that if Google Chrome Data Saver is enabled in Chrome's settings, then Autoplay will be disabled.
Autoplay only works the second time through.
on android 4.1+ you have to have some kind of user event to get the first play() to work. Once that has happened then autostart works.
This is so that the user is acknowledging that they are using bandwidth.
There is another question that answers this .
Autostart html5 video using android 4 browser
similar to KNaito's answer, the following does the trick for me
function simulateClick() {
var event = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': true
});
var cb = document.getElementById('player');
var canceled = !cb.dispatchEvent(event);
if (canceled) {
// A handler called preventDefault.
alert("canceled");
} else {
// None of the handlers called preventDefault.
alert("not canceled");
}
}
In Android 4.1 and 4.2, I use the following code.
evt.initMouseEvent( "click", true,true,window,0,0,0,0,0,false,false,false,false,0, true );
var v = document.getElementById("video");
v.dispatchEvent(evt);
where html is
<video id="video" src="sample.mp4" poster="image.jpg" controls></video>
This works well. But In Android 4.4, it does not work.
Here is a plugin for PhoneGap which solved the problem for me:
https://build.phonegap.com/plugins/1031
I simply included it in my config.xml
<video autoplay controls id='video1' width='100%' poster='images/top_icon.png' webkitEnterFullscreen poster preload='true'>
<source src='http://192.xxx.xxx.xx/XXXXVM01.mp4' type='video/mp4; codecs='avc1.42E01E, mp4a.40.2' >
</video>
I simplified the Javascript to trigger the video to start.
var bg = document.getElementById ("bg");
function playbg() {
bg.play();
}
<video id="bg" style="min-width:100%; min-height:100%;" playsinline autoplay loop muted onload="playbg(); "><source src="Files/snow.mp4" type="video/mp4"></video>
</td></tr>
</table>
*"Files/snow.mp4" is just sample url
Can add muted tag.
<video autoplay muted>
<source src="video.webm" type="video/webm" />
<source src="video.mp4" type="video/mp4" />
</video>
reference https://developers.google.com/web/updates/2016/07/autoplay
don't use "mute" alone, use [muted]="true" for example following code:
<video id="videoPlayer" [muted]="true" autoplay playsinline loop style="width:100%; height: 100%;">
<source type="video/mp4" src="assets/Video/Home.mp4">
<source type="video/webm" src="assets/Video/Home.webm">
</video>
I test in more Android and ios
Chrome has disabled it. https://bugs.chromium.org/p/chromium/issues/detail?id=159336
Even the jQuery play() is blocked. They want user to initiate it so bandwidth can be saved.