Search code examples
iosswiftavplayer

How to set volume for AVPlayer


I'm trying to set volume for the AVPlayer but it doesn't seem to work. It takes system volume and ignores the value set in the code. Following is my code. How do we set a volume level?

let player = AVPlayer(url: URL(string:recordingFileURL)!)
let playerController = AVPlayerViewController()
playerController.player = player
playerController.videoGravity = AVLayerVideoGravity(rawValue: AVLayerVideoGravity.resizeAspectFill.rawValue)

self.present(playerController, animated: true) {
    player.play()
    player.volume = 0.8 // Doesn't have any affect
}

Solution

  • Apple Docs says this isn't possible.

    This property is used to control the player audio volume relative to the system volume. There is no programmatic way to control the system volume in iOS, but you can use the MediaPlayer framework’s MPVolumeView class to present a standard user interface for controlling system volume.

    Since the AVPlayer volume is relative to the system volume, you can never force AVPlayer to play louder than the system volume.