1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369 |
- //
- // KSMediaMergeView.m
- // KulexiuSchoolStudent
- //
- // Created by 王智 on 2023/8/29.
- //
- #import "KSMediaMergeView.h"
- #import "KSMergeAudioControlView.h"
- #import "KSAudioAnimationView.h"
- #import <KSToolLibrary/KSMediaEditor.h>
- #import "KSPlayerView.h"
- #import "KSPlayerSliderView.h"
- #import <CloudAccompanyLibrary/kSNewPlayer.h>
- #import "KSVideoPlayerView.h"
- #import "KSAudioPlayAnimationView.h"
- #import "KSNewAlertView.h"
- #import "MusicPublistAlert.h"
- #import "KSMediaManager.h"
- #import <RSKImageCropper/RSKImageCropper.h>
- #import "KSVideoCropViewController.h"
- #import <KSToolLibrary/KSAudioSessionManager.h>
- @interface KSMediaMergeView ()<kSNewPlayerManagerDelegate,KSVideoPlayerViewDelegate,RSKImageCropViewControllerDelegate,RSKImageCropViewControllerDataSource>
- @property (nonatomic, strong) KSAudioAnimationView *animationView;
- @property (nonatomic, strong) KSMergeAudioControlView *contrlView;
- @property (nonatomic, strong) NSURL *videoUrl;
- @property (nonatomic, strong) NSURL *bgAudioUrl;
- @property (nonatomic, strong) NSURL *recordUrl;
- @property (nonatomic, assign) NSInteger offsetTime;
- @property (nonatomic, strong) KSPlayerView *playerView;
- @property (nonatomic, strong) KSPlayerSliderView *playControlView;
- @property (nonatomic, strong) kSNewPlayer *bgPlayer;
- @property (nonatomic, strong) kSNewPlayer *recordPlayer;
- @property (nonatomic, assign) BOOL isPause;
- @property (nonatomic, strong) NSString *filePath;
- @property (nonatomic, assign) NSInteger originalOffset;
- @property (nonatomic, strong) KSVideoPlayerView *videoView;
- @property (nonatomic, assign) BOOL isVideoPlay;
- @property (nonatomic, assign) BOOL isHideControlView;
- @property (nonatomic, strong) UIButton *showButton;
- @property (nonatomic, strong) KSAudioPlayAnimationView *playAnimationView;
- @property (nonatomic, strong) NSTimer *timer;
- // 远端资源
- @property (nonatomic, strong) NSString *remoteVideoUrl;
- @property (nonatomic, strong) NSString *remoteBgAudioUrl;
- @property (nonatomic, strong) NSString *remoteRecrodUrl;
- @property (nonatomic, strong) NSString *jsonConfig;
- @property (nonatomic, assign) NSInteger originalVolume;
- @property (nonatomic, assign) NSInteger accompanyVolume;
- @property (nonatomic, strong) dispatch_group_t requestGroup;
- @property (nonatomic, copy) DraftEditCallback draftCallback;
- @property (nonatomic, strong) NSDictionary *preJsonDic;
- @property (nonatomic, strong) KSNewAlertView *alertView;
- @property (nonatomic, assign) BOOL hasSave;
- @property (nonatomic, copy) MergeCallback mergeCallback;
- @property (nonatomic, strong) MusicPublistAlert *publishAlert;
- @property (nonatomic, strong) KSMediaManager *mediaManager;
- @property (nonatomic, strong) UIImage *settingImage; // 图片
- @property (nonatomic, assign) BOOL hasModify;
- @property (nonatomic, strong) UIImage *videoCoverImage;
- @property (nonatomic, assign) BOOL isChooseVideoCover;
- @property (nonatomic, strong) NSString *videoCoverUrl; // 视频封面
- @property (nonatomic, assign) BOOL isOtherLogin;
- @property (nonatomic, assign) NSInteger defaultDelay;
- @property (nonatomic, assign) CGFloat bgPlayerRate;
- @property (nonatomic, assign) NSInteger addTime;
- //@property (nonatomic, strong) UILabel *offsetTimeLabel;
- @property (nonatomic, assign) NSInteger evaluateDelay;
- @property (nonatomic, assign) BOOL fromDraftPage; // 是否从草稿页面进入
- @end
- @implementation KSMediaMergeView
- - (instancetype)init {
- self = [super init];
- if (self) {
- self.hasModify = NO;
- self.bgPlayerRate = 1.0;
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterBackground) name:@"appEnterBackground" object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(otherLogin) name:@"otherLogin" object:nil];
- self.addTime = 0;
- [self configAudioSession];
- }
- return self;
- }
- - (void)configAudioSession {
- [[[KSAudioSessionManager alloc] init] configAudioSession:AUDIOCONFIG_PLAYANDRECORD];
- }
- - (void)otherLogin {
- [self stopPlay];
- }
- - (void)appEnterBackground {
- [self stopPlay];
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- }
- return self;
- }
- - (void)configWithVideoUrl:(NSURL *)videoUrl bgAudioUrl:(NSURL *)bgAudioUrl remoteBgUrl:(NSString *)remoteBgUrl recordUrl:(NSURL *)recordUrl offsetTime:(NSInteger)offsetTime mergeCallback:(MergeCallback)callback {
- if (callback) {
- self.mergeCallback = callback;
- }
- if (videoUrl) {
- self.isVideoPlay = YES;
- }
- else {
- self.isVideoPlay = NO;
- }
- self.fromDraftPage = NO;
- self.remoteBgAudioUrl = remoteBgUrl;
- self.videoUrl = videoUrl;
- self.bgAudioUrl = bgAudioUrl;
- self.recordUrl = recordUrl;
- self.defaultDelay = -offsetTime; // default delay = evaluate delay
- self.evaluateDelay = -offsetTime; // 播放延迟和收音延迟
- self.originalOffset = 0;
- [self.contrlView configWithOffsetTime:0];
- self.contrlView.hideBackView = NO;
- [self configPlayer];
- [self configUI];
- }
- - (void)configRemoteVideoUrl:(NSString *)remoteVideoUrl bgAudioUrl:(NSString *)remoteBgAudioUrl recordUrl:(NSString *)remoteRecrodUrl jsonConfig:(NSString *)jsonConfig callback:(DraftEditCallback)callback {
- self.addTime = 0;
- if (callback) {
- self.draftCallback = callback;
- }
- if (![NSString isEmptyString:remoteVideoUrl]) {
- self.isVideoPlay = YES;
- }
- else {
- self.isVideoPlay = NO;
- }
- self.fromDraftPage = YES;
- self.remoteVideoUrl = remoteVideoUrl;
- self.remoteBgAudioUrl = remoteBgAudioUrl;
- self.remoteRecrodUrl = remoteRecrodUrl;
-
- self.preJsonDic = [jsonConfig mj_JSONObject];
- self.originalOffset = [self.preJsonDic ks_integerValueForKey:@"offset"];
- self.defaultDelay = [self.preJsonDic ks_integerValueForKey:@"defaultDelay"];
- self.evaluateDelay = [self.preJsonDic ks_integerValueForKey:@"evaluateDelay"]; // 播放延迟和收音延迟
- self.offsetTime = self.originalOffset;
- [self.contrlView configWithOffsetTime:self.originalOffset];
- self.contrlView.hideBackView = YES;
- [self downloadFileSource];
- }
- - (void)downloadFileSource {
- [LOADING_MANAGER showCustomLoading:@"资源下载中"];
-
- [self downloadVideo];
- [self downloadAccompany];
- [self downloadAudio];
- dispatch_group_notify(self.requestGroup, dispatch_get_main_queue(), ^{
- [LOADING_MANAGER removeCustomLoading];
- [self configPlayer];
- [self configUI];
- });
- }
- - (void)downloadVideo {
- if (![NSString isEmptyString:self.remoteVideoUrl]) {
- dispatch_group_enter(self.requestGroup);
- [KSNetworkingManager downloadFileRequestWithFileUrl:self.remoteVideoUrl progress:^(int64_t bytesRead, int64_t totalBytes) {
-
- } success:^(NSURL * _Nonnull fileUrl) {
- dispatch_group_leave(self.requestGroup);
- self.videoUrl = fileUrl;
- } faliure:^(NSError * _Nonnull error) {
- dispatch_group_leave(self.requestGroup);
- }];
- }
- }
- - (void)downloadAudio {
- if (![NSString isEmptyString:self.remoteRecrodUrl]) {
- dispatch_group_enter(self.requestGroup);
- [KSNetworkingManager downloadFileRequestWithFileUrl:self.remoteRecrodUrl progress:^(int64_t bytesRead, int64_t totalBytes) {
-
- } success:^(NSURL * _Nonnull fileUrl) {
- dispatch_group_leave(self.requestGroup);
- self.recordUrl = fileUrl;
- } faliure:^(NSError * _Nonnull error) {
- dispatch_group_leave(self.requestGroup);
- }];
- }
- }
- - (void)downloadAccompany {
- if (![NSString isEmptyString:self.remoteBgAudioUrl]) {
- dispatch_group_enter(self.requestGroup);
- [KSNetworkingManager downloadFileRequestWithFileUrl:self.remoteBgAudioUrl progress:^(int64_t bytesRead, int64_t totalBytes) {
-
- } success:^(NSURL * _Nonnull fileUrl) {
- dispatch_group_leave(self.requestGroup);
- self.bgAudioUrl = fileUrl;
- } faliure:^(NSError * _Nonnull error) {
- dispatch_group_leave(self.requestGroup);
- }];
- }
- }
- - (void)configPlayer {
- if (self.bgAudioUrl) {
- [self.bgPlayer preparePlayNativeSongWithPath:self.bgAudioUrl];
- }
-
- if (self.recordUrl) {
- [self.recordPlayer preparePlayNativeSongWithPath:self.recordUrl];
- }
- if (self.isVideoPlay) {
- [self.videoView preparePlayNativeVideoWithPath:self.videoUrl];
- self.videoView.isMute = YES;
- }
-
- // 音量同步
- NSInteger accompanyVolume = 100;
- if ([[self.preJsonDic allKeys] containsObject:@"accompanyVolume"]) {
- accompanyVolume = [self.preJsonDic ks_integerValueForKey:@"accompanyVolume"];
- }
- self.accompanyVolume = accompanyVolume;
- self.bgPlayer.volume = accompanyVolume / 100.0;
- NSInteger originalVolume = 100;
- if ([[self.preJsonDic allKeys] containsObject:@"originalVolume"]) {
- originalVolume = [self.preJsonDic ks_integerValueForKey:@"originalVolume"];
- }
- self.originalVolume = originalVolume;
- self.recordPlayer.volume = originalVolume / 100.0;
- [self.contrlView configRecordVolume:originalVolume bgVolume:accompanyVolume];
- }
- - (void)configUI {
- self.backgroundColor = [UIColor whiteColor];
- if (self.isVideoPlay) {
- [self addSubview:self.videoView];
- }
- else {
- [self addSubview:self.playerView];
- }
-
- [self addSubview:self.showButton];
- self.showButton.hidden = YES;
- self.showButton.userInteractionEnabled = NO;
-
- [self addSubview:self.contrlView];
- [self.contrlView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.top.bottom.mas_equalTo(self);
- make.width.mas_equalTo(289);
- }];
-
- if (self.isVideoPlay) {
- [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.bottom.mas_equalTo(self);
- make.right.mas_equalTo(self.contrlView.mas_left).offset(20);
- }];
- }
- else { // 音频
- // 添加投影
- UIImageView *shadowImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"merge_music_bg"]];
- [self.playerView addSubview:shadowImage];
-
-
- [self.playerView addSubview:self.playAnimationView];
- [self.playAnimationView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.mas_equalTo(self.playerView.mas_centerX);
- make.centerY.mas_equalTo(self.playerView.mas_centerY);
- make.width.mas_equalTo(424);
- make.height.mas_equalTo(80);
- }];
-
- [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.bottom.mas_equalTo(self);
- make.right.mas_equalTo(self.contrlView.mas_left).offset(20);
- }];
-
- [self.playerView addSubview:self.animationView];
- [self.animationView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.mas_equalTo(self.playerView.mas_centerX);
- make.centerY.mas_equalTo(self.playerView.mas_centerY);
- make.width.height.mas_equalTo(200);
- }];
- [shadowImage mas_makeConstraints:^(MASConstraintMaker *make) {
- make.width.mas_equalTo(284);
- make.height.mas_equalTo(151);
- make.centerX.mas_equalTo(self.playerView.mas_centerX);
- make.top.mas_equalTo(self.animationView.mas_top).offset(86);
- }];
- [self.animationView configWithImageWithUrl:self.coverImage];
-
- }
-
- [self addSubview:self.playControlView];
- [self.playControlView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.mas_left);
- make.right.mas_equalTo(self.contrlView.mas_left).offset(-20);
- make.height.mas_equalTo(44);
- make.bottom.mas_equalTo(self.mas_bottom).offset(-30);
- }];
-
- [self.playControlView configViewDisplayColor:self.isVideoPlay];
- NSString *imgName = self.isVideoPlay ? @"back_button_white" : @"back_black";
- UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [backButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
- [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:backButton];
- [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.width.height.mas_equalTo(44);
- make.left.mas_equalTo(self.mas_left).offset(10);
- make.top.mas_equalTo(self.mas_top).offset(10);
- }];
- float totalTime = [self.recordPlayer getTotalDuration];
- [self.playControlView configWithDuration:totalTime];
-
- UILabel *songLabel = [[UILabel alloc] init];
- songLabel.font = [UIFont systemFontOfSize:16.0f weight:UIFontWeightSemibold];
- songLabel.text = [NSString returnNoNullStringWithString:self.songName];
- songLabel.textColor = self.isVideoPlay ? HexRGB(0xffffff) : HexRGB(0x131415);
- [self addSubview:songLabel];
- [songLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(backButton.mas_right);
- make.centerY.mas_equalTo(backButton.mas_centerY);
- make.height.mas_equalTo(22);
- make.right.mas_greaterThanOrEqualTo(self.contrlView.mas_left).offset(20);
- }];
-
- // self.offsetTimeLabel = [[UILabel alloc] init];
- // self.offsetTimeLabel.font = [UIFont systemFontOfSize:14.0f weight:UIFontWeightSemibold];
- // self.offsetTimeLabel.textColor = self.isVideoPlay ? HexRGB(0xffffff) : HexRGB(0x131415);
- // [self addSubview:self.offsetTimeLabel];
- // [self.offsetTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.height.mas_equalTo(22);
- // make.centerX.mas_equalTo(self.mas_centerX);
- // make.centerY.mas_equalTo(songLabel.mas_centerY);
- // }];
-
- // [self startPlay];
- // 缓冲完成后播放
- }
- - (void)backAction {
- if (self.hasModify) {
- MJWeakSelf;
- [self.alertView configTitle:@"提示" descMessage:@"是否将本次录制的作品保存为草稿?" leftButtonTitle:@"取消" rightButtonTitle:@"确认" leftButtonAction:^{
- [weakSelf removeViewTips:NO];
- } rightButtonAction:^{
- [weakSelf saveCurrentDraft:YES];
- }];
- [self.alertView showAlert];
- }
- else {
- if (self.mergeCallback) {
- [self removeViewTips:NO];
- }
- else {
- [self removeView];
- }
- }
- }
- #pragma mark ----- lazying
- - (KSAudioAnimationView *)animationView {
- if (!_animationView) {
- _animationView = [KSAudioAnimationView shareInstance];
- }
- return _animationView;
- }
- - (KSPlayerView *)playerView {
- if (!_playerView) {
- _playerView = [[KSPlayerView alloc] init];
- }
- return _playerView;
- }
- - (KSPlayerSliderView *)playControlView {
- if (!_playControlView) {
- _playControlView = [KSPlayerSliderView shareInstance];
- MJWeakSelf;
- [_playControlView sliderControlAction:^(PLAYERTYPE type, NSInteger rate) {
- [weakSelf playSliderAction:type rate:rate];
- }];
- }
- return _playControlView;
- }
- - (void)startPlay {
- self.bgPlayerRate = 1.0;
- self.playControlView.isPlay = YES;
- self.animationView.isPlay = YES;
- NSInteger recordPlayerPosition = self.playControlView.playScheduleTime*1000;
- [self.recordPlayer seekToTimePlay:recordPlayerPosition];
-
- NSInteger realOffsetTime = self.offsetTime + self.evaluateDelay;
- NSInteger offsetTime = recordPlayerPosition + realOffsetTime;
- if (offsetTime >= 0) {
- [self.bgPlayer seekToTimePlay:offsetTime];
- }
- else {
- [self.bgPlayer seekToStart];
- @weakObj(self);
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((labs(offsetTime) + self.addTime) * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
- @strongObj(self);
- // NSLog(@"--bgPlayer start---");
- if (self.playControlView.isPlay == YES) {
- [self.bgPlayer resumePlay];
- [self snapPlayerProgress];
- }
- });
- }
-
- if (self.isVideoPlay) {
- [self.videoView seekToTimePlay:recordPlayerPosition];
- }
- else {
- [self.playAnimationView startAnimation];
- [self startTimer];
- }
- }
- - (void)stopPlay {
- self.animationView.isPlay = NO;
- self.playControlView.isPlay = NO;
- [self.bgPlayer puasePlay];
- [self.recordPlayer puasePlay];
- if (self.isVideoPlay) {
- [self.videoView puasePlay];
- }
- else {
- [self.playAnimationView stopAnimation];
- [self stopTimer];
- }
- }
- - (void)playSliderAction:(PLAYERTYPE)type rate:(NSInteger)rate {
- switch (type) {
- case PLAYERTYPE_PLAY:
- {
- [self startPlay];
- }
- break;
- case PLAYERTYPE_PAUSE:
- {
- [self stopPlay];
- }
- break;
- case PLAYERTYPE_RATE:
- {
- [self.recordPlayer seekOffsetTime:rate*1000];
-
- NSInteger realOffsetTime = self.offsetTime + self.evaluateDelay;
- NSInteger offsetTime = rate*1000 + realOffsetTime;
- if (offsetTime >= 0) {
- [self.bgPlayer seekOffsetTime:offsetTime];
- }
- else {
- [self.bgPlayer puasePlay];
- if (self.recordPlayer.isPlaying) {
- [self.bgPlayer seekToStart];
- @weakObj(self);
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((labs(offsetTime) + self.addTime) * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
- @strongObj(self);
- if (self.playControlView.isPlay == YES) {
- [self.bgPlayer resumePlay];
- [self snapPlayerProgress];
- }
-
- });
- }
- else {
- [self.bgPlayer seekToStart];
- }
- }
-
- if (self.isVideoPlay) {
- [self.videoView seekOffsetTime:rate*1000];
- }
- }
- break;
- default:
- break;
- }
- }
- // 对齐播放延迟
- - (void)snapPlayerProgress {
- @weakObj(self);
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(500 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
- @strongObj(self);
- if (self.playControlView.isPlay == YES) {
- CMTime time = [self.recordPlayer getCurrentPlayTime];
- NSInteger newDelayTime = CMTimeGetSeconds(time) * 1000 + self.offsetTime + self.evaluateDelay;
- [self.recordPlayer seekOffsetTimeNoPuase:CMTimeGetSeconds(time) * 1000];
- [self.bgPlayer seekOffsetTime:newDelayTime];
- }
-
- });
- }
- - (void)startTimer {
- [self.timer setFireDate:[NSDate distantPast]];
- }
- - (void)stopTimer {
- [self.timer setFireDate:[NSDate distantFuture]];
- }
- - (KSMergeAudioControlView *)contrlView {
- if (!_contrlView) {
- _contrlView = [KSMergeAudioControlView shareIntance];
- MJWeakSelf;
- [_contrlView controlActionCallback:^(MERGEACTION type, NSInteger recordVolume, NSInteger bgVolume, NSInteger offsetTime) {
- [weakSelf megerAction:type recordVolume:recordVolume bgVolume:bgVolume offsetTime:offsetTime];
- }];
- }
- return _contrlView;
- }
- - (void)megerAction:(MERGEACTION)action recordVolume:(NSInteger)recordVolume bgVolume:(NSInteger)bgVolume offsetTime:(NSInteger)offsetTime {
- switch (action) {
- case MERGEACTION_CANCLE:
- {
- [self backAction];
- }
- break;
- case MERGEACTION_MODIFY:
- {
- self.hasModify = YES;
- self.accompanyVolume = bgVolume;
- self.originalVolume = recordVolume;
-
- self.bgPlayer.volume = bgVolume / 100.0;
- self.recordPlayer.volume = recordVolume / 100.0;
- }
- break;
- case MERGEACTION_DELAY: // offset
- {
- self.hasModify = YES;
- if (self.bgPlayer.isPlaying) {
- CMTime time = [self.recordPlayer getCurrentPlayTime];
- NSInteger newDelayTime = CMTimeGetSeconds(time) * 1000 + offsetTime + self.evaluateDelay;
- [self.recordPlayer seekOffsetTime:CMTimeGetSeconds(time) * 1000];
- [self.bgPlayer seekOffsetTime:newDelayTime];
- }
-
- self.offsetTime = offsetTime;
- }
- break;
- case MERGEACTION_SAVE: // 保存
- {
- // 暂停播放
- [self stopPlay];
- [self saveCurrentDraft:NO];
- }
- break;
- case MERGEACTION_UPLOAD: // 上传
- {
- [self showPublishAlert];
-
- }
- break;
- case MERGEACTION_HIDEVIEW:
- {
- [self hideControlView];
- }
- break;
- default:
- break;
- }
- }
- - (void)showPublishAlert {
- self.publishAlert = [MusicPublistAlert shareInstance];
- [self.publishAlert configUI:self.isVideoPlay];
- [self.publishAlert evaluateMusicName:self.songName userName:UserDefault(NicknameKey) musicImage:self.coverImage];
- MJWeakSelf;
- [self.publishAlert chooseAction:^(PUBLISH_ACTION type) {
- [weakSelf publishAction:type];
- }];
- [self.publishAlert showAlert];
- }
- - (void)publishAction:(PUBLISH_ACTION)type {
- switch (type) {
- case PUBLISH_ACTION_PUBLISH:
- {
- // 暂停播放
- [self stopPlay];
- self.desc = [NSString isEmptyString:self.publishAlert.publishContainView.textView.text] ? @"我发布了一首演奏作品,快来听听吧~" :self.publishAlert.publishContainView.textView.text;
- if (self.settingImage || self.videoCoverImage) { // 上传图片
- [self updateWithCoverImage];
- }
- else { // 直接上传
- [self publishMusic];
- }
- }
- break;
- case PUBLISH_ACTION_CHOOSEIMG:
- {
- // 暂停播放
- [self stopPlay];
- self.isChooseVideoCover = NO;
- // 调用相册
- self.mediaManager = [[KSMediaManager alloc] init];
- self.mediaManager.mediaType = MEDIATYPE_PHOTO;
- self.mediaManager.maxPhotoNumber = 1;
- self.mediaManager.baseCtrl = [self findViewController];
- self.mediaManager.needCropImage = NO;
- MJWeakSelf;
- [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) {
- // 裁剪
- UIImage *sendImg = [imageArray lastObject];
- // 跳转到裁剪功能
- RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:sendImg cropMode:RSKImageCropModeCustom];
- imageCropVC.avoidEmptySpaceAroundImage = YES;
- imageCropVC.rotationEnabled = YES;
- imageCropVC.delegate = weakSelf;
- imageCropVC.dataSource = weakSelf;
- imageCropVC.modalPresentationStyle = UIModalPresentationFullScreen;
- UIViewController *baseCtrl = [weakSelf findViewController];
- [baseCtrl.navigationController presentViewController:imageCropVC animated:YES completion:nil];
- }];
- [self.mediaManager pushImagePickerController];
- }
- break;
- case PUBLISH_ACTION_VIDEOCOVER: // 视频封面
- {
- // 暂停播放
- [self stopPlay];
- self.isChooseVideoCover = YES;
- // 调用相册
- self.mediaManager = [[KSMediaManager alloc] init];
- self.mediaManager.mediaType = MEDIATYPE_PHOTO;
- self.mediaManager.maxPhotoNumber = 1;
- self.mediaManager.baseCtrl = [self findViewController];
- self.mediaManager.needCropImage = NO;
- MJWeakSelf;
- [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) {
- // 裁剪
- UIImage *sendImg = [imageArray lastObject];
- // 跳转到裁剪功能
- RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:sendImg cropMode:RSKImageCropModeCustom];
- imageCropVC.avoidEmptySpaceAroundImage = YES;
- imageCropVC.rotationEnabled = YES;
- imageCropVC.delegate = weakSelf;
- imageCropVC.dataSource = weakSelf;
- imageCropVC.modalPresentationStyle = UIModalPresentationFullScreen;
- UIViewController *baseCtrl = [weakSelf findViewController];
- [baseCtrl.navigationController presentViewController:imageCropVC animated:YES completion:nil];
- }];
- [self.mediaManager pushImagePickerController];
- }
- break;
- case PUBLISH_ACTION_VIDEOCROP:
- {
- // 暂停播放
- [self stopPlay];
- KSVideoCropViewController *ctrl = [[KSVideoCropViewController alloc] init];
- [ctrl configWithVideoPath:self.videoUrl];
- UIViewController *baseCtrl = [self findViewController];
- MJWeakSelf;
- [ctrl chooseCropImageCallback:^(UIImage * _Nonnull cropImage) {
- [weakSelf displayCoverImage:cropImage];
- }];
- ctrl.modalPresentationStyle = UIModalPresentationFullScreen;
- [baseCtrl.navigationController presentViewController:ctrl animated:YES completion:nil];
- }
- break;
- case PUBLISH_ACTION_CANCLE:
- {
- self.settingImage = nil;
- self.videoCoverImage = nil;
- }
- break;
- default:
- break;
- }
- }
- - (void)displayCoverImage:(UIImage *)image {
- self.videoCoverImage = image;
- [self.publishAlert.coverView.coverImage setImage:self.videoCoverImage];
- }
- - (void)updateWithCoverImage {
-
- [self uploadMusicCover];
- }
- - (void)uploadMusicCover {
- NSString *tips = self.isVideoPlay ? @"视频合成中" : @"音频合成中";
- [LOADING_MANAGER showProgressLoading:tips progress:0];
- if (self.settingImage) {
- NSData *imgData = [UIImage turnsImaegDataByImage:self.settingImage];
- NSString *fileName = @"musicCoverImg";
- [UPLOAD_MANAGER configWithfilePath:@"klx"];
- [[KSUploadManager shareInstance] uploadImage:imgData fileName:fileName successCallback:^(NSMutableArray * _Nonnull fileUrlArray) {
- NSString *avatarUrl = [fileUrlArray lastObject];
- self.coverImage = avatarUrl;
- [self uploadVideoCover];
- } faliure:^(NSError * _Nullable error, NSString *descMessaeg) {
- if (![NSString isEmptyString:descMessaeg]) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:descMessaeg];
- }
- [self uploadVideoCover];
- }];
- }
- else {
- [self uploadVideoCover];
- }
- }
- - (void)uploadVideoCover {
- if (self.videoCoverImage) {
- NSData *imgData = [UIImage turnsImaegDataByImage:self.videoCoverImage];
- NSString *fileName = @"musicCoverImg";
- [UPLOAD_MANAGER configWithfilePath:@"klx"];
- [[KSUploadManager shareInstance] uploadImage:imgData fileName:fileName successCallback:^(NSMutableArray * _Nonnull fileUrlArray) {
- NSString *avatarUrl = [fileUrlArray lastObject];
- self.videoCoverUrl = avatarUrl;
- [self publishMusic];
- } faliure:^(NSError * _Nullable error, NSString *descMessaeg) {
- if (![NSString isEmptyString:descMessaeg]) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:descMessaeg];
- }
- [self publishMusic];
- }];
- }
- else {
- [self publishMusic];
- }
-
- }
- - (void)refreshViewCoverImg:(NSMutableArray *)imageArray {
- UIImage *img = [imageArray lastObject];
- self.settingImage = img;
- [self.publishAlert.publishContainView.musicImage setImage:self.settingImage];
- }
- - (void)publishMusic {
- CGFloat progress = 1.0/3;
- CGFloat rate = 1.0/3;
- if (self.isVideoPlay) {
- [LOADING_MANAGER showProgressLoading:@"视频合成中" progress:0];
- CGFloat offsetTime = self.offsetTime + self.evaluateDelay;
- [KSMediaEditor mixVideoWithRecordAudio:self.recordUrl recordVolume:self.recordPlayer.volume bgAudio:self.bgAudioUrl bgAudioVolume:self.bgPlayer.volume offsetTime:offsetTime videoUrlStr:self.videoUrl completion:^(NSString * _Nonnull outPath, BOOL isSuccess, NSString * _Nonnull desc) {
- [LOADING_MANAGER showProgressLoading:@"视频合成中" progress:progress];
- // 保存文件到指定文件夹
- if (isSuccess) {
- self.filePath = outPath;
- MJWeakSelf;
- [self sendVideoActionWithUrl:outPath isFormal:YES startProgress:progress rate:rate success:^(NSString * _Nonnull uploadVideoUrl) {
- [weakSelf saveMusic:YES isFormal:YES fileUrl:uploadVideoUrl progress:progress*2];
- } failure:^(NSString * _Nonnull desc) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"上传失败"];
- }];
- }
- else {
- [LOADING_MANAGER removeProgressLoading];
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"视频合成失败"];
- }
- }];
- }
- else {
- [LOADING_MANAGER showProgressLoading:@"音频合成中" progress:0];
- NSInteger realOffsetTime = self.offsetTime + self.evaluateDelay;
- [KSMediaEditor mixRecordAudio:self.recordUrl recordVolume:self.recordPlayer.volume bgAudio:self.bgAudioUrl bgAudioVolume:self.bgPlayer.volume offsetTime:realOffsetTime completion:^(NSString * _Nonnull outPath, BOOL isSuccess, NSString * _Nonnull desc) {
- // 保存文件到指定文件夹
- [LOADING_MANAGER showProgressLoading:@"音频合成中" progress:progress];
- if (isSuccess) {
- self.filePath = outPath;
- MJWeakSelf;
- [self sendAudioWithPath:outPath isFormal:YES startProgress:progress rate:rate success:^(NSString *audioUrl) {
- [weakSelf saveMusic:NO isFormal:YES fileUrl:audioUrl progress:progress*2];
- } failure:^(NSString *desc) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"上传失败"];
- }];
- }
- else {
- [LOADING_MANAGER removeProgressLoading];
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"音频合成失败"];
- }
- }];
- }
-
- }
- - (void)saveCurrentDraft:(BOOL)needBack {
- self.hasModify = NO;
- if (self.isVideoPlay) {
- if (self.remoteVideoUrl) {
- // 保存草稿
- [LOADING_MANAGER showProgressLoading:@"正在保存草稿" progress:0 promptCompletion:^{
- [self saveMusic:YES isFormal:NO fileUrl:self.remoteVideoUrl needBack:needBack progress:0.5];
- }];
- }
- else {
- // 合成
- [LOADING_MANAGER showProgressLoading:@"正在上传草稿" progress:0];
- MJWeakSelf;
- [KSMediaEditor mixRecordVideoWithAudio:self.recordUrl recordVolume:100 videoUrlStr:self.videoUrl completion:^(NSString * _Nonnull outPath, BOOL isSuccess, NSString * _Nonnull desc) {
- if (isSuccess) {
- [self sendVideoActionWithUrl:outPath isFormal:NO startProgress:0 rate:0.5 success:^(NSString * _Nonnull uploadVideoUrl) {
- // 保存草稿
- [weakSelf saveMusic:YES isFormal:NO fileUrl:uploadVideoUrl needBack:needBack progress:0.5];
-
- } failure:^(NSString * _Nonnull desc) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"上传失败"];
- }];
- }
- else {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"上传失败"];
- }
- }];
- }
- }
- else {
- if (self.remoteRecrodUrl) {
- [LOADING_MANAGER showProgressLoading:@"正在保存草稿" progress:0 promptCompletion:^{
- [self saveMusic:NO isFormal:NO fileUrl:self.remoteRecrodUrl needBack:needBack progress:0.5];
- }];
- }
- else {
- MJWeakSelf;
- [LOADING_MANAGER showProgressLoading:@"正在上传草稿" progress:0];
- [self sendAudioWithURLPath:self.recordUrl isFormal:NO startProgress:0 rate:0.5 success:^(NSString *audioUrl) {
- // 保存草稿
- [weakSelf saveMusic:NO isFormal:NO fileUrl:audioUrl needBack:needBack progress:0.5];
- } failure:^(NSString *desc) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"上传失败"];
- }];
- }
- }
- }
- - (void)removeViewTips:(BOOL)needTips {
- [self freePlayer];
- [self removeFromSuperview];
- if (self.draftCallback) {
- self.draftCallback();
- }
- if (needTips && self.mergeCallback) {
- self.mergeCallback();
- }
- }
- - (void)removeView {
- [self removeViewTips:YES];
- }
- - (void)freePlayer {
- if (_bgPlayer) {
- [self.bgPlayer freePlayer];
- }
- if (_recordPlayer) {
- [self.recordPlayer freePlayer];
- }
- if (_videoView) {
- [self.videoView freePlayer];
- }
- }
- - (kSNewPlayer *)bgPlayer {
- if (!_bgPlayer) {
- _bgPlayer = [[kSNewPlayer alloc] init];
- _bgPlayer.delegate = self;
- }
- return _bgPlayer;
- }
- - (kSNewPlayer *)recordPlayer {
- if (!_recordPlayer) {
- _recordPlayer = [[kSNewPlayer alloc] init];
- _recordPlayer.delegate = self;
- }
- return _recordPlayer;
- }
- #pragma mark ----- player delegate
- - (void)videoPlayerIsReadyPlay:(AVPlayer *)player {
-
- }
- - (void)playerIsReadyPlay:(kSNewPlayer *)player {
- if (self.bgPlayer.isReady && self.recordPlayer.isReady) {
- [self startPlay];
- }
- }
- - (void)getSongCurrentTime:(NSInteger)currentTime andTotalTime:(NSInteger)totalTime andProgress:(CGFloat)progress currentInterval:(NSTimeInterval)currentInterval playTime:(NSTimeInterval)playTime inPlayer:(kSNewPlayer *_Nonnull)player {
- if (player == self.recordPlayer) {
- // NSLog(@"--- ---- ----- %f", playTime);
- self.playControlView.playScheduleTime = (NSInteger)(playTime / 1000);
- NSInteger realOffset = (NSInteger)(CMTimeGetSeconds([self.bgPlayer getCurrentPlayTime]) *1000 - CMTimeGetSeconds([self.recordPlayer getCurrentPlayTime])*1000);
- // NSLog(@" offset ---- %ld" , realOffset);
- // 如果延迟大于11ms 调整
- NSInteger expectOffset = self.offsetTime + self.evaluateDelay;
- NSLog(@"---- expectOffset == %@", [NSString stringWithFormat:@"实际: %ld, 期望偏差 %ld", realOffset, expectOffset]);
- // self.offsetTimeLabel.text = [NSString stringWithFormat:@"实际: %ld, 期望偏差 %ld", realOffset, expectOffset];
- // if (labs(labs(realOffset) - labs(expectOffset)) >= 11) { // 需要调整
- //
- // if (labs(realOffset) < labs(expectOffset)) { // 要扩大差距
- // if (expectOffset > 0) { // 300
- // if (self.bgPlayerRate != 1.02) {
- // self.bgPlayerRate = 1.02;
- // [self.bgPlayer configPlayerRate:1.02];
- // [self.recordPlayer configPlayerRate:0.98];
- // }
- // }
- // else { // expectOffset < 0 -300
- // if (self.bgPlayerRate != 0.98) {
- // self.bgPlayerRate = 0.98;
- // [self.bgPlayer configPlayerRate:0.98];
- // [self.recordPlayer configPlayerRate:1.02];
- // }
- // }
- // }
- // else { // 需要减小差距
- // if (realOffset > 0) {
- // if (self.bgPlayerRate != 0.98) {
- // self.bgPlayerRate = 0.98;
- // [self.bgPlayer configPlayerRate:0.98];
- // [self.recordPlayer configPlayerRate:1.02];
- // }
- // }
- // else {
- // if (self.bgPlayerRate != 1.02) {
- // self.bgPlayerRate = 1.02;
- // [self.bgPlayer configPlayerRate:1.02];
- // [self.recordPlayer configPlayerRate:0.98];
- // }
- // }
- // }
- // }
- // else {
- // if (self.bgPlayerRate != 1.0) {
- // self.bgPlayerRate = 1.0;
- // [self.recordPlayer configPlayerRate:1.0];
- // [self.bgPlayer configPlayerRate:1.0];
- // }
- // }
- }
- else {
- // NSLog(@"------- bgPlayer ----- %f", playTime);
- }
- }
- - (void)playFinished:(kSNewPlayer *)player {
- if (player == self.recordPlayer) {
-
- [self.recordPlayer puasePlay];
- [self.recordPlayer seekToStart];
-
- [self.bgPlayer puasePlay];
- [self.bgPlayer seekToStart];
-
- self.animationView.isPlay = NO;
- self.playControlView.isPlay = NO;
- [self.playAnimationView stopAnimation];
- [self stopTimer];
- if (self.isVideoPlay) {
- [self.videoView puasePlay];
- [self.videoView seekToStart];
- }
- }
- }
- - (void)playerDidError:(kSNewPlayer *)player {
- [self stopPlay];
- }
- - (KSVideoPlayerView *)videoView {
- if (!_videoView) {
- _videoView = [[KSVideoPlayerView alloc] init];
- _videoView.delegate = self;
- }
- return _videoView;
- }
- - (UIButton *)showButton {
- if (!_showButton) {
- _showButton = [UIButton buttonWithType:UIButtonTypeCustom];
- _showButton.frame = CGRectMake(KLandscapeWidth-30, (KLandscapeHeight-30)/2.0, 30, 30);
- [_showButton setImage:[UIImage imageNamed:@"mergeControl_show"] forState:UIControlStateNormal];
- [_showButton addTarget:self action:@selector(showControlView) forControlEvents:UIControlEventTouchUpInside];
- }
- return _showButton;
- }
- - (void)hideControlView {
- [UIView animateWithDuration:0.5f animations:^{
- [self.contrlView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.bottom.mas_equalTo(self);
- make.width.mas_equalTo(289);
- make.right.mas_equalTo(self.mas_right).offset(289);
- }];
- } completion:^(BOOL finished) {
- self.showButton.hidden = NO;
- self.showButton.userInteractionEnabled = YES;
- }];
- }
- - (void)showControlView {
- [UIView animateWithDuration:0.5f animations:^{
- [self.contrlView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.top.bottom.mas_equalTo(self);
- make.width.mas_equalTo(289);
- }];
- } completion:^(BOOL finished) {
- self.showButton.hidden = YES;
- self.showButton.userInteractionEnabled = NO;
- }];
- }
- - (void)saveMusic:(BOOL)isVideo isFormal:(BOOL)isFormal fileUrl:(NSString *)fileUrl progress:(CGFloat)progress {
-
- [self saveMusic:isVideo isFormal:isFormal fileUrl:fileUrl needBack:NO progress:progress];
- }
- - (void)saveMusic:(BOOL)isVideo isFormal:(BOOL)isFormal fileUrl:(NSString *)fileUrl needBack:(BOOL)needBack progress:(CGFloat)progress {
- NSString *type = isFormal ? @"FORMAL" : @"DRAFT";
- NSMutableDictionary *parm = [NSMutableDictionary dictionary];
- [parm setValue:@(self.offsetTime) forKey:@"offset"];
- [parm setValue:@(self.originalVolume) forKey:@"originalVolume"];
- [parm setValue:@(self.accompanyVolume) forKey:@"accompanyVolume"];
- [parm setValue:@(self.defaultDelay) forKey:@"defaultDelay"];
- [parm setValue:@(self.evaluateDelay) forKey:@"evaluateDelay"];
- self.jsonConfig = [parm mj_JSONString];
- [KSNetworkingManager saveMusicMessage:KS_POST jsonConfig:self.jsonConfig img:self.coverImage videoUrl:fileUrl accompanyUrl:self.remoteBgAudioUrl desc:self.desc type:type musicPracticeRecordId:self.recordId videoImg:self.videoCoverUrl success:^(NSDictionary * _Nonnull dic) {
- if ([dic ks_integerValueForKey:@"code"] == 200) {
- if (isFormal) {
- [LOADING_MANAGER KSShowProgressMsg:@"发布成功" promptCompletion:^{
- [self removeView];
- }];
- }
- else {
- [LOADING_MANAGER KSShowProgressMsg:@"保存成功" promptCompletion:^{
- self.hasModify = NO;
- if (needBack) {
- [self removeViewTips:NO];
- }
- else { // 从云教练进入 点击保存 显示保存弹窗
- if (self.fromDraftPage == NO) {
- [self showSaveDraftTipsAlert];
- }
- }
- }];
- }
- }
- else {
- [LOADING_MANAGER removeProgressLoading];
- [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
- }
- } faliure:^(NSError * _Nonnull error) {
- [LOADING_MANAGER removeProgressLoading];
- }];
- }
- - (void)showSaveDraftTipsAlert {
- MJWeakSelf;
- [self.alertView configTitle:@"提示" descMessage:@"已成功保存到草稿,草稿7天未发布将自动删除。" leftButtonTitle:@"确认" rightButtonTitle:@"查看草稿" leftButtonAction:^{
-
- } rightButtonAction:^{
- [weakSelf displayDraft];
- }];
- [self.alertView showAlert];
- }
- - (void)displayDraft {
-
- // 跳转到我的作品
- UIViewController *baseCtrl = [self findViewController];
- [baseCtrl.navigationController popToRootViewControllerAnimated:YES];
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:@"DisplayDraft" object:nil];
- });
-
- }
- - (void)sendVideoActionWithUrlPath:(NSURL *)fileUrl isFormal:(BOOL)isFormal beginProgress:(CGFloat)beginProgress rate:(CGFloat)rate success:(void (^)(NSString * _Nonnull uploadVideoUrl))success failure:(void (^)(NSString * _Nonnull desc))faliure {
- NSString *tips = isFormal ? @"正在上传作品" : @"正在上传草稿";
- [LOADING_MANAGER showProgressLoading:tips progress:beginProgress];
- NSData *fileData = [NSData dataWithContentsOfURL:fileUrl];
- NSString *suffix = [NSString stringWithFormat:@".%@",[fileUrl pathExtension]];
- [UPLOAD_MANAGER configWithfilePath:@"klx"];
- [[KSUploadManager shareInstance] videoUpload:fileData fileName:@"video" fileSuffix:suffix progress:^(int64_t bytesWritten, int64_t totalBytes) {
- // 显示进度
- float progress = (bytesWritten*1.0 / totalBytes) * rate + beginProgress;
- dispatch_main_async_safe(^{
- [LOADING_MANAGER showProgressNoAnimationLoading:tips progress:progress];
- });
- } successCallback:^(NSMutableArray * _Nonnull fileUrlArray) {
- dispatch_main_async_safe(^{
- NSString *fileUrl = [fileUrlArray lastObject];
- success(fileUrl);
- });
-
-
- } faliure:^(NSError * _Nullable error, NSString * _Nullable descMessaeg) {
- dispatch_main_async_safe(^{
- [LOADING_MANAGER removeProgressLoading];
- faliure(descMessaeg);
- });
-
- }];
- }
- - (void)sendVideoActionWithUrl:(NSString *)fileUrl isFormal:(BOOL)isFormal startProgress:(CGFloat)beginProgress rate:(CGFloat)rate success:(void (^)(NSString * _Nonnull uploadVideoUrl))success failure:(void (^)(NSString * _Nonnull desc))faliure {
- [self sendVideoActionWithUrlPath:[NSURL fileURLWithPath:fileUrl] isFormal:isFormal beginProgress:beginProgress rate:rate success:success failure:faliure];
- }
- - (void)sendAudioWithURLPath:(NSURL *)fileUrl isFormal:(BOOL)isFormal startProgress:(CGFloat)beginProgress rate:(CGFloat)rate success:(void(^)(NSString *audioUrl))success failure:(void(^)(NSString *desc))faliure {
- NSString *tips = isFormal ? @"正在上传作品" : @"正在上传草稿";
- [LOADING_MANAGER showProgressLoading:tips progress:beginProgress];
- NSData *fileData = [NSData dataWithContentsOfURL:fileUrl];
- NSString *suffix = [NSString stringWithFormat:@".%@",[fileUrl pathExtension]];
- [UPLOAD_MANAGER configWithfilePath:@"klx"];
- [UPLOAD_MANAGER uploadFile:fileData fileName:@"evaluateAudio" fileSuffix:suffix progress:^(int64_t bytesWritten, int64_t totalBytes) {
- // 显示进度
- float progress = (bytesWritten*1.0 / totalBytes) * rate + beginProgress;
- dispatch_main_async_safe(^{
- [LOADING_MANAGER showProgressNoAnimationLoading:tips progress:progress];
- });
- } successCallback:^(NSMutableArray * _Nonnull fileUrlArray) {
- NSString *fileUrl = [fileUrlArray lastObject];
- success(fileUrl);
- } faliure:^(NSError * _Nullable error, NSString * _Nullable descMessaeg) {
- [LOADING_MANAGER removeProgressLoading];
- faliure(descMessaeg);
- }];
-
- }
- - (void)sendAudioWithPath:(NSString *)filePath isFormal:(BOOL)isFormal startProgress:(CGFloat)beginProgress rate:(CGFloat)rate success:(void(^)(NSString *audioUrl))success failure:(void(^)(NSString *desc))faliure {
- NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
- [self sendAudioWithURLPath:fileUrl isFormal:isFormal startProgress:beginProgress rate:rate success:success failure:faliure];
- }
- - (void)removeVideoWithPath:(NSString *)videoUrl {
- NSFileManager *fileMamager = [NSFileManager defaultManager];
- if ([fileMamager fileExistsAtPath:videoUrl]) {
- [fileMamager removeItemAtPath:videoUrl error:nil];
- }
- }
- - (KSAudioPlayAnimationView *)playAnimationView {
- if (!_playAnimationView) {
- _playAnimationView = [[KSAudioPlayAnimationView alloc] init];
- _playAnimationView.frame = CGRectMake(0, 0, 424, 80);
- }
- return _playAnimationView;
- }
- - (NSTimer *)timer{
-
- if (!_timer) {
- MJWeakSelf;
- _timer = [NSTimer scheduledTimerWithTimeInterval:0.5f repeats:YES block:^(NSTimer * _Nonnull timer) {
- [weakSelf timerAction];
- }];
- [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
- [_timer setFireDate:[NSDate distantFuture]];
- }
- return _timer;
- }
- - (void)timerAction {
- float value = drand48();
- [self.playAnimationView setSoundsValue:value];
- }
- #pragma mark -- 重置定时器
- - (void)resetTimer{
- [self.timer setFireDate:[NSDate distantPast]];
- [_timer invalidate];
- _timer = nil;
- }
- - (void)removeAll{
-
- if (_timer) {
- [_timer invalidate];
- _timer = nil;
- }
- }
- - (void)dealloc {
- [self removeAll];
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark ----- lazy
- - (dispatch_group_t)requestGroup {
- if (!_requestGroup) {
- _requestGroup = dispatch_group_create();
- }
- return _requestGroup;
- }
- - (KSNewAlertView *)alertView {
- if (!_alertView) {
- _alertView = [KSNewAlertView shareInstance];
-
- }
- return _alertView;
- }
- - (UIViewController *)findViewController {
- for (UIView *view = self; view; view = view.superview) {
- UIResponder *nextResponder = [view nextResponder];
- if ([nextResponder isKindOfClass:[UIViewController class]]) {
- return (UIViewController *)nextResponder;
- }
- }
- return nil;
- }
- #pragma mark --- RSKImageCropViewControllerDelegate
- - (CGRect)imageCropViewControllerCustomMaskRect:(RSKImageCropViewController *)controller {
- CGFloat height = KLandscapeHeight - 80;
-
- CGFloat width = height;
- if (self.isChooseVideoCover) {
- width = height / 9 * 16;
- }
- return CGRectMake((KLandscapeWidth - width) / 2.0f, (KLandscapeHeight - height) / 2.0f, width, height);
- }
- - (UIBezierPath *)imageCropViewControllerCustomMaskPath:(RSKImageCropViewController *)controller {
- //返回裁剪框的位置
- CGFloat height = KLandscapeHeight - 80;
- CGFloat width = height;
- if (self.isChooseVideoCover) {
- width = height / 9 * 16;
- }
- UIBezierPath *path=[UIBezierPath bezierPathWithRoundedRect:CGRectMake((KLandscapeWidth - width) / 2.0f, (KLandscapeHeight - height) / 2.0f, width, height) cornerRadius:0];
- return path;
- }
- /**
- 返回一个图片可以移动的矩形区域
- 仅在RSKImageCropModeCustom下有效
- 如果你想支持图片旋转,则必须实现该方法
- */
- - (CGRect)imageCropViewControllerCustomMovementRect:(RSKImageCropViewController *)controller
- {
- return controller.maskRect;
- }
- /**
- 通知 delegate 取消裁剪图像。
- */
- - (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller {
- self.mediaManager.baseCtrl.zh_statusBarHidden = NO;
- if (self.isChooseVideoCover) {
- self.isChooseVideoCover = NO;
- }
- [controller dismissViewControllerAnimated:YES completion:nil];
- }
- /**
- 通知 delegate 原始图像已经被裁剪。此外,还提供了一个裁剪矩形和用于生成图像的旋转角度。
- */
- - (void)imageCropViewController:(RSKImageCropViewController *)controller didCropImage:(UIImage *)croppedImage usingCropRect:(CGRect)cropRect rotationAngle:(CGFloat)rotationAngle {
- self.mediaManager.baseCtrl.zh_statusBarHidden = NO;
- // croppedImage
- if (self.isChooseVideoCover) {
- self.isChooseVideoCover = NO;
- [self displayCoverImage:croppedImage];
- }
- else {
- self.settingImage = croppedImage;
- [self.publishAlert.publishContainView.musicImage setImage:self.settingImage];
- }
-
- [controller dismissViewControllerAnimated:YES completion:nil];
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|