Is it possible to play video using Avplayer in Background?

前端 未结 8 1356
清歌不尽
清歌不尽 2020-12-04 20:19

I am using Avplayer to show video clips and when i go back (app in background) video stop. How can i keep playing the video?

I have search about background task &am

相关标签:
8条回答
  • 2020-12-04 20:44

    It is not possible to play background music/video using Avplayer. But it is possible using

    MPMoviePlayerViewController. I have done this in one of my app using this player & this app

    is run successfully to appstore.

    0 讨论(0)
  • 2020-12-04 20:47

    If you try to change the background mode: Sorry, App store wont approve it.MPMoviePlayerViewController playback video after going to background for youtube

    In my research, someone would take the sound track out to play in te background when it goes into background as the video would be pause and get the playbacktime for resume playing when go into foreground

    0 讨论(0)
  • 2020-12-04 20:47

    Try with this snippet, I've already integrated this with my app & it's being useful for me..hope this will work for you!!

    Follow the steps given below:

    • Add UIBackgroundModes in the APPNAME-Info.plist, with the selection App plays audio
    • Then add the AudioToolBox framework to the folder frameworks.
    • In the APPNAMEAppDelegate.h add:

      -- #import < AVFoundation/AVFoundation.h>

      -- #import < AudioToolbox/AudioToolbox.h>

    • In the APPNAMEAppDelegate.m add the following:

      // Set AudioSession

       NSError *sessionError = nil;
      
          [[AVAudioSession sharedInstance] setDelegate:self];
      
          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
      
    • /* Pick any one of them */

    • // 1. Overriding the output audio route

    //UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; //AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

    ========================================================================

    // 2. Changing the default output audio route

    UInt32 doChangeDefaultRoute = 1;
    
    AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);
    

    into the

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    • but before the two lines:

      [self.window addSubview:viewController.view];

      [self.window makeKeyAndVisible];

    Enjoy Programming!!

    0 讨论(0)
  • 2020-12-04 20:47

    Swift version for the accepted answer.

    In the delegate:

    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
    AVAudioSession.sharedInstance().setActive(true, error: nil)
    

    In the view controller that controls AVPlayer

    override func viewDidAppear(animated: Bool) {
        UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
        self.becomeFirstResponder()
    }
    
    override func viewWillDisappear(animated: Bool) {
        mPlayer.pause()
        UIApplication.sharedApplication().endReceivingRemoteControlEvents()
        self.resignFirstResponder()
    }
    

    Don't forget to "import AVFoundation"

    0 讨论(0)
  • 2020-12-04 20:48

    In addition to fattomhk's response, here's what you can do to achieve video forwarding to the time it should be after your application comes in foreground:

    • Get currentPlaybackTime of playing video when go to background and store it in lastPlayBackTime
    • Store the time when application goes to background (in userDefault probably)
    • Again get the time when application comes in foreground
    • Calculate the duration between background and foreground time
    • Set current playback time of video to lastPlayBackTime + duration
    0 讨论(0)
  • 2020-12-04 20:54

    I'd like to add something that for some reason ended up being the culprit for me. I had used AVPlayer and background play for a long time without problems, but this one time I just couldn't get it to work.

    I found out that when you go background, the rate property of the AVPlayer sometimes seems to dip to 0.0 (i.e. paused), and for that reason we simply need to KVO check the rate property at all times, or at least when we go to background. If the rate dips below 0.0 and we can assume that the user wants to play (i.e. the user did not deliberately tap pause in remote controls, the movie ended, etc) we need to call .play() on the AVPlayer again.

    AFAIK there is no other toggle on the AVPlayer to keep it from pausing itself when app goes to background.

    0 讨论(0)
提交回复
热议问题