- This topic has 11 replies, 3 voices, and was last updated 10 years, 9 months ago by Code_A.
-
AuthorPosts
-
Code_AMemberI am trying to play an audio file using the following javascript below (simplified code for this post). I can get .wav files to play but .mp3 files will not. From what I have seen, it appears that .mp3 files are supported. Can someone tell me why they won’t play? Do I need to try using jPlayer instead?
var audioFile = new Audio(); audioFile.src = 'test.mp3'; res['test.mp3'] = audioFile; res['test.mp3'].currentTime = 0; res['test.mp3'].play();
Code_AMemberNever mind. I figured it out. Problem was between the chair and the computer.
Code_AMemberOk, so I got the mp3 files playing but the it seems to cause a delay in my game when it plays the audio. Is there a more efficient way to play audio that will not cause a delay?
here is an expanded snippet of the code I am using for playing the audio:
if(this.click) { this.sound = true; this.playSound('test.mp3'); //play audio } this.loadAudio = function() { var self = this; var loaded = 0; var sounds = this.getAudioResources(); for(var i = 0; i < sounds.length; i++) { var audioFile = new Audio(); audioFile.src = sounds[i]; audioFile.addEventListener('canplaythrough', function() { loaded++; if(loaded == sounds.length) { self.audioLoaded = true; } }, false); this.res[sounds[i]] = audioFile; } }; this.playSound = function(name) { if (!this.audioLoaded){ this.loadAudio(); } if(this.sound) { //this.res[name].currentTime = 0; this.res[name].play(); } }; this.getAudioResources = function() { var sounds = [ 'test.mp3'; ]; return sounds; };
BrandonMemberHave you tried using the phonegap media plugin for it?
https://github.com/apache/cordova-plugin-media/blob/dev/doc/index.md
Code_AMemberNo not yet but was wondering if I should try that instead. Before I did though I was just curious if I was doing anything wrong in my code above that would be causing the delay on the game. I will give the phonegap plugin a try. Thanks.
BrandonMemberI dont see anything wrong with the code (havent tested it tough) but I think it may be a problem with the click function. It always seems to delay, where if you call it directly from a widget it seems fine…might be something to try.
Code_AMemberThanks Cincy. The code works fine b/c the audio plays (with a delay) and I know the click function works properly b/c I am using it to move the object in the game. I am confident the clicks are being captured without delay.
My game runs entirely inside an HTML widget. I assuming there is no way to communicate (i.e., pass data) from the HTML widget back to mobione, correct? Ideally, I would like to use the audio widget to play the files but due to my setup I don’t think that is possible.
BrandonMemberAn html widget can pass data, it can adjust the variables, depending on other technologies in the html widget. However, no matter what you can get and share data, even with an external website. It just depends on what you are trying to do.
Code_AMemberCan I call a function in my _custom.js file from the JS in my HTML widget? I do not have the ability to test right now.
If so, then my thought would be to use the audio widget to play the game sounds instead of trying to play them through JS. However, I assumed this type of communication would not be possible. At least not directly.
BrandonMemberYes you can. The HTML widget is actually very powerful, you can even dynamically load into it…I use it a lot.
support-octavioMemberHi Code A,
You might want to use the Audio Widget API, learn more about it here: http://www.genuitec.com/mobile/docs/audioWidgetAPI/audioWidgetAPI.html
Code_AMemberWorked like a charm! For some reason I just assumed that I could not reference widgets by code outside of the Run Javascript box or the _custom.js file. You know what they say about people who assume….
The important thing is I learned something today! Thanks guys for your help.
-
AuthorPosts