gpt4 book ai didi

ios - AVPlayer 无法正常工作

转载 作者:行者123 更新时间:2023-11-29 00:30:11 24 4
gpt4 key购买 nike

当我滑动页面返回时,AVPlayer无法正常工作

------------------------------------------------------------------------------------------------------------------------------------------------------------------

#import "TestAVPlayerViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "TestAVPlayer-Swift.h"

@interface TestAVPlayerViewController ()

@property (strong, nonatomic) AVPlayer *player;
@property (strong, nonatomic) AVPlayerItem *playerItem;
@property (strong, nonatomic) AVPlayerLayer * playerLayer;

@end

@implementation TestAVPlayerViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self initUI];

[self isEnableFullScreenReturn];
}

- (void)initUI{

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.contentSize = CGSizeMake(self.width, self.height * 2);
scrollView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:scrollView];

NSString *playURL = @"http://v.jxvdy.com/sendfile/w5bgP3A8JgiQQo5l0hvoNGE2H16WbN09X-ONHPq3P3C1BISgf7C-qVs6_c8oaw3zKScO78I--b0BGFBRxlpw13sf2e54QA";

AVAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:playURL] options:nil];

_playerItem = [AVPlayerItem playerItemWithAsset:asset];
_player = [[AVPlayer alloc]initWithPlayerItem:self.playerItem];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
_playerLayer.frame = CGRectMake(0, 150, 300, 300);
_playerLayer.backgroundColor = [UIColor blackColor].CGColor;

[scrollView.layer insertSublayer:_playerLayer atIndex:1];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_player play];
});

}
- (CGFloat)width{
return self.view.bounds.size.width;
}
- (CGFloat)height{
return self.view.bounds.size.height;
}

------------------------------------------------------------------------------------------------------------------------------------------------------------我该如何解决这个问题,希望它能顺利恢复。

最佳答案

您仅在 viewDidLoad 上播放视频,因此当用户“返回”您的 View 后出现 View 时,视频将不会开始播放。

尝试将您的[_player play]调用放入viewDidAppear中,例如:

- (void)viewWillAppear:(BOOL)animated
{
[self.player play];
}

如果您这样做,请记住删除 initUI 方法中的 play 调用,因为您不需要告诉玩家玩两次。

关于ios - AVPlayer 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42103466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com