IOS avplayer Background playback problem auto stop problem prevent app from being suspended by background

Source: Internet
Author: User
Tags vars

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
    1. Avaudiosession *session = [Avaudiosessionsharedinstance];
    2. [Session Setcategory:avaudiosessioncategoryplaybackerror: nil];
    3. [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
  1. -(void) applicationwillresignactive: (uiapplication *) application {
  2. if ([Mediaplayerplayer]. playstatus = = mediaplayerplaystatusplaying) {
  3. uidevice* device = [Uidevicecurrentdevice];
  4. if ([Devicerespondstoselector:@selector (ismultitaskingsupported)]) {
  5. if (device. multitaskingsupported) {
  6. if (device. multitaskingsupported) {
  7. if ([Mediaplayerplayer]. Bgtaskid ==uibackgroundtaskinvalid) {
  8. [Mediaplayerplayer]  . bgtaskid = [[Uiapplicationsharedapplication]beginbackgroundtaskwithexpirationhandler:NULL];
  9. }
  10. }
  11. }
  12. }
  13. }
  14. }

Add the following code when the device enters the foreground

[OBJC]View PlainCopy
    1. if ([Mediaplayerplayer]. Bgtaskid!=uibackgroundtaskinvalid) {
    2. [[Uiapplicationsharedapplication]endbackgroundtask:[mediaplayerplayer]. Bgtaskid];
    3. [Mediaplayerplayer]  . Bgtaskid =uibackgroundtaskinvalid;
    4. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.