Let iPhone audio play while AVPlayer plays video in my application? - ios

In my application I am playing a video like so:
let url = NSBundle.mainBundle().URLForResource("myVideo", withExtension: "mp4")
let videoPlayer = AVPlayer(URL: url)
videoPlayer.play()
When the video plays any other audio from other applications stops.
For example, I am listening to iTunes Radio and that stops playing when this code is run.
How can I allow this video to play without interfering with other audio sources on the phone?
I want to be able to play the video an also listen to iTunes Radio. Main reason is because my video in my app has no volume....
Thanks

Related

AVPlayer on Swift app shows a black screen but plays audio when playing video

New to Swift! I am trying to have the app play a video from my Resources upon pressing a button. I have this method in my ViewController:
if let path = Bundle.main.path(forResource: "Cypress", ofType: "mov") {
let player = AVPlayer(url: URL(fileURLWithPath: path))
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
// Modally present the player and call the player's play() method when complete.
self.present(controller, animated: true) {
player.play()
}
}
However, when the video begins to play, it is a black screen but the audio is playing properly. Are there any quick fixes to this? I am using Xcode version 12.2. Any advice would be greatly appreciated thanks :)
The problem is with the video file itself / the encoding.
Deceptively, the Quicktime ".mov" format is not a video codec, but rather audio/video container format that can contain video and audio (and some other things) in any number of compression codecs (h.264, mpeg2, ProRes, MJPEG, AAC, mp3, etc.) … Your files don't work because they include video compressed with a codec which iOS does not support.
From #alexkent in this SO answer on another post.
I don't think a .mov is necessarily not going to work for you, but you'd have to make sure it was encoded in a way that is iOS-compatible. So as you mentioned in the comments, using .mp4 ensures the video is encoded in an iOS-compatible fashion.

What happens when you play an AVAsset from a remote url?

I'm confused on how an AVAsset gets and plays video from a remote url. I have looked at the documentation but am still a little bewildered by AVAssets, items, and players.
The main questions that I have are
Does AVKit automatically manage downloading while playing or does the entire video need to be downloaded in order to play from a remote url?
If I played back the asset in a looping fashion, would it download the video again and again?
let video = AVAsset(url: "https://www.w3schools.com/html/mov_bbb.mp4")
let item = AVPlayerItem(asset: video)
player.replaceCurrentItem(with: item)
player.automaticallyWaitsToMinimizeStalling = false
player.play()

AVPlayer - iOS 13 - Long form video - No sound after unmuting device connected to an AppleTV

I have a video player app with the iOS 13 AirPlay optimisation "Long form video" active (set in Info.plist).
Somewhere in that app, I need an (default muted) inline AVPlayer that the user can unmute.
However, when the device is connected to an Apple TV, the AVPlayer with allowsExternalPlayback = false and isMuted = true, does not have any audio after unmuting (isMuted = false) the player, not on the device nor on the tv.
Disconnecting AirPlay brings back the audio on the speaker.
Fun quirk: When the player is initially muted, the audio is playing on the TV.
When unmuted, no audio is playing anywhere.
The audio session is configured as follows (after unmuting):
try audioSession.setCategory(.playback,
mode: .moviePlayback,
options: [])
try audioSession.setActive(true, options: [])
My goal is to have the muted AVPlayer play the audio on the speaker when it's unmuted and ignore AirPlay all together.

Swift - Playing Audio file and Itunes Music simultaneously

I am currently building a game with swift and spritekit. I want to play a sound in a certain part of the code. Currently I am using AVAudioPlayer. I am currently using this method.
player = try AVAudioPlayer(contentsOfURL: url)
player.prepareToPlay()
player.play()
The sound plays correctly, but if I were to be listening to music through itunes while the sound is played my itunes music is stopped and the sound is played. I have played other people's games in the past where ingame sound effects play and do not effect music that was already playing from another app. Can somone explain how this is achieved.Thanks a bunch.
You'll need to set your audio session to use a category that allows mixing. By default it uses AVAudioSessionCategorySoloAmbient which is non mixable.
I don't know which category will fit your needs but AVAudioSessionCategoryAmbient allows mixing.
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)

Enable the background music back after the app plays a video

I have an app that at the beginning plays a video with AVPlayer. The video is muted. The problem is it stops the music that let say iPhone was playing on iTunes and after the video playback is finished the music does not come back automatically. Is there a way to play the video in my app, during the playback of the video the music on iTunes can be muted which is fine with me, but what I want to achieve is to get the iTunes music back playing when the video is finished without the user's interference.

Resources