2014-10-22 4 views
0

변경 사항이있을 때 내 앱이 이메일 서버를 폴링하고 현지 알림을 보내는 백그라운드에서 실행하게합니다. 백그라운드 상태에서 알림을 시작할 수는 있지만 휴대 전화가 진동하면 오디오가 재생되지 않습니다. 진동이 켜져있을 때 시계 앱의 알람이 계속 울리는 방식을 무시하는 방법이 있습니까? 내가 경고로드휴대 전화가 진동 할 때 알림 음 재생

+ (void)initializeAudioSettings 
{ 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 

    NSError *setCategoryError = nil; 
    BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError]; 
    if (!success) { 
     NSLog(@"Error setting up Audio session: %@", setCategoryError.description); 
    } 

    NSError *activationError = nil; 
    success = [audioSession setActive:YES error:&activationError]; 
    if (!success) { 
     NSLog(@"Error setting audio session active: %@", activationError.description); 
    } 

} 

다음 : 발사에 내 응용 프로그램 위임에

, 내가 아래에있는 내 경고의 클래스 메소드는 백그라운드에서 재생을 위해 설정하는 전화를 그런

- (void)loadAlertSoundID 
{ 
    NSLog(@"Loading alert sound"); 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"alert" ofType:@"mp3"]; 
    NSURL *soundURL = [NSURL URLWithString:soundPath]; 

    CFURLRef url = (__bridge CFURLRef)soundURL; 

    AudioServicesCreateSystemSoundID(url, &_alertSoundID); 
} 

을 때 새 메시지를 알리는 경우 알림 방법을 호출합니다.

- (void)playAlert 
{ 
    NSLog(@"Playing alert"); 
    AudioServicesPlaySystemSound(_alertSoundID); 

    _alertShouldBePlaying = YES; 
    _alertIsPlaying = YES; 

    AudioServicesAddSystemSoundCompletion(_alertSoundID, nil, nil, playSoundFinished, (__bridge void*)self); 

    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(vibrate:) userInfo:nil repeats:YES]; 
} 

그리고 완성을 위해 blo 사례 :

void playSoundFinished (SystemSoundID sound, void *data) 
{ 
    Alert *alert = [Alert sharedAlert]; 
    if (sound == alert.alertSoundID) 
    { 
     alert.alertIsPlaying = NO; 

     // Repeat until alert is disabled 
     if (alert.alertShouldBePlaying && !alert.alertIsPlaying) 
     { 
      [alert playAlert]; 
     } 
    } 

} 
+2

이것은 "극장 밖으로 던져 버려"기능입니다. –

+0

저는 사무실에서 내부적으로 사용할 도구로 이것을 구축하고 있습니다. 작업을 시작한 다음 날에는 진동으로 전환 한 다음 자동으로 업무에 복귀하는 "근무 외 근무"옵션이 있습니다. – Chris

+1

진동에 전화를 걸면 소리가 나지 않습니다. – rmaddy

답변

0

이 작업을 수행하는 방법이 있지만 앱 사용자 인 경우 감사하지 않을 것입니다.

AVAudioSessionCategory을 살펴보십시오. AVAudioSession.category를 AVAudioSessionCategoryPlayback 또는 AVAudioSessionCategoryPlayAndRecord으로 설정하면 경고음 설정뿐만 아니라 전화기의 자동 스위치를 우회합니다. 그것은 여전히 ​​볼륨 버튼에 의해 관리 될 수 있습니다.

편집 : 또한 자동 모드를 우회하기 위해 AudioServicesPlaySystemSound 반대로 사운드를 재생하기 위해 AVAudioPlayer 또는 AudioUnit을 사용하는 것이 좋습니다.

+0

나는 이미 재생하도록 설정했고, 방금 playAndRecord를 시도했지만, 들어올 때는 여전히 조용합니다. – Chris

+0

맞아요, 전체 게시물을 읽었어야합니다. AudioServicesPlaySystemSound 메서드를 통해 사운드를 재생하고 있습니다. AVAudioPlayer 또는 하위 레벨의 AudioUnit API를 사용하는 경우 자동 모드를 바이 패스 할 수 있습니다. AudioServicesPlaySystemSound가 AVAudioSessionCategory를 존중한다고 생각하지 않습니다. – quark

+0

알겠습니다. 통찰력에 감사드립니다. AudioUnit을 살펴보고 내가 할 수있는 것을 확인해 볼 것입니다. – Chris