Code Monkey home page Code Monkey logo

Comments (7)

andreipitis avatar andreipitis commented on May 24, 2024

Hi @DrewBourgeois, I just released version 4.1.0 which adds the functionality you need. If you are using ASPVideoPlayer set the configuration property with allowBackgroundPlay: true.

from aspvideoplayer.

DrewBourgeois avatar DrewBourgeois commented on May 24, 2024

Thanks so much!

from aspvideoplayer.

DrewBourgeois avatar DrewBourgeois commented on May 24, 2024

Hi @andreipitis, I'm trying to have the background audio of a video asset controllable through MPRemoteCommandCenter at the lock screen, but it doesn't seem to work as videoPlayerLayer.player = nil when it enters the background. Do you have any tips on how to make this happen? Thanks!

from aspvideoplayer.

andreipitis avatar andreipitis commented on May 24, 2024

Hi @DrewBourgeois,

The player object needs to be removed from the videoPlayerLayer in order to enable background playback. This does not deallocate the player, there is still a strong reference to it and all the functions to control playback should still work.

You should be able to control the playback with methods like playVideo(), pauseVideo() if you are using ASPVideoPlayerView.

If you are using ASPVideoPlayer you can control playback by accessing the videoPlayerControls property:

//First method
let controls = videoPlayer.videoPlayerControls as? ASPVideoPlayerControls
controls?.pause()

//Second method
let player = videoPlayer.videoPlayerControls.videoPlayer
player?.pauseVideo()

Did you enable Background Modes for audio playback in Capabilities?
I can only speculate without seeing the actual code.

from aspvideoplayer.

DrewBourgeois avatar DrewBourgeois commented on May 24, 2024

Hi @andreipitis,

Thanks for that!

The command center controls don't control the playback when the app is in the background and videoPlayerLayer.player = nil is in appDidEnterBackground. The command center controls work fine when the app is in the foreground, or videoPlayerLayer.player = nil is taken out of appDidEnterBackground and the screen is locked.

The following is in ASPVideoPlayerView:

private func commonInit() {
 UIApplication.shared.beginReceivingRemoteControlEvents()
        let commandCenter = MPRemoteCommandCenter.shared()
        
        commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
            self.pauseVideo()
            return .success
        }
        
        commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
            self.playVideo()
            return .success
        }
}

My AVAudio session is set to AVAudioSessionCategoryPlayback and background audio capabilities are set.

Thanks!

from aspvideoplayer.

andreipitis avatar andreipitis commented on May 24, 2024

@DrewBourgeois I tried your code and it seems to be working fine in my case. I can put the app in the background while continuing to play audio and I can pause/resume it from command center.

In my case, the MPRemoteCommandCenter setup was done in a ViewController that had a reference to the player as to not modify the component code. Maybe this change can work for you as well.

UIApplication.shared.beginReceivingRemoteControlEvents() is not necessary when using MPRemoteCommandCenter according to the documentation, but if it still doesn't work, you could try moving it in AppDelegate:

    func applicationDidEnterBackground(_ application: UIApplication) {
        UIApplication.shared.beginReceivingRemoteControlEvents()
    }

from aspvideoplayer.

DrewBourgeois avatar DrewBourgeois commented on May 24, 2024

Hi @andreipitis I got the code working the way I want.

Thanks so much for your help, and also for developing a really great video player!

from aspvideoplayer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.