使用 Swift 在 iOS 中远程控制事件
Posted
技术标签:
【中文标题】使用 Swift 在 iOS 中远程控制事件【英文标题】:Remote Control event in iOS with Swift 【发布时间】:2015-08-06 13:08:16 【问题描述】:试图弄清楚如何读取 Apple 耳机的音量按钮以用作相机快门的触发器(就像 Apple 相机应用程序所做的那样)。
从 Remote Control Events、
Remote Control Received With Event 和 this git repo 的文档中,我拼凑出我可能需要一个 AVAudioPlayer
对象、.beginReceivingRemoteControlEvents()
和 remoteControlReceivedWithEvent
,以及制作这个查看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()
我希望在按下耳机上的音量按钮时收到"does this work"
,但我只是看到它像平常一样调节耳机音量。所以我一定是错过了什么,也许是delegate
或AVSession
?
【问题讨论】:
【参考方案1】:我在r/swift 上交叉发布了这个,有人告诉我它可能需要播放音频(直接引用自文档)。
因此,虽然这不是理想的解决方案,但它适用于我自己的私人用途。
import UIKit
import AVFoundation
import MediaPlayer
class ViewController: UIViewController, AVAudioPlayerDelegate
var testPlayer: AVAudioPlayer? = nil
func loadSound(filename: NSString) -> AVAudioPlayer
let url = NSBundle.mainBundle().URLForResource(filename as String, withExtension: "caf")
var error: NSError? = nil
let player = AVAudioPlayer(contentsOfURL: url, error: &error)
if error != nil
println("Error loading \(url): \(error?.localizedDescription)")
else
player.prepareToPlay()
return player
override func viewDidLoad()
super.viewDidLoad()
self.testPlayer = self.loadSound("silence")
self.testPlayer?.numberOfLoops = -1
self.testPlayer?.play()
override func canBecomeFirstResponder() -> Bool
return true
override func viewDidAppear(animated: Bool)
super.viewDidAppear(animated)
self.becomeFirstResponder()
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
override func remoteControlReceivedWithEvent(event: UIEvent)
let rc = event.subtype
println("rc.rawValue: \(rc.rawValue)")
// take photo
我注意到在 Apple 的相机应用程序中,+/- 音量按钮触发相机,麦克风按钮暂停/播放在另一个应用程序中运行的任何音频,但在此实现中,音量按钮仍然控制音量(以及任何音频应用启动时已暂停)。
rc.rawValue: 103
对应单击麦克风按钮,双击返回104
,三次单击返回105
,然后有时一次碰到一对返回108
或@ 987654327@.
【讨论】:
这对我有用(对 Swift 3 稍作修改)。有谁知道应用商店是否会接受这样的使用?【参考方案2】:基于 Cody 的回答,但已于 2019 年更新(Swift 5)
import UIKit
import AVFoundation
import MediaPlayer
class ViewController: UIViewController, AVAudioPlayerDelegate
var myPlayer: AVAudioPlayer? = nil
func loadSound(filename: NSString) -> AVAudioPlayer?
let url = Bundle.main.url(forResource: filename as String, withExtension: "mp3")
do
let player = try AVAudioPlayer(contentsOf: url ?? URL(fileURLWithPath: ""))
player.prepareToPlay()
return player
catch
print("Error : \(error)")
return nil
override func viewDidLoad()
super.viewDidLoad()
guard let testPlayer = loadSound(filename: "silence") else
print("Not able to load the sound")
return
testPlayer.delegate = self
testPlayer.volume = 0.8
testPlayer.numberOfLoops = -1
myPlayer = testPlayer
myPlayer?.play()
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(animated)
self.becomeFirstResponder()
UIApplication.shared.beginReceivingRemoteControlEvents()
override func remoteControlReceived(with event: UIEvent?)
let rc = event?.subtype
print("rc.rawValue: \(rc?.rawValue)")
// Do your thing
【讨论】:
以上是关于使用 Swift 在 iOS 中远程控制事件的主要内容,如果未能解决你的问题,请参考以下文章