2014-12-11 2 views
0

AVPlayer으로 플레이어를 구현 중이며 URL에서 비디오를 재생합니다. 이 URL은 "고정 길이"비디오 (미리 설정된 기간 있음) 및 라이브 비디오 (지속 시간을 알 수 없음/정의되지 않음)에 사용할 수 있습니다.AVPlayer가 다른 형식으로 재생되지 않습니다

고정 길이 URL은 .mp4 파일 (H264 코덱 사용)에 대한 링크를 사용하는 반면 라이브 동영상은 .m3u8 확장자 (H264도 포함)입니다.

많은 .m3u8 URL을 재생할 수 있지만 .mp4를 재생하려고하면로드되지 않습니다. 그러나 먼저 앱을 시작하고 .mp4 URL을 재생하면 다른 .mp4 URL을 재생할 수 있지만 .m3u8로 전환 할 수는 없습니다.
두 형식 모두 정상적으로 작동하지만 한 형식에서 다른 형식으로 전환 할 때 약간의 문제가 있습니다.

이 내가 인스턴스화하는 데 사용하는 방법과 "깨끗한"플레이어입니다

- (void)prepareAndPlayAutomatically:(BOOL)playAutomatically { 
    currentItem = [AVPlayerItem playerItemWithURL:videoURL]; 
    [[self player] replaceCurrentItemWithPlayerItem:currentItem]; 
    [self.layer addSublayer:[self playerLayer]]; 

    [[self player] addObserver:self forKeyPath:@"rate" options:0 context:nil]; 

    [[self player] seekToTime:kCMTimeZero]; 

    if (playAutomatically) { 
    [[self player] play]; 
    } 
} 

- (void)clean { 
    [[self player] pause]; 
    [[self player] seekToTime:kCMTimeZero]; 
    [[self player] replaceCurrentItemWithPlayerItem:nil]; 
    [[self player] removeObserver:self forKeyPath:@"rate"]; 
    [self removeFromSuperview]; 
} 

[self player]는 싱글 AVPlayer를 인스턴스를 반환은;
[self playerLayer] 또한 싱글 톤 AVPlayerLayer 인스턴스를 반환합니다.

- (AVPlayer *)player { 
    static dispatch_once_t once; 
    static AVPlayer *player; 
    dispatch_once(&once, ^{ 
    player = [AVPlayer playerWithPlayerItem:currentItem]; 
    }); 
    return player; 
} 

- (AVPlayerLayer *)playerLayer { 
    static dispatch_once_t once; 
    static AVPlayerLayer *playerLayer; 
    dispatch_once(&once, ^{ 
    playerLayer = [AVPlayerLayer playerLayerWithPlayer:[self player]]; 
    }); 
    return playerLayer; 
} 

이 싱글 톤에 대한 문제점을 좁혀 냈습니다. 싱글 톤 패턴을 제거하면 문제가 발생하지 않으며 두 가지 종류의 비디오를 모두 재생할 수 있습니다.

플레이어를 "청소"할 때 내가해야 할 것이 있습니까?

답변

2

AVPlayer 클래스는 비디오 구성이 다른 항목에 대한 재사용을 목적으로하지 않습니다. 워드 프로세서

[AVPlayer replaceCurrentItemWithPlayerItem:] method에 :

특별 고려 사항

새로운 항목은 항목이 대체 또는 더 컴포 지터가없는 동일한 컴포 지터가 있어야합니다.

+0

문제가 해결되지는 않지만 문제가 발생하는 이유를 설명합니다. 솔루션 : 싱글 톤 패턴이 없습니다. – Guilherme

0

나도 같은 문제가 최근에 발생했습니다. AVQueuePlayer의 인스턴스 2 개를 유지하여 해결했습니다. 하나는 Mp4를 재생하고 다른 하나는 M3u8을 재생합니다. 플레이어보기를 숨기고 비디오 형식 (Mp4/M3u8)을 기반으로 다른 플레이어보기를 표시하여이 플레이어간에 전환했습니다.

관련 문제