IOS avplayer Background playback problem auto stop problem prevent app from being suspended by background2016-09-08 16:16 1597 people read comments (0) favorite reports Classification:IOS Development Notes (PNS)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Directory (?) [+]
1. Create avaudiosession when creating player
[OBJC]View PlainCopy
- Avaudiosession *session = [Avaudiosessionsharedinstance];
- [Session Setcategory:avaudiosessioncategoryplaybackerror: nil];
- [Session setActive:YES error: nil];
2. Add a field to the Plist file
Required Background modes
To add a background play here:
APP plays audio or streams audio/video using AirPlay
3. Add the following code when the device will suspend the app
[OBJC]View PlainCopy
- -(void) applicationwillresignactive: (uiapplication *) application {
- if ([Mediaplayerplayer]. playstatus = = mediaplayerplaystatusplaying) {
- uidevice* device = [Uidevicecurrentdevice];
- if ([Devicerespondstoselector:@selector (ismultitaskingsupported)]) {
- if (device. multitaskingsupported) {
- if (device. multitaskingsupported) {
- if ([Mediaplayerplayer]. Bgtaskid ==uibackgroundtaskinvalid) {
- [Mediaplayerplayer] . bgtaskid = [[Uiapplicationsharedapplication]beginbackgroundtaskwithexpirationhandler:NULL];
- }
- }
- }
- }
- }
- }
Add the following code when the device enters the foreground
[OBJC]View PlainCopy
- if ([Mediaplayerplayer]. Bgtaskid!=uibackgroundtaskinvalid) {
- [[Uiapplicationsharedapplication]endbackgroundtask:[mediaplayerplayer]. Bgtaskid];
- [Mediaplayerplayer] . Bgtaskid =uibackgroundtaskinvalid;
- }
Using these three steps will basically guarantee long-time playback problems in the background, and will not cause the playback to stop because the app hangs in the background.
IOS avplayer Background playback problem auto stop problem prevent app from being suspended by background