Cố gắng tìm hiểu cách đọc các nút âm lượng của tai nghe Apple để sử dụng làm trình kích hoạt cho màn trập máy ảnh (như ứng dụng Apple Camera).Sự kiện điều khiển từ xa trong iOS với Swift
Từ các tài liệu trên Remote Control Events, Remote Control Received With Event, và this git repo, tôi đã chắp ghép rằng tôi có lẽ sẽ cần một đối tượng AVAudioPlayer
, .beginReceivingRemoteControlEvents()
, và remoteControlReceivedWithEvent
, cùng với tạo ra quan điểm này canBecomeFirstResponder()
return true
.
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate {
var player: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
var session: AVAudioSession = AVAudioSession.sharedInstance()
session.setActive(true, error: nil)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
println("viewDidAppear worked...")
self.becomeFirstResponder()
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override func remoteControlReceivedWithEvent(event: UIEvent) {
let rc = event.subtype
println("does this work? \(rc.rawValue)")
//takePicture()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
tôi mong đợi để có được "does this work"
khi nhấn nút âm lượng trên tai nghe, thay vào đó tôi chỉ nhìn thấy nó điều chỉnh âm lượng tai nghe như bình thường. Vì vậy, tôi phải thiếu một cái gì đó, có thể với một delegate
hoặc AVSession
?
Điều này làm việc cho tôi (với sửa đổi nhỏ cho Swift 3) một cách riêng tư. Có ai biết nếu sử dụng như thế này sẽ được chấp nhận cho các cửa hàng ứng dụng? – triiiiista