AVPlayer不会在iOS9中播放来自网址的视频

时间:2015-09-30 11:43:29

标签: ios xcode avplayer

我正在尝试在UIView中嵌入AVPlayer并从网址播放mp4视频文件。问题是我只收到一个黑色的空白视图(见截图)

enter image description here

在之前的iOS版本中,它对我有用,但自从升级到iOS9后我遇到了这个问题。

我的.h文件如下所示:

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *viewPlayerContainer;

在我的实现文件中,我有以下内容:

@import AVFoundation;
@import AVKit;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];

    NSURL *url = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"];

    AVURLAsset *asset = [AVURLAsset assetWithURL: url];
    AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];

    AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];

    [playerViewController.view setFrame:CGRectMake(0, 0, _viewPlayerContainer.bounds.size.width, _viewPlayerContainer.bounds.size.width)];

    playerViewController.showsPlaybackControls = NO;

    [_viewPlayerContainer addSubview:playerViewController.view];


    [player play];


}

我在这里错过了什么吗?

提前致谢!

5 个答案:

答案 0 :(得分:14)

@implementation ViewController{
    AVPlayerViewController *playerViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     playerViewController = [[AVPlayerViewController alloc] init];
}

- (IBAction)playVideo:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"];

    AVURLAsset *asset = [AVURLAsset assetWithURL: url];
    AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];

    AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
    playerViewController.player = player;
    [playerViewController.view setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width)];

    playerViewController.showsPlaybackControls = YES;

    [self.view addSubview:playerViewController.view];

    [player play];
}

答案 1 :(得分:2)

视频未播放的原因仅在于tls HTTP版本过于陈旧public class ProxyCollection<TProxy, TSource> : IEnumerable<TProxy>, IDisposable{ private readonly Dictionary<TSource, TProxy> _map = new Dictionary<TSource, TProxy>(); private readonly Func<TSource, TProxy> _proxyFactory; private readonly ObservableCollection<TSource> _sourceCollection; public ProxyCollection(ObservableCollection<TSource> sourceCollection, Func<TSource, TProxy> proxyFactory){ _sourceCollection = sourceCollection; _proxyFactory = proxyFactory; AddItems(sourceCollection); _sourceCollection.CollectionChanged += OnSourceCollectionChanged; } public IEnumerator<TProxy> GetEnumerator(){ return _map.Values.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator(){ return GetEnumerator(); } private void AddItems(IEnumerable<TSource> sourceCollection){ foreach (TSource sourceItem in sourceCollection){ AddProxy(sourceItem); } } private void AddProxy(TSource source){ _map[source] = _proxyFactory(source); } private void OnSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e){ switch (e.Action){ case NotifyCollectionChangedAction.Add: AddItems(e.NewItems.Cast<TSource>()); break; case NotifyCollectionChangedAction.Remove: RemoveItems(e.OldItems.Cast<TSource>()); break; case NotifyCollectionChangedAction.Replace: ReplaceItems(e.OldItems.Cast<TSource>(), e.NewItems.Cast<TSource>()); break; case NotifyCollectionChangedAction.Move: throw new NotImplementedException("Your code here"); case NotifyCollectionChangedAction.Reset: throw new NotImplementedException("Your code here"); default: throw new ArgumentOutOfRangeException(); } } private void ReplaceItems(IEnumerable<TSource> oldItems, IEnumerable<TSource> newItems){ RemoveItems(oldItems); AddItems(newItems); } private void RemoveItems(IEnumerable<TSource> sourceItems){ foreach (TSource sourceItem in sourceItems){ _map.Remove(sourceItem); } } public void Dispose(){ _sourceCollection.CollectionChanged -= OnSourceCollectionChanged; //optionally foreach (IDisposable proxy in _map.Values.OfType<IDisposable>()){ proxy.Dispose(); } } } ,请参阅App Transport Security

答案 2 :(得分:0)

你错过了这行代码

 playerViewController.player = player;

答案 3 :(得分:0)

将AVPlayerViewController作为视图控制器的属性,然后将其分配给viewDidLoad。

答案 4 :(得分:0)

如果您想从一个网址播放视频,则无需使用AVURLAsset。当您有多个视频或网址时,您应该使用AVURLAsset。 (但两种方式都有效)

参考:https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAsset_Class/index.html#//apple_ref/occ/cl/AVAsset

如果您有一个网址,以下方法可以正常工作。

NSURL *url = [NSURL URLWithString:@“<your_url_here>”];
AVPlayer *player = [[AVPlayer alloc] initWithURL: url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
controller.delegate = self;// if you want to use delegates...
controller.showsPlaybackControls=YES;
controller.view.frame = self.view.frame;
[self.view addSubview:controller.view];
[player play];