ヘッドフォンが接続されていますか? iOS7
ヘッドフォンでも聴く必要があるオーディオファイルを備えたiPhone用のアプリを開発しています。
ヘッドフォンが接続されていないかどうかを確認して、ユーザーにヘッドフォンを接続するように指示するにはどうすればよいですか。
私は別のスレッドから次のコードを持っていますが、audioSessionGetPropertyメソッドは非推奨です。誰でもこの作業を行うために次のコードを変更する方法を知っているか、独自のコード/ソリューションを持っています。
ありがとう。
– (BOOL)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
//Maybe changing it to something like the following would work for iOS7?
//AVAudioSession* session = [AVAudioSession sharedInstance];
//OSStatus error = [session setCategory:kAudioSessionProperty_AudioRoute…?
//the line below is whats giving me the warning
OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);
/* Known values of route:
* “Headset”
* “Headphone”
* “Speaker”
* “SpeakerAndMicrophone”
* “HeadphonesAndMicrophone”
* “HeadsetInOut”
* “ReceiverAndMicrophone”
* “Lineout”
*/
if (!error && (route != NULL)) {
NSString* routeStr = (__bridge NSString*)route;
NSRange headphoneRange = [routeStr rangeOfString : @”Head”];
if (headphoneRange.location != NSNotFound) return YES;
}
return NO;
}
これはうまくいくはずですが、私は今すぐそれをテストすることはできません、私は夕方にやります。
– (BOOL)isHeadsetPluggedIn {
AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
for (AVAudioSessionPortDescription* desc in [route outputs]) {
if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
return YES;
}
return NO;
}
source