헤드폰 상태 수신 -

2110 단어
  • 이 글은 이어폰 상태에 대한 소개
  • 인터넷의 대부분 방법은ios6를 바탕으로 제가 쓴 것은 비교적 새로운 버전
  • 입니다.
  • 코드는 아래와 같습니다. 무슨 문제가 있으면 저에게 연락 주세요
  • 1. 단일 예제 및 공지 작성

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [[AVAudioSession sharedInstance] setActive:YES error:nil];//                 .
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChangeListenerCallback:)   name:AVAudioSessionRouteChangeNotification object:nil];//    
    }
    

    2. 통지 방법의 실현

    - (void)audioRouteChangeListenerCallback:(NSNotification*)notification
    {
        
        NSDictionary *interuptionDict = notification.userInfo;
        NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
        switch (routeChangeReason) {
            case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
                NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable");
                tipWithMessage(@"    ");
                break;
            case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
                NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
                tipWithMessage(@"    ,      ");
                break;
            case AVAudioSessionRouteChangeReasonCategoryChange:
                // called at start - also when other audio wants to play
                tipWithMessage(@"AVAudioSessionRouteChangeReasonCategoryChange");
                break;
        }
    }
    

    3. 알림 및 사용자 정의 알림 창 제거

    -(void)dealloc{
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    //      
    NS_INLINE void tipWithMessage(NSString *message){
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"  " message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
            
            [alerView show];
            
            [alerView performSelector:@selector(dismissWithClickedButtonIndex:animated:) withObject:@[@0, @1] afterDelay:0.9];
        });
    }
    

    좋은 웹페이지 즐겨찾기