SellyCloudSDK_demo/Example/SellyCloudSDK/Play/SCVideoPlayerViewController.m

219 lines
6.0 KiB
Objective-C

//
// SCVODPlayerViewController.m
// SellyCloudSDK_Example
//
// Created by Caleb on 16/9/25.
// Copyright © 2025 Caleb. All rights reserved.
//
#import "SCVideoPlayerViewController.h"
#import <SellyCloudSDK/SellyCloudManager.h>
#import <Masonry/Masonry.h>
#import "SCPlayItemContainerView.h"
#import <Photos/Photos.h>
#import <MediaPlayer/MediaPlayer.h>
@interface SCVideoPlayerViewController ()<SellyPlayerManagerDelegate>
//模拟同时播放多个player
//@property (nonatomic, strong)NSMutableArray<SellyCloudRtmpPlayer *> *players;
@property (nonatomic, strong)SellyVideoPlayer *player;
@property (nonatomic, strong)UIView *v1;
@property (nonatomic, strong)UIView *v2;
@property (nonatomic, strong)SCPlayItemContainerView *containerView;
@end
@implementation SCVideoPlayerViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.whiteColor;
// Do any additional setup after loading the view.
[self.view addSubview:self.v1];
[self.v1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.right.equalTo(self.view);
make.height.offset(400);
}];
[self.view addSubview:self.v2];
[self.v2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.right.equalTo(self.view);
make.top.equalTo(self.v1.mas_bottom);
}];
[self.view addSubview:self.containerView];
[self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.offset(0);
make.bottom.offset(-48);
make.height.offset(100);
}];
[self startPlay];
[UIApplication sharedApplication].idleTimerDisabled = YES;
}
- (void)viewDidLayoutSubviews {
[self.view bringSubviewToFront:self.containerView];
}
- (void)startPlay {
{
SellyVideoPlayer *player;
if (self.streamInfo) {
player = [[SellyVideoPlayer alloc] initWithStreamInfo:self.streamInfo];
}
else {
player = [[SellyVideoPlayer alloc] initWithUrl:[NSURL URLWithString:self.url]];
}
player.view.frame = self.view.bounds;
player.delegate = self;
player.scaleMode = SellyPlayerScalingModeAspectFit;
player.shouldAutoplay = true;
[player prepareToPlay];
[self.view addSubview:player.view];
self.player = player;
}
__weak typeof(self) weakSelf = self;
NSMutableArray *items = NSMutableArray.new;
{
SCPlayItemModel *model = SCPlayItemModel.new;
model.type = 1;
model.title = @"播放/暂停";
model.clickCallback = ^{
if (weakSelf.player.isPlaying) {
[weakSelf pause];
}
else {
[weakSelf start];
}
};
[items addObject:model];
}
{
SCPlayItemModel *model = SCPlayItemModel.new;
model.type = 2;
model.title = @"静音";
model.clickCallback = ^{
weakSelf.player.playbackVolume = 1-weakSelf.player.playbackVolume;
};
[items addObject:model];
}
{
SCPlayItemModel *model = SCPlayItemModel.new;
model.type = 3;
model.title = @"截图";
model.clickCallback = ^{
[weakSelf saveCurrentFrameToPhotoAlbum:weakSelf.player.getCurrentImage];
};
[items addObject:model];
}
{
SCPlayItemModel *model = SCPlayItemModel.new;
model.type = 4;
model.title = @"+10s";
model.clickCallback = ^{
weakSelf.player.currentPlaybackTime = weakSelf.player.currentPlaybackTime+10;
};
[items addObject:model];
}
self.containerView.models = items;
}
- (void)player:(SellyVideoPlayer *)player prepareToPlayChanged:(BOOL)prepare {
}
- (void)player:(SellyVideoPlayer *)player playbackDidFinished:(NSDictionary *)resultInfo {
}
- (void)player:(SellyVideoPlayer *)player playbackStateChanged:(SellyPlayerState)state {
}
- (void)player:(SellyVideoPlayer *)player onError:(NSError *)error {
}
- (void)start {
[self.player play];
}
- (void)pause {
[self.player pause];
}
// 假设 self.session 是你的 LFLiveSession 实例
- (void)saveCurrentFrameToPhotoAlbum:(UIImage *)image {
if (!image) {
NSLog(@"当前没有图像可保存");
return;
}
// 检查权限
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusDenied || status == PHAuthorizationStatusRestricted) {
NSLog(@"无权限访问相册,请到设置中开启权限");
return;
}
if (status == PHAuthorizationStatusNotDetermined) {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus newStatus) {
if (newStatus == PHAuthorizationStatusAuthorized) {
[self saveImage:image];
}
}];
} else {
[self saveImage:image];
}
}
- (void)saveImage:(UIImage *)image {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
// 保存完成回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
NSLog(@"保存失败:%@", error.localizedDescription);
} else {
NSLog(@"已保存当前视频帧至相册");
}
}
- (UIView *)v1 {
if (!_v1) {
_v1 = [[UIView alloc] init];
// _v1.backgroundColor = [UIColor systemPinkColor];
}
return _v1;
}
- (UIView *)v2 {
if (!_v2) {
_v2 = [[UIView alloc] init];
// _v2.backgroundColor = [UIColor systemPinkColor];
}
return _v2;
}
- (SCPlayItemContainerView *)containerView {
if (!_containerView) {
_containerView = [[SCPlayItemContainerView alloc] init];
}
return _containerView;
}
- (void)dealloc {
[UIApplication sharedApplication].idleTimerDisabled = NO;
NSLog(@"###%@ dealloc",NSStringFromClass(self.class));
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.player stop];
}
@end