2016-10-21 183 views
0

我有一個splash_screen.mp4文件。Mp4文件無法播放

這是我在.h文件

@property AVPlayer * player; 
@property AVPlayerLayer * playerLayer; 

代碼.m文件:

NSURL* mURL = [[NSBundle mainBundle] URLForResource:@"splash_screen" withExtension:@"mp4"]; 

self.player = [AVPlayer playerWithURL:mURL]; 
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone; 
Float64 seconds = 1.5f; 
CMTime targetTime = CMTimeMakeWithSeconds(seconds, NSEC_PER_SEC); 
[self.player seekToTime:targetTime]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(playerItemDidReachEnd:) 
               name:AVPlayerItemDidPlayToEndTimeNotification 
               object:[self.player currentItem]]; 

self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; 
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
self.playerLayer.needsDisplayOnBoundsChange = YES; 
[self.playerLayer setFrame:[self.view bounds]]; 

[self.view.layer addSublayer:self.playerLayer]; 
self.view.layer.needsDisplayOnBoundsChange = YES; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 
     if (self.player) { 
      [self.player play]; 
     } 
    }); 

在過去,我有一個.mov文件,並可以播放視頻文件,但現在我有一個.mp4文件,無法播放它。

測試模擬器上,視頻運行,但在設備上,它不運行。

+0

它適合我。當你播放.mp4文件時,應用程序崩潰或不?如果崩潰,控制檯中的輸出是什麼? – ronan

+0

@ronan,它不會崩潰。只是白色的空白屏幕。我調試,可以得到的網址,但我不知道爲什麼視頻不能播放。 – Khuong

+0

檢查,它正確複製到複製束資源中。 –

回答

0

您需要在右側面板爲您的MP4文件,選擇「目標成員」。

  1. 選擇的MP4文件

2。看截圖。

enter image description here

+0

我已檢查所有目標會員 – Khuong

+0

否只有第一個,現在檢查 – KKRocks

+0

它不受影響。 – Khuong

0

希望這個作品

Asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:filePath]]; 
avPlayerItem =[[AVPlayerItem alloc]initWithAsset:Asset]; 
avPlayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem]; 
avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:Player]; 
[avPlayerLayer setFrame:self.view.frame]; 
[self.view.layer addSublayer:avPlayerLayer]; 
[avPlayer seekToTime:kCMTimeZero]; 
[avPlayer play]; 
0

您是否嘗試過用在嘗試&捕捉處理它在它加入觀察者。

請啓用「應用程序播放音頻或者使用AirPlay的流音頻/視頻」中所需的背景模式。

NSURL* mURL = [[NSBundle mainBundle] URLForResource:@"splash_screen" withExtension:@"mp4"]; 

    NSURL *url = mURL; 
     AVPlayer *player = [[AVPlayer alloc]initWithURL:url]; 
     if (_audioPlayer != nil) 
     { 
      @try { 
       [_audioPlayer removeObserver:self forKeyPath:@"status" context:nil]; 
      } @catch (NSException *exception) { 
       NSLog(@"Catch player exception"); 
      } 
     } 
     _audioPlayer = player; 
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; 
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
self.playerLayer.needsDisplayOnBoundsChange = YES; 
[self.playerLayer setFrame:[self.view bounds]]; 

[self.view.layer addSublayer:self.playerLayer]; 
     [_audioPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; 

一旦AVPlayer將有來自mURL的有效文件,它會調用觀察者狀態準備播放。 「AVPlayerStatusReadyToPlay」

#pragma Observer for Player status. 
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 

    if (object == _audioPlayer && [keyPath isEqualToString:@"status"]) { 
     if (_audioPlayer.status == AVPlayerStatusFailed) { 
      NSLog(@"AVPlayer Failed"); 

     } else if (_audioPlayer.status == AVPlayerStatusReadyToPlay) { 
      NSLog(@"AVPlayerStatusReadyToPlay");    
       [_audioPlayer play]; 

      } 

     } else if (_audioPlayer.status == AVPlayerItemStatusUnknown) { 
      NSLog(@"AVPlayer Unknown"); 


     } 
     [_audioPlayer removeObserver:self forKeyPath:@"status" context:nil]; 
    } 

} 

你可以請嘗試一下。併發布您的評論,如果我們需要通過其他方式處理它。也請檢查視頻文件是否有效並且有內容。