2013-01-24 2 views
4

나는이 주소 http://developer.apple.com/library/ios/#qa/qa1668/_index.html비디오 재생

하는 것으로 메모를 보았다 백그라운드에서 계속 연주하십시오. 그러나 응용 프로그램이 실제로 백그라운드로 전환되기 전에 이러한 변경 사항이 적용되어야합니다.

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
    [self becomeFirstResponder]; 

    [[AVAudioSession sharedInstance] setDelegate: self]; 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
    [[AVAudioSession sharedInstance] setActive: YES error: nil]; 



    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:AnUrl]]; 
    AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem]; 


    AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer]; 
    avPlayerLayer.frame = self.view.layer.bounds; 
    UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds]; 
    [newView.layer addSublayer:avPlayerLayer]; 
    [self.view addSubview:newView]; 

    [avPlayer play]; 

이 프로그램은 작동합니다. 그러나 배경에서는 소리가 차단됩니다. 배경 소리 만 계속 연주하고 싶습니다. AVPlayer에서 AVPlayerLayer를 어떻게 분리합니까?

답변

1

코드는 위의 괜찮지 만, 배경 /에서 전환하는 동안이 일을 수행

  • 응용 프로그램이 될 활성 : (당신의 avPlayerLayer가 AVPlayerLayer을해야합니다 당신이보기에 추가)

    - (void)applicationDidBecomeActive:(UIApplication *)application { 
        [avPlayerLayer playerLayerWithPlayer:avPlayer]; 
    
    } 
    
  • 응용 프로그램이 백그라운드로 전환 : (당신의 avPlayerLayer가 AVPlayerLayer을해야합니다 당신이보기에 추가)

    - (void)applicationWillResignActive:(UIApplication *)application { 
        [avPlayerLayer playerLayerWithPlayer:nil]; 
    } 
    

또한, plist의 UIBackgroundModes에 오디오를 추가하는 것을 잊지 마십시오.

건배.

+0

이것이 정확히 내가 필요한 것일 수 있습니다. 빠른 질문 (noobish 가능) :'applicationWillResignActive'는'AppDelegate' 메소드이고, 모든'AVPlayerLayer' 코드는 별도의 ViewController 파일에 있습니다. AppDelegate에서 VideoViewController의 플레이어를 참조 할 수있는 가장 좋은 방법은 무엇입니까? VideoVideoController를 AppDelegate로 가져 오거나 응용 프로그램 전체에서 "현재 실행중인 AVPlayer를 가져 오십시오"라는 방법이 있습니까? 가장 좋은 방법은 무엇입니까? – Nerrolken

+0

대리인이나 알림을 사용할 수 있습니다. (1) 대리인의 사용에 대해서는이 stackoverflow 문제를 참조하십시오. http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c. (2) 알림을 보려면 다음을 참조하십시오. http://stackoverflow.com/questions/9011868/whats-the-best-way-to-detect-when-the-app-is-entering-the-background-for- 내보기. 알림을 사용하는 것이 좋습니다. (UIApplicationDidBecomeActiveNotification 및 UIApplicationWillResignActiveNotification 사용). – mltpyt