【问题标题】:Why this Objective-C code won't play an AVPlayerViewController video?为什么这个 Objective-C 代码不会播放 AVPlayerViewController 视频?
【发布时间】:2017-05-12 13:22:08
【问题描述】:

两者都在 viewDidLoad() 中。 这个objective-c源文件有什么问题? 快速版本就像一个魅力...... Xcode 版本。 8.2 iOS 10

斯威夫特(好的):

        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];

【问题讨论】:

标签: ios objective-c avplayer avplayerviewcontroller


【解决方案1】:
@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];
}

试试我的代码。如果我的代码有任何问题,请发表评论。 谢谢。 快乐编码。

【讨论】:

  • 是的,它适用于在线视频,但我无法让它适用于本地视频(我只有一个空白屏幕)。并且仍然想知道问题的代码有什么问题......无论如何,谢谢。
  • #import 你用过这个框架吗@huse
  • 使用属性不是 AVPlayer* 播放器的本地指针,我怀疑它会在分配后立即释放
  • @LucaRocchi 如果是这种情况,它可能会崩溃而不是不播放视频?
  • 抱歉回复晚了(寒假短)。我现在就试试你的建议。
【解决方案2】:

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

试试

使用 [NSURL fileURLWithPath:path] 代替 [NSURL URLWithString:path]

[NSURL 文件URLWithPath]

或者

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

【讨论】:

  • 我已经应用了这个,但什么也没发生。播放器视图只出现但不播放歌曲。我正在播放带有 URL 的 mp3 歌曲。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
相关资源
最近更新 更多