iOS中的远程控制事件与Swift [英] Remote Control event in iOS with Swift

查看:548
本文介绍了iOS中的远程控制事件与Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试找出如何读取Apple耳机的音量按钮,以用作相机快门的触发器(如苹果相机应用程序)。

Trying to figure out how to read the Apple headphone's volume buttons to use as a trigger for the camera shutter (as the Apple Camera app does).

远程控制事件
通过事件
接收到远程控制, AVAudioPlayer 对象, / / gc / code> .beginReceivingRemoteControlEvents()和 remoteControlReceivedWithEvent ,并使此视图 canBecomeFirstResponder c $ c> return true

From the documentation on Remote Control Events, Remote Control Received With Event, and this git repo, I've pieced together that I'll probably need an AVAudioPlayer object, .beginReceivingRemoteControlEvents(), and remoteControlReceivedWithEvent, along with making this view 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()
    }
}

我希望得到 当击中耳机上的音量按钮,而不是我只是看到它像正常调整耳机音量。所以我必须缺少一些东西,也许用委托 AVSession

I expected to get "does this work" when hitting the volume buttons on the headphones, instead I just see it adjust the headphone volume like normal. So I must be missing something, maybe with a delegate or AVSession?

推荐答案

我在 r / swift ,其中我被告知可能需要播放音频(直接从文档引用)。

I cross-posted this on r/swift, where I was told it probably requires playing audio (quoted straight from the documentation).

所以,虽然这不是理想的解决方案,它适合我自己的私人使用。

So while this isn't the ideal solution, it works for my own private use.

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

我注意到,在苹果的相机应用程序,+/-音量按钮触发相机,麦克风按钮暂停/播放任何音频运行在另一个应用程序,但在这个实现中音量按钮仍然控制音量(以及任何音频在应用程序启动时已暂停)。

I noticed that in Apple's camera app, the +/- volume buttons trigger the camera, and the microphone button pauses/plays any audio running in another app, but in this implementation the volume buttons still control the volume (and any audio has been paused when the app is launched).

rc.rawValue:103 对应于麦克风按钮的单击,双击返回 104 ,三次点击返回 105 ,然后有时碰巧一对夫妇会返回 108 109

An rc.rawValue: 103 corresponds to a single click of the microphone button, a double click returns 104, and a triple click returns 105, and then sometimes bumping a couple at a time returns a 108 or 109.

这篇关于iOS中的远程控制事件与Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆