Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Android issues: mp3 stream createAudioPlayer

Ti SDK 1.3.0
Android 2.1 update

Audio issue: Audio does not update url.
Loading a mp3 to stream using createAudioPlayer. using setUrl() or prop url should change the audio stream.
The url shows a change but initial audio still loads and plays.

//... load xml, place objects and trigger setData()

var sound = Ti.Media.createAudioPlayer();
sound.addEventListener('change', function(e){
    if(sound.state == 3){
     playBtn.image = imgPause;
         actInd.hide();
    }
    else{
        playBtn.image = imgPlay;
    }
});
sound.addEventListener('error', function(e){
     // an error is always detected. 
     //"Unknown media error" is the one when no audio plays
     if(e.message == "Unknown media error"){
         alert("Error Playing Audio, Skipping: " + e.message);
         actInd.hide();
         nextSong();
    }
});

sound.addEventListener('complete', function(){ nextSong(); });



sound.addEventListener('resume', function(){
    Titanium.API.info('Resume ');
});

sound.addEventListener('progress', function(e){
    //Titanium.API.info('Time Played: ' + Math.round(e.progress) + ' seconds' + " " + sound.state + " " + sound.paused + " " + sound.duration);
    actInd.hide();
});


function setData(d){
    // OMMITTED CODE 
    // set vars from passed in data

    loadAudio(audioUrl);

    artistTxt.text = artist;
    titleTxt.text = title;
    nextTxt.text = "Next: " + d.item(currentSong+1).getElementsByTagName("artist").item(0).childNodes.item(0).nodeValue + " \"" + d.item(currentSong+1).getElementsByTagName("title").item(0).childNodes.item(0).nodeValue+"\"";
}

function loadAudio(s){
    actInd.show();
    actInd.message = "Loading Audio...";
    sound.stop();

    sound.allowBackground = true;
    sound.setUrl(s);
    sound.url = s;

    sound.start();
}

The above would only play one mp3 regardless of how I set the url. So I tried to create a new audio player (below code) for each new song by overwriting the sound object. The issue here is the audio does not play consistent. I get onEvent errors for each load. Sometimes 2, sometimes 3. Some are "Unknown media issue" or "Unknown media error". The "Unkown media issue" error would play. But the Unknown media error would never play. Any ideas?

// only change is position of sound listeners and create new audio for every load.
var sound = Ti.Media.createAudioPlayer();

function loadAudio(s){
    actInd.show();
    actInd.message = "Loading Audio...";
    sound.stop();

    sound = Titanium.Media.createAudioPlayer({url:s,preload:true,allowBackground:true});
    sound.addEventListener('change', function(e){
        if(sound.state == 3){
         playBtn.image = imgPause;
             actInd.hide();
        }
        else{
            playBtn.image = imgPlay;
        }
    });
    sound.addEventListener('error', function(e){
         // an error is always detected. 
         //"Unknown media error" is the one when no audio plays
         if(e.message == "Unknown media error"){
             alert("Error Playing Audio, Skipping: " + e.message);
             actInd.hide();
             nextSong();
        }
    });

    sound.addEventListener('complete', function(){ nextSong(); });



    sound.addEventListener('resume', function(){
        Titanium.API.info('Resume ');
    });

    sound.addEventListener('progress', function(e){
        //Titanium.API.info('Time Played: ' + Math.round(e.progress) + ' seconds' + " " + sound.state + " " + sound.paused + " " + sound.duration);
        actInd.hide();
    });

    sound.start();
}

Is this a bug for android or am I doing something wrong?

— asked July 26th 2010 by Khary Mallea
  • android
  • audio
  • createaudioplayer
  • mp3
2 Comments
  • BUMP Any workarounds?

    — commented October 9th 2010 by Ben Hornedo
  • hi all,
    seems this hasn't changed in SDK 1.6.1 (android 2.1), setUrl() only works initially. is there any workaround (except recreating the audioplayer)? so many great functions in Ti, but when you want to use them as described in the docs, it often becomes difficult, IMHO…

    — commented April 2nd 2011 by u no

4 Answers

  • I worked around this by creating a new AudioPlayer for each audio clip played.

    if (audioPlayer) {
        audioPlayer.stop();
        audioPlayer = null;
    }
    audioPlayer = Titanium.Media.createAudioPlayer({});
    audioPlayer.addEventListener('change', playbackStateChange);
    audioPlayer.addEventListener('progress', progressChange);
    audioPlayer.setUrl(url);
    audioPlayer.start();
    
    — answered October 15th 2010 by Nick Robillard
    permalink
    2 Comments
    • But creating a new AudioPlayer also has another issue..

      Kindly look at my question in http://developer.appcelerator.com/question/118302/android–audio-stops-in-navigation

      Do anyone have a fix for this..

      — commented April 18th 2011 by Primoris online
    • And also the event listeners created initially for sound (audioplayer) is not called after creating the audioplayer in the same name.

      — commented May 9th 2011 by Primoris online
  • 'progress' fails on Andriod but rounds properly on iPhone.
    4.123 becomes 4 on iPhone when rounded but 4123 on Android.

     Math.round(e.progress)
    
    — answered May 9th 2011 by Chris Flowers
    permalink
    0 Comments
  • how is this progressing? any solutions so far?

    — answered July 29th 2011 by vincent youmans
    permalink
    0 Comments
  • Hi all! I am creating an app that plays various voiceover sounds. Each sound must wait for the last one to finish before it is played. This works fine on the iPhone, but on Android it clips off the sounds. I am using Ti.Media.createSound instead of createAudioPlayer. Does this matter? Any clues?

    — answered July 13th 2012 by Amy H
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.