【问题标题】:How to play an video in an AVPlayer using the videoID for the searched item?如何使用搜索项目的 videoID 在 AVPlayer 中播放视频?
【发布时间】:2017-10-23 00:50:39
【问题描述】:

我为搜索的项目构建了一个显示视频的iOS应用,出现的问题如下:

  1. 我使用了 YouTube 数据 api 并提取了 videoId
  2. 我无法将其添加到 AVPlayer(但当使用 YouTube 播放器时,我能够添加 videoId,例如:

    [self.playerView loadWithVideoId:self.strngvideoId]; [self.view addSubview:_playerView];

  3. 我只想使用AVPlayer,不使用其他玩家,如何解决这个问题?

    我想通过AVPlayer 中的videoId 并播放视频。

【问题讨论】:

  • 我已经完成了但是使用 MPMoviePlayerViewController。
  • @ajjjjjjjj 你能告诉你你是怎么做的吗...只有你使用 videoID 的代码部分...提前谢谢
  • 如果您仍然想要代码。我会分享
  • @ajjjjjjjj ...请分享
  • @Roby 请查看此链接github.com/0xced/XCDYouTubeKit

标签: ios objective-c youtube


【解决方案1】:

如果你想播放视频,你必须使用 AVPlayerViewController。首先在你的项目中导入 AVKit 框架。下面的代码完美运行。

在 ViewController.h 中我导入了 AVKit 框架。在此之前我们需要添加 Target->BuildPhases->Link Binary With Libraries

ViewController.h

#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) AVPlayerViewController *playerViewController;
- (IBAction)actionPlayVideo:(id)sender;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController (){
    NSURL *vedioURL;
}

@end

@implementation ViewController
@synthesize playerViewController;

- (IBAction)actionPlayVideo:(id)sender{
   vedioURL =[NSURL fileURLWithPath:strngvideoId];
   AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:vedioURL];
   AVPlayer* playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem];
   playerViewController = [[AVPlayerViewController alloc] init];
   playerViewController.player = playVideo;
   playerViewController.player.volume = 0;
   playerViewController.view.frame = self.view.bounds;
   [self.view addSubview:playerViewController.view];
   [playVideo play];
}

Playing Video

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    相关资源
    最近更新 更多