음악 재생에 AVPlayer
을 사용하고 있습니다. 내 문제는 들어오는 호출 후에 플레이어가 다시 시작되지 않는다는 것입니다. 전화가 왔을 때 어떻게 처리합니까?전화 수신 후 AVplayer 재개
답변
AVAudioSession은 중단이 시작되고 끝날 때 알림을 보냅니다. 이 당신이 위임 방법을 사용했다 전에
- (id)init
{
if (self = [super init]) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(audioSessionInterrupted:) name:AVAudioSessionInterruptionNotification object:nil];
}
}
- (void)audioSessionInterrupted:(NSNotification *)notification
{
int interruptionType = [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
if (_state == GBPlayerStateBuffering || _state == GBPlayerStatePlaying) {
NSLog(@"Pausing for audio session interruption");
pausedForAudioSessionInterruption = YES;
[self pause];
}
} else if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
if ([notification.userInfo[AVAudioSessionInterruptionOptionKey] intValue] == AVAudioSessionInterruptionOptionShouldResume) {
if (pausedForAudioSessionInterruption) {
NSLog(@"Resuming after audio session interruption");
[self play];
}
}
pausedForAudioSessionInterruption = NO;
}
}
Handling Audio Interruptions, 당신은 AVAudioSessionInterruptionNotification
및 AVAudioSessionMediaServicesWereResetNotification
를 처리해야합니다 아이폰 OS 6 일부터 시작을 참조하십시오.
먼저 AVAudioSession 싱글 톤을 호출하고 원하는 용도로 구성해야합니다.
AVAudioSession *aSession = [AVAudioSession sharedInstance];
[aSession setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionAllowBluetooth
error:&error];
[aSession setMode:AVAudioSessionModeDefault error:&error];
[aSession setActive: YES error: &error];
그런 다음 당신은 AVAudioSession가 부를 것이다 통지하는 두 가지 방법을 구현해야합니다 : 예를 들어
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleAudioSessionInterruption:)
name:AVAudioSessionInterruptionNotification
object:aSession];
먼저 하나 때문에의 호출됩니다 중단을위한 들어오는 호출, 자명종 등
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleMediaServicesReset)
name:AVAudioSessionMediaServicesWereResetNotification
object:aSession];
두 번째는 med ia 서버가 어떤 이유로 재설정되면 오디오를 재구성하거나 하우스 키핑을 수행하려면이 알림을 처리해야합니다. 그런데 알림 사전에는 어떤 객체도 포함되지 않습니다. 옵션 값이 AVAudioSessionInterruptionOptionShouldResume
입니다 그리고 다른 통지를 다음의주의해야 할 때 재생을 다시 시작해야
- (void)handleAudioSessionInterruption:(NSNotification*)notification {
NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];
switch (interruptionType.unsignedIntegerValue) {
case AVAudioSessionInterruptionTypeBegan:{
// • Audio has stopped, already inactive
// • Change state of UI, etc., to reflect non-playing state
} break;
case AVAudioSessionInterruptionTypeEnded:{
// • Make session active
// • Update user interface
// • AVAudioSessionInterruptionOptionShouldResume option
if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
// Here you should continue playback.
[player play];
}
} break;
default:
break;
}
}
공지 사항 : 여기
는 재생 중단을 처리하기위한 예입니다 :- (void)handleMediaServicesReset {
// • No userInfo dictionary for this notification
// • Audio streaming objects are invalidated (zombies)
// • Handle this notification by fully reconfiguring audio
}
감사합니다.
설명서에서 MediaServicesWereReset을 처리 할 때 "응용 프로그램에서 사용하는 모든 오디오 객체를 다시 초기화하는 적절한 단계를 수행합니다."라고 말하면 AudioSession을 다시 구성하고 다시 활성으로 설정합니다. 내가해야 할 일은 뭐니? – xialin
으로 전화를 걸어도 내 AVPlayer
이 재생되지 않는 경우가 있습니다.
func interruptionNotification(_ notification: Notification) {
guard let type = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt,
let interruption = AVAudioSessionInterruptionType(rawValue: type) else {
return
}
if interruption == .ended && playerWasPlayingBeforeInterruption {
player.replaceCurrentItem(with: AVPlayerItem(url: radioStation.url))
play()
}
}
- 1. 전화 수신 후 오디오 녹음이 작동하지 않습니다.
- 2. 통화 후 음악 재개 방법
- 3. 수신 전화
- 4. 통화 후 앱 재개
- 5. 안드로이드 어떻게 전화를 후 목록보기를 재개
- 6. 수신 전화 차단
- 7. SMS 수신 전화 형식
- 8. 수신 전화 번호 받기
- 9. 수신 전화 별도 수신기
- 10. J2ME : 수신 전화 차단
- 11. 안드로이드의 수신 전화 유형
- 12. 안드로이드에서 수신 전화 차단하기
- 13. 수신 전화 거절
- 14. 차단 수신 전화
- 15. MPMoviePlayerController 및 수신 전화
- 16. 기본 전화 응용 전에 수신 전화 감지
- 17. BroadcastReceiver의 수신 및 발신 전화
- 18. 정전 후 카멜 처리 재개
- 19. Powershell 재부팅 후 스크립트 재개
- 20. preventDefault() 후 제출 프로세스 재개
- 21. VBA 오류 후 실행 재개
- 22. 전화 수신 후 안드로이드 미디어 플레이어가 재개되지 않습니다.?
- 23. 이메일 수신 후 이메일 본문의 번호로 자동 전화 걸기 코드
- 24. 수신 전화 연결이 끊어진 후 IDLE 상태로 전화를 받으려면 어떻게합니까?
- 25. Git에서 수신 후 수신 권한
- 26. 수신 전화 번호를 얻으려면 어떻게해야하나요?
- 27. Android에서 수신 전화 만 캡처하기
- 28. Twilio 수신 전화 - asp.net C#
- 29. 수신 전화 번호 가져 오기?
- 30. Android : 수신 전화 거절을위한 버튼
우리가 AVAudioSession는보다 자세한 인터페이스를 제공하기 때문에 내가 CTCallCenter을 제안하지 않을이 – user2889249
에 대한 CTCallCenter를 사용할 수 있습니다 만 재 장전 플레이어는 문제를 해결하기 위해 나에게 도움이됩니다. 사용되지 않는 메소드를 제거하기 위해 내 대답을 업데이트했습니다. –