耳机是否插好?IOS 7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21292586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:13:23  来源:igfitidea点击:

Are headphones plugged in? iOS7

iosios7avaudioplayeravaudiosessionheadphones

提问by 4GetFullOf

Developing an app for an iPhone with audio files that need to be listened too through headphones.

为 iPhone 开发一个应用程序,其中包含也需要通过耳机收听的音频文件。

How do I check if headphones aren't plugged in so I can tell the user to plug in headphones.

如何检查耳机是否未插入,以便我可以告诉用户插入耳机。

I have the following code from another thread but the audioSessionGetProperty method is deprecated. Anyone know how to alter the following code to make this work OR have there own code/solution.

我有来自另一个线程的以下代码,但不推荐使用 audioSessionGetProperty 方法。任何人都知道如何更改以下代码以使其工作或拥有自己的代码/解决方案。

Thanks.

谢谢。

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

回答by Antonio E.

This should work, but I cannot test it right now, I'll do in the evening.

这应该有效,但我现在无法测试,我会在晚上测试。

- (BOOL)isHeadsetPluggedIn {
    AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
    for (AVAudioSessionPortDescription* desc in [route outputs]) {
        if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
            return YES;
    }
    return NO;
}

回答by Warif Akhand Rishi

Just to extend @Antonio's answer. If you need to detect whether the user has pulled out or plugged in the headphone.

只是为了扩展@Antonio 的回答。如果需要检测用户是否拔出或插入耳机。

#import <AVFoundation/AVFoundation.h>


// [AVAudioSession sharedInstance]; // @Boris edited: you may need it if there is no `AVAudioSession instance` created before. If doesn't work, uncomment this line.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChangeListenerCallback:)
                                             name:AVAudioSessionRouteChangeNotification
                                           object:nil];
// don't forget to `removeObserver:`


// If the user pulls out he headphone Hyman, stop playing.
- (void)audioRouteChangeListenerCallback:(NSNotification*)notification
{
    NSDictionary *interuptionDict = notification.userInfo;

    NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];

    switch (routeChangeReason) {

        case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
            NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable");
            NSLog(@"Headphone/Line plugged in");
            break;

        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
            NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
            NSLog(@"Headphone/Line was pulled. Stopping player....");
            break;

        case AVAudioSessionRouteChangeReasonCategoryChange:
            // called at start - also when other audio wants to play
            NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");
            break;
    }
}

回答by Sajjon

Swift 3:

斯威夫特 3:

To check if headphones are connected

检查耳机是否连接

extension AVAudioSession {

    static var isHeadphonesConnected: Bool {
        return sharedInstance().isHeadphonesConnected
    }

    var isHeadphonesConnected: Bool {
        return !currentRoute.outputs.filter { 
func handleRouteChange(_ notification: Notification) {
    guard
    let userInfo = notification.userInfo,
    let reasonRaw = userInfo[AVAudioSessionRouteChangeReasonKey] as? NSNumber,
    let reason = AVAudioSessionRouteChangeReason(rawValue: reasonRaw.uintValue)
    else { fatalError("Strange... could not get routeChange") }
    switch reason {
    case .oldDeviceUnavailable:
        print("oldDeviceUnavailable")
    case .newDeviceAvailable:
        print("newDeviceAvailable") 
        if AVAudioSession.isHeadphonesConnected {
             print("Just connected headphones")
        } 
    case .routeConfigurationChange:
        print("routeConfigurationChange")
    case .categoryChange:
        print("categoryChange")
    default:
        print("not handling reason")
    }
}

func listenForNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange(_:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil)
}
.isHeadphones }.isEmpty } } extension AVAudioSessionPortDescription { var isHeadphones: Bool { return portType == AVAudioSessionPortHeadphones } }

Then you can just print("isHeadphones connected: \(AVAudioSession.isHeadphonesConnected)")

那么你就可以 print("isHeadphones connected: \(AVAudioSession.isHeadphonesConnected)")

Listening to Changes

倾听变化

In Swift 3the syntax is this:

Swift 3 中,语法是这样的:

 if AVAudioSession.isHeadphonesConnected {
    print("Just connected headphones")
 }

Notice use of:

注意使用:

func audioRouteChangeListenerCallback (notif: NSNotification){
        let userInfo:[NSObject:AnyObject] = notif.userInfo!
        println("\(userInfo)")
        let routChangeReason = UInt((userInfo[AVAudioSessionRouteChangeReasonKey]?.integerValue)!)
        switch routChangeReason {
        case AVAudioSessionRouteChangeReason.NewDeviceAvailable.rawValue:
            self.println("Headphone/Line plugged in");
            break;

        case AVAudioSessionRouteChangeReason.OldDeviceUnavailable.rawValue:
            //If the headphones was pulled move to speaker
            do {
                try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker)
            } catch _ {
            }
            self.println("Headphone/Line was pulled. Stopping player....");
            break;

        case AVAudioSessionRouteChangeReason.CategoryChange.rawValue:
            // called at start - also when other audio wants to play
            self.println("AVAudioSessionRouteChangeReasonCategoryChange");
            break;
        default:
            break;
        }
    }

回答by Husam

@Warif's code in Swift 2.0with little changes ...

@Warif 在Swift 2.0 中的代码几乎没有变化......

    func bluetoothOrHeadphonesConnected() -> Bool {

        let outputs = AVAudioSession.sharedInstance().currentRoute.outputs

        for output in outputs{

            if output.portType == AVAudioSessionPortBluetoothA2DP ||
               output.portType == AVAudioSessionPortBluetoothHFP ||
               output.portType == AVAudioSessionPortBluetoothLE ||
               output.portType == AVAudioSessionPortHeadphones {
                return true
            }

        }

        return false 
    }

:D

:D

回答by Pedro Silva

Swift 3.0 version

斯威夫特 3.0 版本

  • Method to check if headphones are plugged or any Bluetooth device with audio output connected
  • 检查耳机是否已插入或是否连接了任何具有音频输出的蓝牙设备的方法
 
    private func setupObservers() {

        NotificationCenter.default.addObserver(self, selector: #selector(self.audioRouteChangeListener), name: .AVAudioSessionRouteChange, object: nil)

    }

    func audioRouteChangeListener(notification: Notification) {

        guard let audioRouteChangeReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as? Int else { return }

        switch audioRouteChangeReason {

            case AVAudioSessionRouteChangeReason.oldDeviceUnavailable.hashValue:
                //plugged out

            default:
                break

        }

    }

  • It's important to check if the headphones are plugged out while you listen any audio.
  • 在您收听任何音频时,检查耳机是否已插入非常重要。
    func headsetPluggedIn() -> Bool {
    let route = AVAudioSession.sharedInstance().currentRoute
    return (route.outputs as! [AVAudioSessionPortDescription]).filter({ 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(plugout:) name:AVAudioSessionRouteChangeNotification object:nil];
-(void)plugout:(NSNotification*)notification
{
    isRemovedHeadset = YES;
}
.portType == AVAudioSessionPortHeadphones }).count > 0 }

回答by Mike Sprague

In Swift (as of 1.2):

在 Swift 中(从 1.2 开始):

if (moviePlayer.playbackState == MPMoviePlaybackStatePaused) 
{
    if(isRemovedHeadset)
    {
        isRemovedHeadset = NO;
        [moviePlayer prepareToPlay];
        [moviePlayer play];
        return;
    }
}

回答by Hardik Mamtora

func handleRouteChange(_ notification: Notification) {
    guard
        let userInfo = notification.userInfo,
        let reasonRaw = userInfo[AVAudioSessionRouteChangeReasonKey] as? NSNumber,
        let reason = AVAudioSession.RouteChangeReason(rawValue: reasonRaw.uintValue)
        else { fatalError("Strange... could not get routeChange") }
    switch reason {
    case .oldDeviceUnavailable:
        print("oldDeviceUnavailable")
    case .newDeviceAvailable:
        print("newDeviceAvailable")
        if AVAudioSession.isHeadphonesConnected {
            print("Just connected headphones")
        }
    case .routeConfigurationChange:
        print("routeConfigurationChange")
    case .categoryChange:
        print("categoryChange")
    default:
        print("not handling reason")
    }
}

func listenForNotifications() {
    NotificationCenter.default.rx
        .notification(AVAudioSession.routeChangeNotification)
        .subscribe(onNext: { (n) in
            self.handleRouteChange(n)
        })
        .disposed(by: disposeBag)
}

and handle your code using this isRemovedHeadset boolean in your

并使用此 isRemovedHeadset 布尔值处理您的代码

##代码##

回答by mazy

@Sajjon solution on Swift 5 with RxSwift

@Sajjon 在 Swift 5 上使用 RxSwift 的解决方案

##代码##