2016-12-28 9 views
3

enter image description hereどちらもviewDidLoad()にあります。 このobjective-cソースファイルの何が問題なのですか? スウィフト版は魅力のように機能します... Xcode ver。 8.2 iOSの10このObjective-CコードがAVPlayerViewControllerビデオを再生しないのはなぜですか?

スウィフト(OK):

 let path : String = Bundle.main.path(forResource: "charlie", ofType: "mp4")! 
     let movieurl : URL = URL(fileURLWithPath: path) 
     let movie : AVPlayerViewController = AVPlayerViewController() 
     movie.view.frame = self.view.bounds 

     let player : AVPlayer = AVPlayer(url: movieurl) 
     movie.player = player 

     self.view.addSubview(movie.view) 
     player.play() 

は、Objective-Cの(ビデオを再生/表示されません):

 AVPlayerViewController* movie; 
     AVPlayer* player; 

     NSString *path = [[NSBundle mainBundle] 
      pathForResource:@"charlie" ofType:@"mp4"]; 

     NSURL *url = [NSURL URLWithString:path] ; 

     movie = [[AVPlayerViewController alloc] init]; 
     movie.view.frame = self.view.bounds; 
     player = [[AVPlayer alloc] initWithURL:url]; 

     [movie setPlayer:player]; 
     [self.view addSubview:movie.view]; 

     [player play]; 
+0

コードはおそらくビデオを見つけられませんでした。このhttp://stackoverflow.com/questions/1949992/working-with-paths-from-nsbundle-mainbundle-resourcepathを使用してバンドルパスを修正しようとしましたか? –

答えて

2
@import AVKit; 
@import AVFoundation; 
@property (nonatomic) AVPlayer *avPlayer; 
@property (nonatomic) AVPlayerViewController* avPlayerView; 


NSString *path = [[NSBundle mainBundle] pathForResource:@"Video1" ofType:@"mp4"]; 

NSURL *url = [[NSURL alloc] initFileURLWithPath: path]; 

AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil]; 
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset]; 

avPlayer = [AVPlayer playerWithPlayerItem:anItem]; 
[avPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; 
self.avPlayerView = [[AVPlayerViewController alloc] init]; 
self.avPlayerView.view.frame = self.view.bounds; 
[self.avPlayerView setPlayer:_avPlayer]; 
[self.view addSubview:_avPlayerView.view]; 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 

    if (object == avPlayer && [keyPath isEqualToString:@"status"]) { 
     if (avPlayer.status == AVPlayerStatusFailed) { 
      NSLog(@"AVPlayer Failed"); 
     } else if (avPlayer.status == AVPlayerStatusReadyToPlay) { 
      NSLog(@"AVPlayer Ready to Play"); 
     } else if (avPlayer.status == AVPlayerItemStatusUnknown) { 
      NSLog(@"AVPlayer Unknown"); 
     } 
    } 
} 

-(IBAction) BtnPlay:(id)sender { 
    [avPlayer play]; 
} 

-(IBAction) BtnPause:(id)sender { 
    [avPlayer pause]; 
} 

私のコードを試してみてください。私のコードに問題がある場合は、コメントを入れてください。 ありがとうございます。 ハッピーコーディング。

+0

はい、それはオンラインビデオで働いていましたが、私はそれをローカルビデオで動作させることができませんでした(私はただの空白の画面しか持っていませんでした)。そして、依然として質問のコードに何が間違っているか知りたいです...とにかくありがとう。 – huse

+0

#import このフレームワークを使用しました@huse –

+0

AVPlayer *プレーヤーのローカルポインタではなくプロパティを使用します。割り当てられた時点ですぐにリリースされると思われます。 –

0

NSString * path = [[NSBundle mainBundle] pathForResource:@ "charlie" ofType:@ "mp4"];

[NSURL fileURLWithPath:パス]使用

を試す代わりに[NSURL URLWithString:パス]

[NSURL fileURLWithPath]

または

[[NSBundle mainBundle]をURLForResource:@ "charlie" withExtension:@ "mp4"]

+0

私はこれを適用しましたが、何も起こっていません。プレーヤービューは表示されますが、曲は再生されません。私はURLでMP3曲を再生しています。 – Moxarth

関連する問題