【问题标题】:how to play the video in objective-c如何在objective-c中播放视频
【发布时间】:2017-09-13 04:37:03
【问题描述】:

我需要使用 Objective-c 在 Xcode 中播放视频。我在 Xcode 中拖了一个 mp4 文件。并使用了代码:

NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"videoname" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.view.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];

[self.avPlayer play];

但在文件路径中将错误显示为 nil。

【问题讨论】:

  • 在拖放到 XCode 时,您是否选中了复选框复制项目(如果需要)?
  • 我检查并给出了副本项目,我得到了输出,谢谢
  • 很高兴听到,欢迎

标签: ios objective-c iphone avplayer


【解决方案1】:

通过拖放添加项目时,您应该注意以下事项

1) 拖放到 XCode 时,您必须选中复选框copy item if needed

2) 右侧面板上的检查目标应与您当前的目标进行检查

【讨论】:

    【解决方案2】:

    试一试

      NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"videoname" ofType:@"mp4"];
      NSURL *streamURL = [NSURL fileURLWithPath:videoPath];
      MPMoviePlayerController *moviplayer =[[MPMoviePlayerController alloc] initWithContentURL:     streamURL];
      [moviplayer prepareToPlay];
      [moviplayer.view setFrame: self.view.bounds];
      [self.view addSubview: moviplayer.view];
      moviplayer.fullscreen = YES;
      moviplayer.shouldAutoplay = YES;
      moviplayer.repeatMode = MPMovieRepeatModeNone;
      moviplayer.movieSourceType = MPMovieSourceTypeFile;
      [moviplayer play]; 
    

    【讨论】:

      【解决方案3】:
      1. 将视频文件拖放到您的项目中,并且必须选中复选框 如果需要,复制项目
      2. 还在构建阶段复制捆绑资源中添加您的视频文件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-07
        • 2013-07-12
        • 1970-01-01
        • 2013-03-22
        • 2012-07-16
        • 1970-01-01
        相关资源
        最近更新 更多