Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-17148] Android: Support "allowBackground" for Ti.Media.VideoPlayer

GitHub Issuen/a
TypeNew Feature
PriorityMedium
StatusClosed
ResolutionWon't Fix
Resolution Date2015-01-07T04:52:11.000+0000
Affected Version/sRelease 3.2.3
Fix Version/sn/a
ComponentsAndroid
LabelsAndroid, AudioPlayer, VideoPlayer, allowBackgr
ReporterFokke Zandbergen
AssigneeIngo Muschenetz
Created2014-06-11T13:03:47.000+0000
Updated2015-01-07T04:52:11.000+0000

Description

The Ti.Media.AudioPlayer has a property [allowBackground](http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.Media.AudioPlayer-property-allowBackground) to allow the audio not to be paused with the activity is paused, as implemented in [AudioPlayerProxy.java on line #226](https://github.com/appcelerator/titanium_mobile/blob/e7334c98aaff370d5bbfa4bfe08656726d43baf9/android/modules/media/src/java/ti/modules/titanium/media/AudioPlayerProxy.java#L226). Unfortunately, Ti.Media.VideoPlayer does not have such a property. Though it might sound logical to pause the audio when the user can't see the video anymore, there are use cases where this is not the desired behaviour. Take the TED app, where the audio continues as well since the video is just to support the audio. It seems to me this could be easily fixed in a similar way in [VideoPlayerProxy.java around line #668](https://github.com/appcelerator/titanium_mobile/blob/1b98dcd8c45953e5d5e93805594472ea6a9bd23e/android/modules/media/src/java/ti/modules/titanium/media/VideoPlayerProxy.java#L668)

Comments

  1. Ritu Agrawal 2014-06-12

    Moving this new feature request to engineering for further evaluation and prioritization.
  2. Ashraf Abu 2015-01-02

    I looked into this and found out the following:- Ti.Media.VideoPlayer in Android uses VideoPlayerProxy that uses [TiVideoView8 | https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/media/src/java/android/widget/TiVideoView8.java] to play the video. When the Activity is sent to background, the TiVideoView8 has a [surfaceDestroyed | https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/media/src/java/android/widget/TiVideoView8.java#L630] method that destroys the view. This happens when the Activity holding the TiViewView8 goes to onPause (aka Background). The [surfaceDestroyed | https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/media/src/java/android/widget/TiVideoView8.java#L637] also stops and release the MediaPlayer that is used to play the music and show the video. Hence, it is not similar to the AudioPlayerProxy as the AudioPlayerProxy had no View objects being used. Implementing this for the VideoPlayerProxy would not be the same.
  3. Ashraf Abu 2015-01-02

    Workaround in Javascript for Android. This uses the AudioPlayer to run in the background when the App goes to background.
       var vidWin = Titanium.UI.createWindow({
           title : 'Video View Demo',
           backgroundColor : '#fff'
       });
       
       var videoPlayer = Titanium.Media.createVideoPlayer({
           top : 2,
           autoplay : true,
           backgroundColor : 'blue',
           allowBackground: true,
           height : 300,
           width : 300,
           mediaControlStyle : Titanium.Media.VIDEO_CONTROL_DEFAULT,
           scalingMode : Titanium.Media.VIDEO_SCALING_ASPECT_FIT
       });
       
       var currentPlaybackTime = 0;
       
       var basicSwitch = Ti.UI.createSwitch({
         titleOn:'On BG',
         titleOff:'Off BG',
         value:true,
         width: 200, height:120
       });
       
       var urlToPlay = 'rtsp://r7---sn-a5m7zu76.c.youtube.com/CiILENy73wIaGQlD0rwH11uU1RMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp';
       
       videoPlayer.url = urlToPlay;
       vidWin.add(videoPlayer);
       vidWin.add(basicSwitch);
       
       // allowBackground: true on Android allows the 
       // player to keep playing when the app is in the 
       // background.
       var audioPlayer = Ti.Media.createAudioPlayer({ 
           url: urlToPlay,
           allowBackground: true
       });  
       
       vidWin.addEventListener('blur', function() {
       	Ti.API.info('Closing VideoPlayer at Time Played: ' + Math.round(videoPlayer.getCurrentPlaybackTime()) + ' milliseconds');
       	if(basicSwitch.getValue()) {
       		currentPlaybackTime = videoPlayer.getCurrentPlaybackTime();
       		audioPlayer.setTime(currentPlaybackTime);
       		audioPlayer.start();
       	}
       });
       
       vidWin.addEventListener('focus', function() {
       	Ti.API.info('Closing AudioPlayer at Time Played: ' + Math.round(audioPlayer.getTime()) + ' milliseconds');
       	if(basicSwitch.getValue()) {
       		currentPlaybackTime = audioPlayer.getTime();
       	    audioPlayer.stop();
       	    audioPlayer.release();
       		videoPlayer.setCurrentPlaybackTime(currentPlaybackTime);
       	}
       });
       
       audioPlayer.addEventListener('complete', function() {
           Ti.API.info('Audio ended at: ' + Math.round(audioPlayer.getTime()) + ' milliseconds');
       });
       
       vidWin.open();
       
       
  4. Chee Kiat Ng 2015-01-07

    Closing this ticket and won't be including this in the SDK, considering that it is not of common use case. Please use Ashraf's JS workaround if need be. Will be considered for the SDK in the future if ticket gets more support.
  5. Chee Kiat Ng 2015-01-07

    Closing this ticket and won't be including this in the SDK, considering that it is not of common use case. Please use Ashraf's JS workaround if need be. Will be considered for the SDK in the future if ticket gets more support.

JSON Source