音量不断重置为不同的音频 - ios swift

时间:2017-04-19 11:16:42

标签: ios iphone swift xcode

我有一个播放3种不同音频的应用。我有一个音量控制滑块。 对于单个音频,它工作正常。

但是,如果我更改音频,则音量会自动变为最大。这是流程:

  1. 播放音频1.
  2. 我使用滑块减小音量并使其接近零(不是零,但体积非常小)。
  3. 现在我选择音频2。
  4. 音量应该很低。但它又变得最大。
  5. 这是代码:

    @IBAction func volumeSlidercontroller(_ sender: UISlider) {
    
        player.volume = volumeSlider.value;
    

    我包含滑块值的截图。

    slider description

    这是播放器的代码:

    @IBAction func music1(_ sender: UIButton) {
    
    
            let path = Bundle.main.path(forResource: "fire1", ofType: "mp3")
            let filePath = NSURL(fileURLWithPath:path!)
            player = try! AVAudioPlayer.init(contentsOf: filePath as URL)
            player.numberOfLoops = -1 //logic for infinite loop
            player.prepareToPlay()
            player.play()
    
    
            let audioSession = AVAudioSession.sharedInstance()
            try!audioSession.setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers)
    
            UserDefaults.standard.set("fire1", forKey: "music")
            toggleButton(button: music01, onImage: #imageLiteral(resourceName: "music select"), offImage:  #imageLiteral(resourceName: "music select"))
            toggleButton(button: music02, onImage: #imageLiteral(resourceName: "music unselect"), offImage: #imageLiteral(resourceName: "music unselect"))
    
            toggleButton(button: music03, onImage: #imageLiteral(resourceName: "music unselect"), offImage: #imageLiteral(resourceName: "music unselect"))
            let defaults = UserDefaults.standard
            defaults.set(true, forKey: "InstructionsButtonIsHidden")
    
    //            let sounddefaults = UserDefaults.standard
    //            player.pause()
            onoffLabel.setTitle("on", for: .normal)
    
    //            sounddefaults.set(true, forKey: "soundMute")
    
    
    
    
    
    }
    
    @IBAction func music2(_ sender: UIButton) {
        let path = Bundle.main.path(forResource: "fire2", ofType: "mp3")
        let filePath = NSURL(fileURLWithPath:path!)
        player = try! AVAudioPlayer.init(contentsOf: filePath as URL)
        player.numberOfLoops = -1 //logic for infinite loop
        player.prepareToPlay()
        player.play()
    
    
        let audioSession = AVAudioSession.sharedInstance()
        try!audioSession.setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers)
    
    
    
        UserDefaults.standard.set("fire2", forKey: "music")
        toggleButton(button: music02, onImage: #imageLiteral(resourceName: "music select"), offImage: #imageLiteral(resourceName: "music select"))
        toggleButton(button: music03, onImage: #imageLiteral(resourceName: "music unselect"), offImage: #imageLiteral(resourceName: "music unselect"))
        toggleButton(button: music01, onImage: #imageLiteral(resourceName: "music unselect"), offImage: #imageLiteral(resourceName: "music unselect"))
    
        let defaults = UserDefaults.standard
        defaults.set(true, forKey: "InstructionsButtonIsHidden")
        UserDefaults.standard.set(true, forKey: "volumeValue")
    
    //        let sounddefaults = UserDefaults.standard
    //        
    //        sounddefaults.set(true, forKey: "soundMute")
        onoffLabel.setTitle("on", for: .normal)
    
    }
    
    @IBAction func music3(_ sender: UIButton) {
        let path = Bundle.main.path(forResource: "fire3", ofType: "mp3")
        let filePath = NSURL(fileURLWithPath:path!)
        player = try! AVAudioPlayer.init(contentsOf: filePath as URL)
        player.numberOfLoops = -1 //logic for infinite loop
        player.prepareToPlay()
        player.play()
    
    
        let audioSession = AVAudioSession.sharedInstance()
        try!audioSession.setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers)
    
    
    
        UserDefaults.standard.set("fire3", forKey: "music")
        toggleButton(button: music03, onImage: #imageLiteral(resourceName: "music select"), offImage: #imageLiteral(resourceName: "music select"))
        toggleButton(button: music02, onImage: #imageLiteral(resourceName: "music unselect"), offImage: #imageLiteral(resourceName: "music unselect"))
        toggleButton(button: music01, onImage: #imageLiteral(resourceName: "music unselect"), offImage: #imageLiteral(resourceName: "music unselect"))
    
        let defaults = UserDefaults.standard
        defaults.set(true, forKey: "InstructionsButtonIsHidden")
        UserDefaults.standard.set(true, forKey: "volumeValue")
    
    //        let sounddefaults = UserDefaults.standard
    //        
    //        sounddefaults.set(true, forKey: "soundMute")
        onoffLabel.setTitle("on", for: .normal)
    
    
    }
    
    
    
    func SetUpSound() {
    
        if let path = Bundle.main.path(forResource: "fire1", ofType: "mp3") {
            let filePath = NSURL(fileURLWithPath:path)
            player = try! AVAudioPlayer.init(contentsOf: filePath as URL)
            player.numberOfLoops = -1 //logic for infinite loop
            player.prepareToPlay() 
            player.play()
        }
    
        let audioSession = AVAudioSession.sharedInstance()
        try!audioSession.setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers)
    }
    

    有没有办法播放以前固定音量的音频?即,如果我保持音量1的低音量,音频2也应低。它不应该成为最大的。任何帮助是极大的赞赏。

1 个答案:

答案 0 :(得分:2)

每次播放下一个音频文件时,您都在重新创建AVAudioPlayer。这意味着您设置的先前音量设置现在被丢弃。将此行添加到您创建音频播放器的位置:

player = try! AVAudioPlayer.init(contentsOf: filePath as URL)
player.volume = volumeSlider.value

以这种方式使用自己的音量控制滑块的问题是你没有调整系统音量 - 但实际上这可能就是你想要的。因此,如果设备音量设置为50%,然后用户将滑块设置为100% - 这仍将仅为50%音量。您可能需要查看MPVolumeView,它可以访问系统音量滑块,这将始终影响AVAudioPlayer,您无需直接设置播放器音量。它还允许您访问Airplay图标,以便用户可以轻松地在蓝牙或Airplay设备(如Apple TV)上播放音频。

另一件事是你在整个过程中重复相同的代码。你可以通过一个只传递音频文件名的播放功能来整理它,如下所示:

func playAudio(name: String) {
    let path = Bundle.main.path(forResource: name, ofType: "mp3")
    let filePath = NSURL(fileURLWithPath:path!)
    player = try! AVAudioPlayer.init(contentsOf: filePath as URL)
    player.numberOfLoops = -1 //logic for infinite loop
    player.prepareToPlay()
    player.play()
}

然后你可以这样称呼:

@IBAction func music1(_ sender: UIButton) {
    playAudio(name: "fire1")
    ...