I am having an app build with Cordova and Angular JS. I am trying to play a sound when a new event occurs and I am trying to achieve this by creating an append of tag within the new event function and then calling #mySound.play(). When I test it on my desktop browser there it is working perfectly on every new event I can hear the sound playing but on the other hand when I create it's APK through Phonegap and test it on my Samsung Galaxy there it isn't playing any sound on new event. Kindly let me know if I am missing anything or what could be a better way to achieve this goal?
function displayEvents(data,div_id)
{
var icon_trans_type=''; var icons=''; var icons2='';
var htm='';
$.each( data, function( key, val ) {
var new_tag='';
if (val.viewed==1){
$('<audio id="chatAudio"><source src="locales/android/raw/notify.mp3" type="audio/mp3"></audio>').appendTo('body');
$('#chatAudio')[0].play();
new_tag='<div class="new-tag rounded" id="new-event">'+ getTrans('new','new') +'</div>';
}
});
//createElement('new-events',htm);
createElement(div_id,htm);
}
The play method provided by HTML5 API is useful for playing media files available on the web. Cordova provides the media plugin to play local media files in device.
Cordova Media Plugin should do the trick for you as it is supported in various mobile platforms like Android, iOS, Windows, BlackBerry etc.
Related
I have this code to play a video in a html5 page:
<video autoplay loop id="bgvid">
<source src="video-background.mp4" poster="/poster.png" type="video/mp4">
</video>
The problem is that it does not work in mobile chrome (Android Phone) and either in mobile safari (iPhone). But it works in "every" browser (tested with Safari, Chrome, Firefox) in desktop and also on mobile firefox (Android Phone).
How can I overcame this problem?
Edit:
Added this code:
var myVideo = document.getElementById("bgvid");
function playVid() {
myVideo.play();
}
function pauseVid() {
myVideo.pause();
}
If I add a button that trigger the function playVid() it works. So I think the problem is on the autoplay. I tried to trigger the function with the load event but it does not works.
Very simply there is no support for autoPlay on mobile safari . Please test all android browsers .
I use one trick with buffering (show some popup to force user for initial first click on any tag/document - It is smart to use accept cookies or term of use popups):
var ONLYONETIME_EXECUTE = null;
window.addEventListener('load', function(){ // on page load
document.body.addEventListener('touchstart', function(e){
if (ONLYONETIME_EXECUTE == null) {
video.play();
//if you want to prepare more than one video/audios use this trick :
video2.play();
video2.pause();
// now video2 is buffering and you can play it programmability later
// My personal testing was maximum 6 video/audio for android
// and maybe 3 max for iOS using single click or touch.
// Every next click also can prepare more audios/videos.
ONLYONETIME_EXECUTE = 0;
}
}, false)
}, false)
// It is very usually that user touch screen ...
Review :
I dont understand ios html5 politic . They stop supporting javascript console logger (i quest now :from ver 5.1 ios) .Auto play disabled , webrtc ...
They wanna device who works perfect. Auto play can be dangerous on load. In near future i expect activation full html5 support on all mobile devices.
New update :
I found this like positive answer :
Since the release of iOS 10 Apple has allowed muted video autoplay: https://webkit.org/blog/6784/new-video-policies-for-ios/
I want users to be able to share a url link on google plus from inside my as3 app using navigateToUrl, but I want the native google plus app to open up, not go through mobile web browser. I am using the as3 function below for iOS which works perfectly fine:
function shareOnGooglePlus(event:Event):void {
var request:URLRequest = new URLRequest("gplus://plus.google.com/share?url=" + URL_LINK);
navigateToURL(request, "_blank")
}
This function just isn't doing anything on android and I don't know why. Does anyone have any ideas on this?
Thanks in advance
i am working in mobile application using Flash 6 Air , AS3 , i want to know whether there is any possibility to make the auto update feature , when ever the user open the app , it goes to see for update .
so could you please help me on this requirement and give me the code that I have to use ?!!
thanks in advance.
there's two options here
Upload your app to the Google Play Store, everytime you update your app, increase the version number, upload the new version and Google will handle it for you.
The more complicated option is to write your own update downloader + a native extension to tell the operating system to launch the .apk file.
A sample downloader
public function start():void
{
var fileLoader:URLLoader = new URLLoader
fileLoader.dataFormat = URLLoaderDataFormat.BINARY;
fileLoader.addEventListener(Event.COMPLETE,downloadSuccess);
var url:URLRequest = new URLRequest("http://www.example.com/updates/"+targetVersion+".apk");
fileLoader.load(url);
}
private function downloadSuccess(event:Event):void
{
var fileLoader:URLLoader = event.target as URLLoader;
fileLoader.removeEventListener(Event.COMPLETE,downloadSuccess);
var fs:FileStream = new FileStream();
var file:File = File.userDirectory.resolvePath(targetVersion+".apk");
fs.open(file,FileMode.WRITE);
fs.writeBytes(fileLoader.data);
fs.close();
//insert native extension call here.
}
Then you'll need an ANE to trigger the android package installer. There are plenty of ANE tutorials out there, though none that cover specifically what you want. I recommend starting with this one.
I am currently working on an app that has to play audio from an external source. It is in the .amr file format, but giving it the URL and looking at log cat I can see its trying to access it locally logging:
FAILED renaming /storage/sdcard0/tmprecording.3gp to /storage/sdcard0/http://URL/audio.amr
Here is my player code
function play(source) {
var media = new Media(source, Played, Failed);
media.play();
document.getElementById('status').innerHTML = "playing";
}
with the function linked to a button and given a URL to the audio. I am testing this out on Android.
Thanks!
Never mind, it was just an issue with formats, .amr is an incompatible format so it didnt work
I'm working on a simple app that has but a few functions, that is supposed to play audio ether from a web site ie.
function playfile(){
var playing = "http://static.weareswoop.com/audio/charlestown/track_1.mp3";
var my_media = new Media(playing,
function() {
alert("playAudio():Audio Success");
},
function(err) {
if(err.message != undefined) { alert("playAudio():Audio Error: "+err.message); }
if(err.code != undefined) { alert("playAudio():Audio Error: "+err.code); }
});
// Play audio
my_media.play();
}
with the html of
play
No matter what I do this does not work on Andriod (version 2.X and 4.X), but does work just fine on iOS (v5 iPad 1 tested).
If I do this against a web based file, I get no response from either the Success or Failure functions, but if I run this agains a local file on the SD car i.e. file:///mnt/sd-card/mining/charlestown/track1.mp3 I get error code 1 without an error message.
Any help would be more than welcomed.
edit: to get the local file version which I am using FileTransfer() download (which works just fine on any OS).
window.appRootDirName = "mining"; // sets the working directory
var playing = window.appRootDir.fullPath + "/track1.mp3"; // sets filename
Thus I end up with a URI file:///mnt/sd-card/mining/track1.mp3 which is the exact correct uri when working on 2.x Samsung Galaxy Y.
Again, I must stress, this entire situation works on iOS, but not on Android. On Android when getting a HTTP: based file it gives no error or success, on FILE:// it gives error code 1 when trying to play.
You have to add in the android_asset/www/ to the filepath in order to get it to play from the file see the following bug.
Playing local sound in phonegap
this should solve the playing a local file part of the problem
I tried playing http://static.weareswoop.com/audio/charlestown/track_1.mp3 and it plays fine for me using Cordova 3.0 and android 4.3
Playing media files does not work when running via the PhoneGap Desktop / PhoneGap Developer apps. It does work when you do a "real" build, for example using PhoneGap Build, and then install the app on your phone.
So as well as using the full file path, as described in other answers, you also need to be running a "real" build.
It looks like this is a bug that PhoneGap know about and have on their list: https://github.com/phonegap/phonegap-app-desktop/issues/335.