- This topic has 11 replies, 3 voices, and was last updated 10 years, 6 months ago by Code_A.
-
AuthorPosts
-
Code_AMemberI am using javascript to play audio files in my game. Everything works fine on iOS but there is no sound on Android devices. I have read about the ‘user interaction’ requirement. This is implemented in my code but still no luck on Android. Any ideas how to fix within the JS code?
Here is a very rough outline of some code showing the logic
sound = false; //disabled on load function = buttonClick() { //tied to 'touchstart' event listener sound = true; playSound("test.mp3"); //already loaded as a resource } function = playSound(name) { //called from the game if (sound){ var audioFile = new Audio(); audioFile.src = name; audioFile.play(); } }
support-octavioMemberHi Code A,
Please take a look at the MobiOne Audio Widget API since it is the only audio resource that we officially support.
Code_AMemberThanks for the reply. I was hoping to find a workaround to use my existing JS code. If there is not one available, then I will convert to using the audio widget.
support-michaelKeymasterA couple of thoughts:
1) if you are using the html5 <audio> element then you should ensure that it is actually supported on your platform and media type. We stopped trying to support the audio tag a couple of years ago because of such inconsistent behavior across platforms. Your mileage will vary.
2) are you sure your code is running correctly? Load your project into the simulator and open the chrome dev tools. You’ll be able to see syntax errors immediately in the console. If no errors then set a breakpoint in your js code and step into it. That is the quickest way to find problems.
Code_AMemberI am trying to convert my code to use the audio widget with no UI. I keep getting an error in the phoneui.js > phoneui.createMedia function code stating that “device is not defined”. The error is occurring specifically at this line in the code:
if (phoneui.cordovaAvailable() && device.platform === "Android") {
What is causing this and how can I fix it? Can I define device?
Here is my code (less the unimportant stuff):
var test; function testThis() { //audio files this.media; this.done = function() {}; this.error = function() {}; this.status = function(status) {}; this.init = function() { this.setAudio(); this.start(); //start loop return this; }; this.setAudio = function() { this.media = phoneui.createMedia('audio/blip.mp3', this.done, this.error, this.status); }; this.start = function () { var self = this; requestAnimationFrame(function(){self.start()}); setInterval (function() { self.media.play(); }, 2000); }; window.onload = function() { test = new testThis().init(); };
Code_AMemberAny idea why my device is not defined when trying to call the phoneui.createMeadia function (see above post)? I am using Cordova 2.9. Thanks.
support-octavioMemberHi Code A,
The problem is that you are not waiting for the deviceready event that is necesary when using the cordova api. You can learn more about it here: http://www.genuitec.com/mobile/docs/usingCordovaAPI/usingCordovaAPI.html#wait_for_deviceready
So, I’d add the code for waiting deviceready event in the phoneui.documentReadyHandler function:
phoneui.documentReadyHandler = function() { test = new testThis(); document.addEventListener("deviceready", test.init(), false); }
Just tested it and worked well. Let me know how it goes for you.
Code_AMemberThank you Octavio for the reply. That helped to eliminate the device error I was receiving, however my audio will still not play on Android devices. The sound plays fine in the simulator but nothing when loaded on an actual device.
You can see from my test code that all I am trying to do is play an audio file without UI. In my real app, I have multiple sound effects that I want to play in my game. Ideally, I would like to play the audio files without UI (i.e., no widget).
I have attached my files if needed. Thanks for the help.
Attachments:
You must be logged in to view attached files.
support-octavioMemberHi Code A,
Are you able to play your audio file in the web simulator?
Note that additional files are copied to the root of your generated app’s directory, so changing the url of your media to just ‘blip.mp3’ should make your media works.Hope this is helpful.
Code_AMember@support-octavio wrote:
Are you able to play your audio file in the web simulator?
Thanks Octavio for your continued support. Yes, everything works in the simulator just fine but this test app will not run on iOS or Android when installed. I am just getting a black screen. Any idea why?
I have attached my modified files which include your recommendations.
Attachments:
You must be logged in to view attached files.
support-octavioMemberHi Code A,
I have discussed with the dev team about the problem and it looks like a cordova bug. In the meantime they proposed this workaround:
test = new testThis(); document.addEventListener("deviceready", function() { setTimeout(function() { test.init(); }, 100); }, false);
Code_AMemberOctavio, thank you for following up on this issue. The workaround seems to have fixed the issue on iOS, but Android is still having an issue both in the simulator and on the device.
On the device (Xyboard 8.2), the app is still giving me a black screen with no sound, thus not working at all.
In the simulator (Nexus S), the app works but the sound is glitchy and only plays once instead of repeating every 2s as intended.
Sometimes the app fails all together in the simulator, and I receive the following error.
See attachment Capture.PNG
What is the next step in getting to work on Android? Is there a better way to implement this that will make it work on both iOS and Android?I am making a game and need the sound effects to play in my app (preferably with no UI). Thank you for your support.
Attachments:
You must be logged in to view attached files. -
AuthorPosts