Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-20093] Android Audio Player - playing in the background

GitHub Issuen/a
TypeImprovement
Priorityn/a
StatusOpen
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsn/a
ReporterShawn Lan
AssigneeUnknown
Created2015-08-26T23:51:47.000+0000
Updated2017-09-21T21:17:13.000+0000

Description

If allowBackground set to true, the audio player should be started as a foreground service. See [here](http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices) The benefits are: 1. The audio player keeps playing even after the app exits. 2. The system won't accidentally purge the audio player out of memory. Please implement the audio player based on the entire section "Using a Service with MediaPlayer" from the docs above. It suggests not only foreground service, but also several tips for a good background audio player. Thanks.

Comments

  1. Rakhi Mitro 2015-09-21

    Hello, Tested this issue.Android Audio Player is playing as expected in background. *Test Environments:* Appc CLI Version :5.0.1 Titanium SDK Version5.0.0.GA Device:Nexus 7,5.1.1 Appc Studio: Appcelerator Studio, build: 4.3.0.201509151348 Alloy: 1.7.6 Mac OS X,Version = 10.10.1. *Test Steps*: * Create a classic project * Paste sample code to resource directory * Run project * Press *home* in device and check Android Audio Player playing or not in the background. *Test code:* *app.js*
       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 audioPlayer = Ti.Media.createAudioPlayer({ 
           url: 'song.mp3',
           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();
       
    *Test Result:* Audio Player is working as expected in the background. *Ti logs:*
       
       [INFO] :   APSAnalyticsService: Stopping Analytics Service
       [INFO] :   Time Played: 21342 milliseconds
       
       [INFO] :   Time Played: 22335 milliseconds
       
       [INFO] :   Time Played: 23353 milliseconds
       
       [INFO] :   Time Played: 24320 milliseconds
       
       [INFO] :   Time Played: 25339 milliseconds
       
       [INFO] :   Time Played: 26331 milliseconds
       
       [INFO] :   Time Played: 27350 milliseconds
       
       [INFO] :   Time Played: 28317 milliseconds
       
       [INFO] :   Time Played: 29336 milliseconds
       
       [INFO] :   Time Played: 30354 milliseconds
       
       [INFO] :   Time Played: 31347 milliseconds
       
       [INFO] :   Time Played: 32366 milliseconds
       
       [INFO] :   Time Played: 33437 milliseconds
       
       [INFO] :   Time Played: 34456 milliseconds
       
       [INFO] :   Time Played: 35500 milliseconds
       
       [INFO] :   Time Played: 36519 milliseconds
       
       [INFO] :   Time Played: 37590 milliseconds
       
       [INFO] :   Time Played: 38609 milliseconds
       
       [INFO] :   Time Played: 39680 milliseconds
       
       [INFO] :   Time Played: 40699 milliseconds
       
       [INFO] :   Time Played: 41691 milliseconds
       
       [INFO] :   Time Played: 42710 milliseconds
       
       [INFO] :   Time Played: 43729 milliseconds
       
       [INFO] :   Time Played: 44800 milliseconds
       
       [INFO] :   Time Played: 45819 milliseconds
       
       [INFO] :   Time Played: 46864 milliseconds
       
       [INFO] :   Time Played: 47882 milliseconds
       
       [INFO] :   Time Played: 48953 milliseconds
       
       [INFO] :   Time Played: 49972 milliseconds
       
       [INFO] :   Time Played: 50991 milliseconds
       
       [INFO] :   Time Played: 51984 milliseconds
       
       [INFO] :   Time Played: 53002 milliseconds
       
       [INFO] :   Time Played: 54021 milliseconds
       
       [INFO] :   Time Played: 55092 milliseconds
       
       [INFO] :   Time Played: 56163 milliseconds
       
       [INFO] :   Time Played: 57182 milliseconds
       
       [INFO] :   Time Played: 58175 milliseconds
       
       [INFO] :   Time Played: 59246 milliseconds
       
       [INFO] :   Time Played: 60264 milliseconds
       
       [INFO] :   Time Played: 61336 milliseconds
       
       [INFO] :   Time Played: 62354 milliseconds
       
       [INFO] :   APSAnalyticsService: Analytics Service Started
       
       [INFO] :   Time Played: 63347 milliseconds
       
       [INFO] :   Time Played: 64366 milliseconds
       
       [INFO] :   Time Played: 65384 milliseconds
       
       [INFO] :   APSAnalyticsService: Stopping Analytics Service
       
       [INFO] :   Time Played: 66403 milliseconds
       
       [INFO] :   Time Played: 67422 milliseconds
       
       [INFO] :   Time Played: 68467 milliseconds
       
       [INFO] :   Time Played: 69486 milliseconds
       
       [INFO] :   Time Played: 70557 milliseconds
       
       [INFO] :   Time Played: 71576 milliseconds
       
       [INFO] :   Time Played: 72620 milliseconds
       
       [INFO] :   Time Played: 73639 milliseconds
       
       [INFO] :   Time Played: 74658 milliseconds
       
       [INFO] :   Time Played: 75729 milliseconds
       
       [INFO] :   Time Played: 76696 milliseconds
       
       [INFO] :   Time Played: 77740 milliseconds
       
       [INFO] :   Time Played: 78759 milliseconds
       
       [INFO] :   Time Played: 79778 milliseconds
       
       [INFO] :   Time Played: 80849 milliseconds
       
       [INFO] :   Time Played: 81868 milliseconds
       
       [INFO] :   Time Played: 82860 milliseconds
       
       [INFO] :   Time Played: 83879 milliseconds
       
       [INFO] :   Time Played: 84950 milliseconds
       
       [INFO] :   Time Played: 85969 milliseconds
       
       [INFO] :   Time Played: 87040 milliseconds
       
       [INFO] :   Time Played: 88059 milliseconds
       
       [INFO] :   Time Played: 89051 milliseconds
       
       [INFO] :   Time Played: 90122 milliseconds
       
       [INFO] :   Time Played: 91141 milliseconds
       
       [INFO] :   Time Played: 92160 milliseconds
       
       [INFO] :   Time Played: 93179 milliseconds
       
       [INFO] :   Time Played: 94224 milliseconds
       
       [INFO] :   Time Played: 95242 milliseconds
       
       [INFO] :   Time Played: 96313 milliseconds
       
       [INFO] :   Time Played: 97332 milliseconds
       
       [INFO] :   Time Played: 98403 milliseconds
       
       [INFO] :   Time Played: 99422 milliseconds
       
       [INFO] :   Time Played: 100467 milliseconds
       
       [INFO] :   Time Played: 101486 milliseconds
       
       [INFO] :   Time Played: 102557 milliseconds
       
       [INFO] :   Time Played: 103576 milliseconds
       
       [INFO] :   Time Played: 104620 milliseconds
       
       [INFO] :   Time Played: 105639 milliseconds
       
       [INFO] :   Time Played: 106318 milliseconds
       
       
    Thanks
  2. Shawn Lan 2015-11-12

    Yes the audio player can play in the background, but it's not as a "foreground service." Please see the Android documentation included in the description. This is not a fix request. This is an improvement request.
  3. Henry Orozco 2017-09-21

    Using a Service with the AudioPlayer is very useful to avoid stop the background audio playback when the Activity gets killed by Android. Please consider support that functionality on the AudioPlayer, I'm using SDK 6.2.2 on my app and every time the Activity of my app is killed the audio playback stops. Thank you!

JSON Source