ios - 快速检测音量变化

标签 ios swift avaudioplayer

我在检测有人按下调高或调低音量按钮时遇到问题。目前我只是播放一个文件,但我想知道用户何时按下按钮以在音量变化时显示警报。我在 Swift 中开发,我正在使用 AVFoundation 来创建这个播放器。目前我找不到适用于 Swift 的东西。我对这种语言很陌生。

import UIKit
import AVFoundation

class ViewController: UIViewController {
    var backgroundMusicPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        playBackgroundMusic("IronBacon.mp3")
    }

    func playBackgroundMusic(filename:String){
        let url = NSBundle.mainBundle().URLForResource(filename, withExtension: nil)
        print(url)
        guard let newUrl = url else{
            print("couldn't find file: \(filename)")
            return
        }
        do{
            backgroundMusicPlayer = try AVAudioPlayer(contentsOfURL: newUrl)
            backgroundMusicPlayer.numberOfLoops = -1
            backgroundMusicPlayer.prepareToPlay()
        }catch let error as NSError{
            print(error.description)
        }
    }

    @IBAction func playPauseAction(sender: UIButton) {
        sender.selected = !sender.selected
        if sender.selected {
            backgroundMusicPlayer.play()
        } else {
            backgroundMusicPlayer.pause()
        }
    }

    func ShowAlert(title: String, message: String, dismiss: String) {
        let alertController = UIAlertController(title: title, message:
            message, preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: dismiss, style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
    }

    func volumeUp(){
        ShowAlert( "example", message: "example", dismiss: "close")
    }

    func volumeDown(){
        ShowAlert( "example", message: "example", dismiss: "close")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

最佳答案

这应该可以解决问题。

class ViewController: UIViewController {

    // MARK: Properties

    let notificationCenter = NSNotificationCenter.defaultCenter()

    // MARK: Lifecycle

    override func viewDidAppear(animated: Bool) {

        super.viewDidAppear(animated)

        notificationCenter.addObserver(self,
            selector: #selector(systemVolumeDidChange),
            name: "AVSystemController_SystemVolumeDidChangeNotification",
            object: nil
        )
    }

    override func viewDidDisappear(animated: Bool) {

        super.viewDidDisappear(animated)

        notificationCenter.removeObserver(self)
    }

    // MARK: AVSystemPlayer - Notifications

    func systemVolumeDidChange(notification: NSNotification) {

        print(notification.userInfo?["AVSystemController_AudioVolumeNotificationParameter"] as? Float)
    }
}

关于ios - 快速检测音量变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39402223/

相关文章:

ios - 网络运行后获取小区计数

ios - 如何在 Swift 3 中使用 AVAudioPlayer 播放 .ogg 音频文件?

ios - SKAction.playSoundFileNamed -- 如何将音量设置在 0.0 - 1.0 之间

ios - AVAudioPlayer 音量范围?

ios - 使用 Storyboard时设置默认选项卡

iphone - UISearchBar 在添加多个空格后画上句号

swift - 如何将字符串转换为 UIColor (Swift)

ios - 点击按钮时重复播放声音

objective-c - IB中的子类化

ios - 向服务器发送设备 token 或注册 token ?