DaveMS
DaveMS

Reputation: 157

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
}

Upvotes: 2

Views: 9251

Answers (4)

Rajesh SilverSky
Rajesh SilverSky

Reputation: 21

after giving player.volume = 100 it made an effect

Upvotes: 0

kashish makkar
kashish makkar

Reputation: 159

player.volume = 0.8

This property is most useful on iOS to control the volume of the AVSampleBufferAudioRenderer relative to other audio output, not for setting absolute volume.

Upvotes: 5

matt
matt

Reputation: 534885

You said in a comment:

My assumption was that the value set in above code should overwrite the system volume's value

That assumption is wrong. If the system volume is 0.2 and you set the player's volume to 0.8, all you're doing is making the player even softer (0.16). As the documentation tells you:

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.

Upvotes: 5

dsringari
dsringari

Reputation: 134

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.

Upvotes: 7

Related Questions