[TIMOB-17148] Android: Support "allowBackground" for Ti.Media.VideoPlayer
| GitHub Issue | n/a |
|---|---|
| Type | New Feature |
| Priority | Medium |
| Status | Closed |
| Resolution | Won't Fix |
| Resolution Date | 2015-01-07T04:52:11.000+0000 |
| Affected Version/s | Release 3.2.3 |
| Fix Version/s | n/a |
| Components | Android |
| Labels | Android, AudioPlayer, VideoPlayer, allowBackgr |
| Reporter | Fokke Zandbergen |
| Assignee | Ingo Muschenetz |
| Created | 2014-06-11T13:03:47.000+0000 |
| Updated | 2015-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)
Moving this new feature request to engineering for further evaluation and prioritization.
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.
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();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.
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.