Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

ios - detect up volume change 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.
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should do the trick.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...