2012-03-01 4 views
0

로컬 음악을 재생하기 위해 앱을 제작하려하지만, 아쉽게도 AVQueuePlayer가 설치된 iOS5에서 백그라운드로 오디오를 재생할 수 없습니다.AVQueuePlayer가 설치된 iOS5에서 백그라운드에서 오디오 재생을 수행 할 수 없습니다.

다음
// Player setup 
mAudioPlayer = [[AVQueuePlayer alloc] init]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil]; 
[mAudioPlayer addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0, 1) queue:NULL usingBlock:^(CMTime time) { 
    [self updatePositionOnDisplay]; 
}]; 

// Audio session setup 
NSError *setCategoryErr = nil; 
NSError *activationErr = nil; 
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr]; 
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr]; 

내 "playerItemDidReachEnd"방법 : 내있는 viewDidLoad에서

, 나는이 코드를 가지고 내가 재생을 시작하면 작동

- (void)playerItemDidReachEnd:(NSNotification*)notification 
{ 
    NSLog(@"playerItemDidReachEnd"); 

    UIBackgroundTaskIdentifier newTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
     [[UIApplication sharedApplication] endBackgroundTask:newTaskID]; 
    }]; 
    NSLog(@"playerItemDidReachEnd 2"); 

    NSLog(@"searching next song"); 
    mCurrentSong = [self getNextSongWithIsSwitched:NO]; 
    if(mCurrentSong != nil){ 
     NSLog(@"Start : %@ - %@", mCurrentSong.artist, mCurrentSong.title); 

     mTapeTitle.text = [NSString stringWithFormat:@"%@ - %@", mCurrentSong.artist, mCurrentSong.title]; 

     AVPlayerItem* i = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:mCurrentSong.path]]; 

     if(i != nil){ 
      [mAudioPlayer insertItem:i afterItem:nil]; 
     }else 
      NSLog(@"BING!! no AVPlayerItem created for song's path: %@", mCurrentSong.path); 

     [i release]; 
    }else{ 
     NSLog(@"no song found"); 
     [mAudioPlayer pause]; 
     isPlaying = NO;   
     [mPlayButton setSelected:NO]; 
    } 

    [[UIApplication sharedApplication] endBackgroundTask:newTaskID]; 
    newTaskID = UIBackgroundTaskInvalid; 
} 

, 그리고 내가 전환 할 때 계속 연주 화면에서. 노래가 끝나면하지만, 여기에 로그

2012-03-01 10:00:27.342 DEMO[3096:707] playerItemDidReachEnd 
2012-03-01 10:00:27.360 DEMO[3096:707] playerItemDidReachEnd 2 
2012-03-01 10:00:27.363 DEMO[3096:707] searching next song 
2012-03-01 10:00:27.381 DEMO[3096:707] Start : Moby - Ah-Ah 

하지만 노래는

는 사람이 내 코드에 문제가 있는지 말해 줄 수 ... 효과적으로 시작이다?

고마워요. 작동하는지

답변

0

상태 AVPlayerStatusReadyToPlay 다음 백그라운드 작업

을 종료 할 때 "currentItem.status"에 대한 mAudioPlayer에 관찰자를 추가 할 필요가 다음 라인

[[UIApplication sharedApplication] endBackgroundTask:newTaskID]; 
newTaskID = UIBackgroundTaskInvalid; 

을 언급하려고

관련 문제