Titanium Community Questions & Answer Archive

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

Audio streaming in background

Hi,

I have an audioplayer playing a shoutcast stream and when the device goes into sleep mode (or locked), or into multitasking the audio fades out and stops?? any ideas how to stop this? Ti v1.3.2 SDK 4.0

— asked July 1st 2010 by Toby Hughes
  • iphone
1 Comment
  • You can just set allowBackground: true on Android allows the player to keep playing when the app is in the background. Further methods are listed on snippet below:

    var win = Titanium.UI.createWindow({  
        title:'Audio Test',
        backgroundColor:'#fff',
        layout: 'vertical'
    });
    
    var startStopButton = Titanium.UI.createButton({
        title:'Start/Stop Streaming',
        top:10,
        width:200,
        height:40
    });
    
    var pauseResumeButton = Titanium.UI.createButton({
        title:'Pause/Resume Streaming',
        top:10,
        width:200,
        height:40,
        enabled:false
    });
    
    win.add(startStopButton);
    win.add(pauseResumeButton);
    
    // allowBackground: true on Android allows the 
    // player to keep playing when the app is in the 
    // background. 
    var audioUrl = 'http://broadcast.infomaniak.net:80/energyzuerich-high.mp3';
    var audioPlayer = Ti.Media.createAudioPlayer({ 
        url: audioUrl,
        allowBackground: true
    });           
    
    startStopButton.addEventListener('click',function() {
        // When paused, playing returns false.
        // If both are false, playback is stopped.
        if (audioPlayer.playing || audioPlayer.paused)
        {
            audioPlayer.stop();
            pauseResumeButton.enabled = false;
            if (Ti.Platform.name === 'android')
            { 
                audioPlayer.release();
            }   
        }
        else
        {
            audioPlayer.start();
            pauseResumeButton.enabled = true;
        }
    });
    
    pauseResumeButton.addEventListener('click', function() {
        if (audioPlayer.paused) {
            audioPlayer.start();
        }
        else {
            audioPlayer.pause();
        }
    });
    
    audioPlayer.addEventListener('progress',function(e) {
        Ti.API.info('Time Played: ' + Math.round(e.progress) + ' milliseconds');
    });
    
    audioPlayer.addEventListener('change',function(e)
    {
        Ti.API.info('State: ' + e.description + ' (' + e.state + ')');
    });
    
    win.addEventListener('close',function() {
        audioPlayer.stop();
        if (Ti.Platform.osname === 'android')
        { 
            audioPlayer.release();
        }
    });
    
    win.open();
    
    — commented July 22nd 2013 by Eduardo Gomez

0 Answers

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.