簡體   English   中英

Avplayer無法在iOS 8.0中播放視頻

[英]Avplayer not playing video in ios 8.0

我已經以編程方式實現了avplayer。

@interface showAskUsVideo ()
{
    AVURLAsset *asset;
    AVPlayerItem *item;
}   
@property (strong, nonatomic) AVPlayer *player;

-(void)viewWillAppear:(BOOL)animated
{
    [self playVideo];
}

-(void)playVideo {
         [playerViewController.view setFrame:CGRectMake(37, 80 ,495,220)];
         asset = [AVURLAsset assetWithURL: url];
         item = [AVPlayerItem playerItemWithAsset: asset];
         _player = [[AVPlayer alloc] initWithPlayerItem: item];
         playerViewController.showsPlaybackControls = NO;
        playerViewController.player = _player;

        [item addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];

       [_player addObserver:self forKeyPath:@"rate" options:0 context:nil];

       [self.view addSubview:playerViewController.view];
}

但是,當我選擇部署目標10.0時,它可以正常工作。 但是,對於iOS 8.0到9.3,只能在屏幕上看到PlaybackControls而不是視頻。

我的代碼需要更改什么?

playerViewController是否是視圖控制器層次結構的一部分? 如果沒有,那可以解釋一下。 嘗試將其添加為視圖控制器的子視圖控制器:

[self addChildViewController:playerViewController];
[self.view addSubview:playerViewController.view];

嘗試這樣:添加到.m

@property(非原子的,保留)AVPlayer * player;

@屬性(弱,非原子)IBOutlet AVPlayerClass * playerView;

(playerView是您要顯示它的視圖)

NSURL *url = [[NSBundle mainBundle]URLForResource:@"video_name" withExtension:@"mp4"];
_player = [AVPlayer playerWithURL:url];


playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
playerLayer.frame = _playerView.bounds;

playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

[_playerView.layer insertSublayer:playerLayer atIndex:0];

_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[_player currentItem]];
[_player play];



-(void)playerItemDidReachEnd:(NSNotification *)notification{

AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];

}

如果您為播放器使用ViewController 最佳做法是presentingViewController

[self presentViewController:playerViewController animated:YES completion:nil];

如果您使用XIB

PlayerViewController *vc = [[PlayerViewController alloc]
                          initWithNibName:@"PlayerViewController" bundle:nil];
[self.view addSubview:[vc view]];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM