facebook

mp3 won’t play using javascript: CLOSED

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #348223 Reply

    Code_A
    Member

    I 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();
    
    #348225 Reply

    Code_A
    Member

    Never mind. I figured it out. Problem was between the chair and the computer.

    #348226 Reply

    Code_A
    Member

    Ok, 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;
        };
    #348241 Reply

    Brandon
    Member

    Have you tried using the phonegap media plugin for it?
    https://github.com/apache/cordova-plugin-media/blob/dev/doc/index.md

    #348242 Reply

    Code_A
    Member

    No 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.

    #348244 Reply

    Brandon
    Member

    I 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.

    #348287 Reply

    Code_A
    Member

    Thanks 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.

    #348288 Reply

    Brandon
    Member

    An 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.

    #348291 Reply

    Code_A
    Member

    Can 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.

    #348293 Reply

    Brandon
    Member

    Yes you can. The HTML widget is actually very powerful, you can even dynamically load into it…I use it a lot.

    #348302 Reply

    Hi 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

    #348304 Reply

    Code_A
    Member

    Worked 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.

Viewing 12 posts - 1 through 12 (of 12 total)
Reply To: mp3 won’t play using javascript: CLOSED

You must be logged in to post in the forum log in