2016-09-09 2 views
2

누군가 위 또는 아래 볼륨 버튼을 눌렀을 때 감지하는 데 문제가 있습니다. 잠시 동안 파일을 재생하지만 볼륨이 변경되면 사용자가 단추를 눌러 경고를 표시하는 시점을 알고 싶습니다. Swift에서 개발 중이며 AVFoundation을 사용하여이 플레이어를 만들고 있습니다. 잠시 동안 스위프트에서 일하는 것을 찾을 수 없습니다. 나는이 언어에 익숙하지 않다.신속한 볼륨 변경 감지

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. 
    } 
} 
+0

여기에 트릭 https://github.com/jpsim/JPSVolumeButtonHandler 예뻐요 – Callam

+0

... 암모니아을해야 좋은 cocoapod입니다하지만 당신은 신속의 예를? – Martin

+0

포드가 필요없는 새로운 답변을 게시했습니다. – Callam

답변

2

트릭을 수행해야합니다.

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

도움을 주셔서 감사합니다. 조사가 필요했지만 결국 작동합니다. 다시 한 번 감사드립니다. – Martin