[英]How do I implement a volume shutter in iOS 11?
我想在我的相機應用程序中實現音量快門。 當用戶按下音量按鈕時,我應該得到一個事件來拍照。
我正在尋找滿足以下要求的實現:
關於這個主題存在其他問題和答案,但對於舊版本的iOS,我想找到一個適用於iOS 11的版本。
ProCamera,ProCam和Camera +等相機應用程序具有滿足所有這些條件的音量快門,因此顯然可行。
所以這里的代碼將滿足您的所有要求。
我在StackOverflow上從問題/答案中提取了所有這些代碼。
Apple將拒絕使用此代碼提交的所有應用。
在Xcode 8.3.1中使用iOS 10.2進行測試
您需要使用AVFoundation
和MediaPlayer
框架才能工作。
import UIKit
import AVFoundation
import MediaPlayer
class ViewController: UIViewController {
//keeps track of the initial volume the user had set when entering the app
//used to reset the volume when he exits the app
var volume: Float = 0
override func viewDidLoad() {
super.viewDidLoad()
let audioSession = AVAudioSession.sharedInstance()
volume = audioSession.outputVolume-0.1 //if the user is at 1 (full volume)
try! audioSession.setActive(true)
audioSession.addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.new, context: nil)
//prevents the volume hud from showing up
let volView = MPVolumeView(frame: .zero)
view.addSubview(volView)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
//when the user changes the volume,
//prevent the output volume from changing by setting it to the default volume we specified,
//so that we can continue pressing the buttons for ever
(MPVolumeView().subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(volume, animated: false)
//implement your photo-capturing function here
print("volume changed")
}
}
如果要在用戶退出應用程序后確保代碼仍然有效,請在應用程序變為活動狀態時使用AppDelegate安裝觀察者,如下所示:
AppDelegate中
import UIKit
import AVFoundation
import MediaPlayer
var volume: Float = 0.5
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let audioSession = AVAudioSession.sharedInstance()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
(MPVolumeView().subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(volume, animated: false)
NotificationCenter.default.removeObserver(self)
NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "volumeChanged")))
}
func applicationDidBecomeActive(_ application: UIApplication) {
volume = audioSession.outputVolume
if volume == 0 { volume += 0.1 } else if volume == 1 { volume -= 0.1 }
try! audioSession.setActive(true)
audioSession.addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.new, context: nil)
}
func applicationWillResignActive(_ application: UIApplication) {
audioSession.removeObserver(self, forKeyPath: "outputVolume")
}
}
視圖控制器
import UIKit
import AVFoundation
import MediaPlayer
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.volumeChanged), name: Notification.Name(rawValue: "volumeChanged"), object: nil)
//prevents the volume hud from showing up
let volView = MPVolumeView(frame: .zero)
view.addSubview(volView)
}
func volumeChanged() {
print("volume changed")
}
}
根據Apple的App Store評論指南 ,這將是拒絕的明確理由。
2.5.9將拒絕更改標准交換機功能的應用程序,例如音量調高/降低和振鈴/靜音開關,或其他本機用戶界面元素或行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.