ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • AVAudioSession
    App/Objective-C 2017. 2. 21. 10:02

    AVAudioSession 은 Audio를 동시재생될 수 있도록 해주는 기능이며

    background mode로 진입시에도 Audio/TTS가 Play될 수 있도록 해주는 기능


    사용법

    //AppDelegate.m

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
        NSError *error = NULL;
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setCategory:AVAudioSessionCategoryPlayback error:&error];
        if(error) {
            // Do some error handling
        }
        [session setActive:YES error:&error];
        if (error) {
            // Do some error handling
        }
     
        return YES;
    }
    cs



    //ViewController.m(Player Controller)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    -(void)viewDidAppear:(BOOL)animated{
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    }
     
    -(void)viewWillDisappear:(BOOL)animated {
        [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
        [self resignFirstResponder];
        [super viewWillDisappear:animated];
     
    - (BOOL) canBecomeFirstResponder {
        return YES;
    }
     
    //airPlay Button Event
    - (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
        NSLog(@"received event!");
        if (receivedEvent.type == UIEventTypeRemoteControl) { 
            switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause: { // airPlay가 일시정지 상태일 경우 
                [self startAudio:nil];
                break;
            }
            case UIEventSubtypeRemoteControlPlay: { // airPlay가 재생 상태일 경우 
                [self startAudio:nil];
                break;
            }
            case UIEventSubtypeRemoteControlPause: {
                [self startAudio:nil];
                break;
            }
            default:
            break;
            }
        }
    }
    cs


    'App > Objective-C' 카테고리의 다른 글

    Protocol  (0) 2017.04.02
    GCM(Google Cloud Messaging)  (0) 2017.02.24
    new와 alloc의 차이  (0) 2017.02.13
    ARC(Automatic Reference Counting)  (0) 2017.02.13
    AppDelegate 함수 정리  (0) 2017.02.07
Designed by Tistory.