快速检测音量变化 [英] detect up volume change swift

查看:131
本文介绍了快速检测音量变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在检测有人何时按下音量调高按钮时遇到问题.目前,我只是播放一个文件,但我想知道用户何时按下按钮以在音量变化时显示警报.我正在开发Swift,并且正在使用AVFoundation创建此播放器.目前,我找不到适用于Swift的东西.我是这门语言的新手.

I'm having problems detecting when someone presses up or down volume button. For the moment I just play a file but I want to know when the user presses the button to show an alert when the volume changes. I'm developing in Swift and I'm using AVFoundation to create this player. For the moment I can't find something that works in Swift. I'm very new to this language.

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)
    }
}

这篇关于快速检测音量变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆