2017-11-30 16 views
0

안녕을 재생되지 않습니다 나는 내가 비디오를 재생하기 위해 시작 화면을 추가 할AVPlayer를 나는 스토리 보드를 사용하지 않는 아이폰 OS 오브젝티브 C 응용 프로그램이 비디오를

(그것은 XIB 파일을 사용합니다) UIViewController에서 파생 된 새로운 Coca Touch 클래스를 추가했습니다. (또한 'XIB 파일을 작성하십시오').

이 클래스를 새 기본 화면으로 바꿨으므로 제대로로드되지만 비디오는 재생되지 않습니다. AVFoundation 프레임 워크 등을 추가 했으므로 문제가되지 않습니다.

여기 내 코드입니다.

.H 파일

#import <UIKit/UIKit.h> 

@interface VideoIntroViewController : UIViewController 
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; 
@end 

하는 .m 파일

import <AVFoundation/AVFoundation.h> 

static const float PLAYER_VOLUME = 0.0; 

@interface VideoIntroViewController() 

@property (nonatomic) AVPlayer *player; 
@property (weak, nonatomic) IBOutlet UIView *playerView; 

@end 

@implementation VideoIntroViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self createVideoPlayer]; 
} 

- (void)createVideoPlayer 
{ 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"welcome_video" ofType:@"mp4"]; 
    NSURL *url = [NSURL fileURLWithPath:filePath]; 

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url]; 

    self.player = [AVPlayer playerWithPlayerItem:playerItem]; 
    self.player.volume = PLAYER_VOLUME; 

    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; 
    playerLayer.videoGravity = UIViewContentModeScaleToFill; 
    playerLayer.frame = self.playerView.layer.bounds; 
    [self.playerView.layer addSublayer:playerLayer]; 

    [self.player play]; 
} 

시작 화면하지만 영상이 재생됩니다.

무엇이 잘못 될 수 있습니까?

+0

어떤 디버깅을 했습니까? 'createVideoPlayer' 메소드에서 런타임에 실제로 어떻게됩니까? 'playerLayer.frame'에 지정한 실제 프레임은 무엇입니까? 아무것도 아니야? – rmaddy

+0

나는 시뮬레이터에서 디버깅 중이며, 아무것도 createVideoPlayer에서 null입니다. 디버그 출력 창에서 많은 AQDefaultDevice가 inputstream 0 0 0x0 메시지를 건너 뛰는 것을 볼 수 있습니다. –

답변

0

내 기본 XIB 파일에서 View 컨트롤을 배치하지 않았습니다. View Control을 배치하고 CTL + Right를 클릭하여 내 playerView에 연결하면 비디오가 재생됩니다.

1

코드를보십시오

적인 filePath는 NSString * = [NSBundle mainBundle] pathForResource @ ofType "welcome_video": "MP4"@];

NSURL *url = [NSURL fileURLWithPath:filePath]; 

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url]; 

self.player = [AVPlayer playerWithPlayerItem:playerItem]; 

CALayer *superlayer = self.playerView.layer; 
self.player.volume = 20.0; 

AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; 
[playerLayer setFrame:self.playerView.bounds]; 
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;// you can also use AVLayerVideoGravityResizeAspect to clip video to view's bound 
playerLayer.frame = self.playerView.layer.bounds; 
[superlayer addSublayer:playerLayer]; 
[self.player seekToTime:kCMTimeZero]; 
[self.player play]; 

다른 동영상 파일을 확인한 결과 정상적으로 작동합니다.