当耳机插入/拔出时如何获得通知?苹果电脑 [英] How to get notifications when the headphones are plugged in/out? Mac

查看:1313
本文介绍了当耳机插入/拔出时如何获得通知?苹果电脑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在耳机插孔插入或拔出耳机时收到通知。

我在stackoverflow上搜索过这个,但我似乎找不到我的寻找Mac,我只能找到iOS。

那么,你有没有任何想法如何执行这个?我想要做的是:当耳机插上时,我想以编程方式暂停iTunes(类似iOS的功能)。

谢谢!

I'd like to get notified when headphones are plugged in or out in the headphone jack.
I've searched around for this on stackoverflow but I can't seem to find what I'm looking for for the Mac, I can only find for iOS.
So, do you have any ideas on how to perform this? What I want to do with this is: when headphones are plugged out I want to programmatically pause iTunes (iOS-like feature).
Thank you!

推荐答案

您可以使用 CoreAudio 框架观察更改。

You can observe changes using the CoreAudio framework.

耳机和扬声器都是同一音频输出设备(内置类型)上的数据源。其中一个将在基于耳机插入或不插入的音频设备上。

Both headphones and the speakers are data sources on the same audio output device (of type built-in). One of both will be on the audio device based on headphones being plugged in or not.

要接收通知,您将听到内置输出设备上活动数据源的更改。

To get notifications you listen to changes of the active datasource on the built-in output device.

为了保持简短,我们将使用默认的输出设备。在大多数情况下,这是内置的输出设备。在现实生活的应用程序中,您需要循环播放所有可用的设备,以便找到它,因为默认设备可以设置为不同的音频设备(例如soundflower或airplay)。

To keep this short we'll use the default output device. In most cases this is the built-in output device. In real-life applications you'll want to loop all available devices to find it, because the default device could be set to a different audio device (soundflower or airplay for example).

AudioDeviceID defaultDevice = 0;
UInt32 defaultSize = sizeof(AudioDeviceID);

const AudioObjectPropertyAddress defaultAddr = {
    kAudioHardwarePropertyDefaultOutputDevice,
    kAudioObjectPropertyScopeGlobal,
    kAudioObjectPropertyElementMaster
};

AudioObjectGetPropertyData(kAudioObjectSystemObject, &defaultAddr, 0, NULL, &defaultSize, &defaultDevice); 



2。读取其当前数据源



设备上当前的数据源由 UInt32 类型的ID标识。

AudioObjectPropertyAddress sourceAddr;
sourceAddr.mSelector = kAudioDevicePropertyDataSource;
sourceAddr.mScope = kAudioDevicePropertyScopeOutput;
sourceAddr.mElement = kAudioObjectPropertyElementMaster;

UInt32 dataSourceId = 0;
UInt32 dataSourceIdSize = sizeof(UInt32);
AudioObjectGetPropertyData(defaultDevice, &sourceAddr, 0, NULL, &dataSourceIdSize, &dataSourceId);



3。观察数据源的更改



3. Observe for changes to the data source

AudioObjectAddPropertyListenerBlock(_defaultDevice, &sourceAddr, dispatch_get_current_queue(), ^(UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses) {
    // move to step 2. to read the updated value
});



确定数据源类型



你有数据源id为 UInt32 ,你可以使用值变换器查询音频对象的属性。例如,要获取源名称作为字符串使用 kAudioDevicePropertyDataSourceNameForIDCFString 。这将导致字符串内部扬声器或耳机。

Determine the data source type

When you have the data source id as UInt32 you can query the audio object for properties using a value transformer. For example to get the source name as string use kAudioDevicePropertyDataSourceNameForIDCFString. This will result in the string "Internal Speaker" or "Headphones". However this might differ based on user locale.

一种更简单的方法是直接比较数据源ID:

An easier way is to compare the data source id directly:

if (dataSourceId == 'ispk') {
    // Recognized as internal speakers
} else if (dataSourceId == 'hdpn') {
    // Recognized as headphones
}

但是我找不到为这些定义的常数值,因此这是一种未记录的。

However I couldn't find any constants defined for these values, so this is kind of undocumented.

这篇关于当耳机插入/拔出时如何获得通知?苹果电脑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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