繁体   English   中英

检测到音量变化 swift

[英]detect up volume change swift

我在检测有人按下音量增大或减小按钮时遇到问题。 目前我只是播放一个文件,但我想知道用户何时按下按钮以在音量变化时显示警报。 我正在 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)
    }
}

由于某种原因不起作用。 这是你可以做到的 -

func addObserver() {
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(systemVolumeDidChange),
                                           name: Notification.Name("SystemVolumeDidChange"),
                                           object: nil)
}
                                           
func systemVolumeDidChange(notification: NSNotification) {
    Log.msg("New Volume = \(notification.userInfo?["Volume"] as? Float)")
}

用户信息中还有一些字段可以确定音量变化原因等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM