Source language: Translate to:

Playing sound file on a timer alert!

General questions about NeoAppBuilder - our rapid application development tool for building HTML5, web and mobile apps.

Moderator: Neosoft Support

Playing sound file on a timer alert!

Postby PaulTomo » Sat Feb 07, 2015 8:54 am

Any tips on how to get a sound file to play on an "Android Phone" at the end of a timer function.

Code: Select all
BeginJS
TimeVar=setTimeout(function () {var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'http://mywebsite/Alert.mp3');
audioElement.play(); alert('Sorry - Times up!');}, 60000);
EndJS


This works fine on the Debug window within NeoAppBuilder and also as a Web test but if I add the compiled apk file to an Android phone, the Alert pops up but no sound is played.

Code: Select all
BeginJS
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'http://mywebsite/Alert.mp3');
audioElement.play();
EndJS


If however I add the JS code to a button so the sound can be played on a button click, the sound plays fine on the Android Phone.

Ideally I would want to have the sound to be added as a file within NeoAppBuilder (probably in the list of things to add) so it can be exported to the phone and played direct from there rather than a web address but as yet I've not worked out how to load local or saved "Phone files" into the project, so any tips on this would also be appreciated.
User avatar
PaulTomo
 
Posts: 62
Joined: Tue Apr 28, 2009 1:15 am
Location: UK

Re: Playing sound file on a timer alert!

Postby Neosoft Support » Sat Feb 07, 2015 11:17 am

I'm not sure why the timer wouldn't be able to play sound on your phone. Maybe this will help:

http://stackoverflow.com/questions/7620871/how-to-play-a-sound-with-js

Ideally I would want to have the sound to be added as a file within NeoAppBuilder (probably in the list of things to add) so it can be exported to the phone and played direct from there rather than a web address but as yet I've not worked out how to load local or saved "Phone files" into the project, so any tips on this would also be appreciated.


Eventually you will be able to play the sound file locally, but it hasn't been implemented yet.
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5593
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA

Re: Playing sound file on a timer alert!

Postby PaulTomo » Sun Feb 08, 2015 2:11 pm

Having done a lot of searching through JS coding forums over the last few day's I have now found a workaround for the sound file not auto playing on the Android Phone within a timer function..

I found few explanations as to why it won't auto play and this one explains it fairly well.

Introduction

If you have been trying to implement a web audio player that works perfectly on your mobile browsers, probably you've faced an issue that your player doesn't play audio automatically on page load, with a timer, or even with external calls. Or probably your desktop browsers play audio fine while the mobile browsers don't.

Background

Mobile browsers ignore the autoplay attribute on <audio> and <video> elements. Stupid reasons include saving mobile bandwidth on behalf of the user and/or securing app store sales. The only way to play the audio is to physically click/touch a button/link.

This is not acceptable and the mobile browsers' policy must be changed as there are many of us who develop web games/apps that require audio to be automatically triggered without the need to initiate a physical click/touch.


It seems that if you can play the audio file by first initiating it with a button click, it will then play a second time with an auto play within a timer!

Code: Select all
BeginJS
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'http://mywebsite/sound.mp3');
audioElement.play();
setTimeout(function() {
audioElement.play(); alert('Sorry - Times up!');}, 60000);
EndJS


So I have now managed to find a sort of work around by first playing the sound file on the timer button click and then when the time has elapsed it plays at the end of the timer.

Having the sound play twice first at the beginning and then at the end just does not seem right, I did add a mute, which again worked fine on the debugger and web but not on the Mobile Phone, so If anyone has any other ideas, solution or a better understanding of this, please let me know.
User avatar
PaulTomo
 
Posts: 62
Joined: Tue Apr 28, 2009 1:15 am
Location: UK


Return to General NeoAppBuilder Discussion

Who is online

Users browsing this forum: No registered users and 2 guests