如何在ios中使用媒体音量而不是铃声音量来播放声音?

时间:2017-01-05 19:40:22

标签: ios swift avaudioplayer

我正在尝试播放声音,但我当前的代码将音量调节到铃声音量。如何将其更改为媒体卷?

var player: AVAudioPlayer?

...

func playSound(name: String) {
        let url = Bundle.main.url(forResource: name, withExtension: "mp3")!

        do {
            player = try AVAudioPlayer(contentsOf: url)
            guard let player = player else { return }

            player.prepareToPlay()
            player.play()
        } catch let error {
            print(error.localizedDescription)
        }
    }

...

playSound(name: "baby")

2 个答案:

答案 0 :(得分:0)

仅在将一些URL放入播放器之前进行设置。

-n

答案 1 :(得分:-1)

要使用媒体音量播放声音,您应该这样做:

func play(){

        let path = Bundle.main.path(forResource: "Plop", ofType: "mp3")!
        let url = URL(fileURLWithPath: path)

        do {
            let sound = try AVAudioPlayer(contentsOf: url)
            bombSoundEffect = sound
            sound.play()
        } catch {
            // couldn't load file :(
        }
    }