Prevent background sound suppression when using AVPlayer

I am playing a video using an instance of AVPlayer, and I want to continue listening to background music while playing the video.

If any background application plays music, the music is turned off every time it is called playon AVPlayer. How can I turn off background sound?

This is how I create and run my AVPlayer:

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];

playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
[self.layer addSublayer:playerLayer];

// mutes all background audio
[player play];
+4
source share
1 answer

I was able to solve this problem by following these steps in the AppDelegate.swift class:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    } catch {

    }
    return true
}

, iTunes, , , .

0

Source: https://habr.com/ru/post/1629482/


All Articles