| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845 |
- //
- // KSAccompanyWebViewController.m
- // KulexiuForTeacher
- //
- // Created by Kyle on 2022/3/20.
- //
- #import "KSAccompanyWebViewController.h"
- #import "WebViewBaseConfig.h" // 基础配置
- #import "SwiftImportHeader.h" // swift 桥接
- #import <KSAudioSessionManager.h> // audio session
- #import <KSAQRecordManager.h> // 录音
- #import "KSTargetWebSocketManager.h" // web socket
- #import <KSVideoRecordManager.h> // 视频录制
- #import <KSCloudBeatView.h> // 节拍器
- #import <MidiPlayerEngine.h> // midi 播放
- #import "AccompanyLoadingView.h"
- #define KSMidiSongFileKey (@"KSDownloadMidiSong")
- // 合成
- #import <KSMediaEditor.h>
- #import "KSAccompanyDraftViewController.h"
- #import <KSTunerLibrary/KSTunerLibrary-Swift.h>
- #import "KSLogManager.h"
- #import "AudioEnginePlayer.h"
- @interface KSAccompanyWebViewController ()<KSAQRecordManagerDelegate,KSAudioSessionManagerDelegate,PlayerEngineDelegate,TunerDelegate,AudioEnginePlayerDelegate>
- @property (nonatomic, strong) KSAudioSessionManager *audioSessionManager;
- @property (nonatomic, assign) MetronomeType beatType;
- @property (nonatomic, strong) MidiPlayerEngine *playerEngine; // player
- @property (nonatomic, assign) BOOL initEngineSuccess; // 加载引擎是否成功
- @property (nonatomic, assign) float songOriginalSpeed;
- @property (nonatomic, assign) float currentSpeed; // 当前播放的速度
- @property (nonatomic, assign) BOOL isPlaying; // 是否正在播放的状态
- @property (nonatomic, strong) NSString *currentSongId; // song id
- @property (nonatomic, strong) NSDictionary *configEngineParm; // 初始化mid播放引擎的参数
- @property (nonatomic, strong) NSMutableDictionary *metronomeParm; // 节拍器播放的参数
- // 是否发送了musicXML信息
- @property (nonatomic, assign) BOOL hasSendStartMessage;
- @property (nonatomic, strong) KSAQRecordManager *AQManager;
- @property (nonatomic, strong) KSTargetWebSocketManager *socketManager;
- @property (nonatomic, strong) NSMutableDictionary *evaluatParm;
- // 录制的message
- @property (nonatomic, strong) NSMutableDictionary *recordParm;
- // 评测开始时发送recordStart消息的标识
- @property (nonatomic, assign) BOOL isCompareStart;
- // 校音开始时发送 checkStart消息标识
- @property (nonatomic, assign) BOOL isSoundCheckStart;
- // 自定义UI控件容器
- @property (nonatomic, strong) UIView *viewContainer;
- @property (nonatomic, strong) KSVideoRecordManager *videoRecordManager;
- @property (nonatomic, strong) NSDictionary *endRecordParm;
- @property (nonatomic, assign) BOOL isCameraOpen;
- @property (nonatomic, strong) AccompanyLoadingView *loadingView;
- @property (nonatomic, assign) BOOL isTunerRuning;
- @property (nonatomic, strong) Tuner *tuner;
- // 延迟校准开始时发送 AdjustStart消息标识
- @property (nonatomic, assign) BOOL isDelayCheckStart;
- // 检测延迟播放器
- @property (nonatomic, strong) AudioEnginePlayer *delayCheckPlayer;
- // 音频播放器
- @property (nonatomic, strong) AudioEnginePlayer *musicPlayer;
- @property (nonatomic, assign) float musicSpeed; //播放速度
- // 录音开始时间
- @property (nonatomic, assign) NSTimeInterval recordStartTime;
- // 播放开始时间
- @property (nonatomic, assign) NSTimeInterval playerStartTime;
- // 播放延迟
- @property (nonatomic, assign) NSInteger offsetTime;
- @property (nonatomic, assign) BOOL checkPlayerReady;
- @property (nonatomic, assign) BOOL musicPlayerReady;
- @property (nonatomic, strong) NSDictionary *playerParm;
- @property (nonatomic, strong) NSMutableArray *delayArray;
- @property (nonatomic, assign) NSInteger checkIndex;
- @property (nonatomic, assign) NSInteger checkPrequence;
- @property (nonatomic, strong) NSURL *bgAudioUrl;
- @property (nonatomic, strong) NSURL *recordUrl;
- @property (nonatomic, strong) NSString *accompanyUrl;
- @property (nonatomic, assign) BOOL muteAccompany; // 是否静音
- @property (nonatomic, assign) NSInteger musicStartTime; // 开始播放的时间
- @property (nonatomic, assign) BOOL hasRecordMusicOffset; // 是否记录了播放延迟
- @end
- @implementation KSAccompanyWebViewController
- - (void)handerAudioInterruption {
- if (_playerEngine) {
- [self stopPlayAction];
- }
- if (_musicPlayer) {
- [self stopMp3Player];
- }
- }
- - (void)resumeAudioSession {
- // 恢复
- if (_playerEngine) {
- [self.playerEngine resumeAUGraph];
- }
- }
- // 打断处理
- - (void)audioInterruption {
- if (_videoRecordManager) {
- [self.videoRecordManager resetSession];
- }
- [self handerAudioInterruption];
- }
- - (void)ignorRecordVideo {
- if (_videoRecordManager) {
- self.videoRecordManager.skipSaveRecord = YES;
- }
- }
- - (void)stopSession {
- if (_videoRecordManager) {
- [self.videoRecordManager stopSession];
- }
- }
- - (void)resetPlayerConfig {
- [self freeMp3Player]; // 若之前有播放器,剔除
- self.checkPrequence = 800;
- self.checkPlayerReady = NO;
- self.musicPlayerReady = NO;
- self.recordStartTime = 0;
- self.playerStartTime = 0;
- self.offsetTime = 0;
- }
- - (void)downloadMp3File:(NSString *)musicUrl {
- if (![NSString isEmptyString:musicUrl]) {
- NSString *fileName = [musicUrl getUrlFileName];
- NSString *filePath = [self getAccompanyFilePathWithName:fileName];
- NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:filePath];
- if ([self checkSongHasSaveAccompanyWithSongUrl:musicUrl]) {
- [self configVideoRecord:fileUrl];
- [self initMusicPlayer:fileUrl];
- }
- else {
- MJWeakSelf;
- [self downloadUrl:musicUrl success:^{
- [weakSelf configVideoRecord:fileUrl];
- [weakSelf initMusicPlayer:fileUrl];
- } faliure:^{
-
- }];
- }
- }
- else {
- [self configVideoRecord:nil];
- }
- }
- - (void)initMusicPlayer:(NSURL *)musicUrl {
-
- [self.musicPlayer prepareNativeSongWithUrl:musicUrl];
- }
- - (void)downloadCheckMusic:(NSString *)checkMusicUrl {
- if (![NSString isEmptyString:checkMusicUrl]) {
- NSString *fileName = [checkMusicUrl getUrlFileName];
- NSString *filePath = [self getAccompanyFilePathWithName:fileName];
- NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:filePath];
- if ([self checkSongHasSaveAccompanyWithSongUrl:checkMusicUrl]) {
- [self initCheckPlayer:fileUrl];
- }
- else {
- MJWeakSelf;
- [self downloadUrl:checkMusicUrl success:^{
- [weakSelf initCheckPlayer:fileUrl];
- } faliure:^{
-
- }];
- }
- }
- }
- - (void)initCheckPlayer:(NSURL *)checkUrl {
- [self.delayCheckPlayer prepareNativeSongWithUrl:checkUrl];
- }
- - (void)initMp3Player:(NSString *)musicUrl checkUrl:(NSString *)checkUrl {
-
- [self resetPlayerConfig];
- self.accompanyUrl = musicUrl;
- [self downloadCheckMusic:checkUrl];
- }
- - (void)freeMp3Player {
- if (_delayCheckPlayer) {
- [self.delayCheckPlayer freePlayer];
- }
- if (_musicPlayer) {
- [self.musicPlayer freePlayer];
- }
- }
- - (void)stopMp3Player {
- if (_delayCheckPlayer) {
- [self.delayCheckPlayer stopPlay];
- }
- if (_musicPlayer) {
- [self.musicPlayer stopPlay];
- }
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [self configAudioSession];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterBackground) name:@"appEnterBackground" object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterForeground) name:@"appEnterForeground" object:nil];
- }
- - (void)checkMessage:(id)message {
- NSDictionary *result = [message mj_JSONObject];
- NSString *type = [[result ks_dictionaryValueForKey:@"header"] ks_stringValueForKey:@"type"];
- NSString *commond = [[result ks_dictionaryValueForKey:@"header"] ks_stringValueForKey:@"commond"];
- if ([type isEqualToString:@"DELAY_CHECK"] && [commond isEqualToString:@"recordEnd"]) {
- NSDictionary *body = [result ks_dictionaryValueForKey:@"body"];
- NSTimeInterval micDelay = [body ks_doubleValueForKey:@"firstNoteDelayDuration"] - self.offsetTime;
- if (micDelay > 0) {
- [self.delayArray addObject:[NSNumber numberWithDouble:micDelay]];
- }
- }
- }
- - (void)connectSocketService {
-
- MJWeakSelf;
- self.socketManager.didReceiveMessage = ^(id _Nonnull message) {
- dispatch_async(dispatch_get_main_queue(), ^{
- [weakSelf checkMessage:message];
- // api
- NSMutableDictionary *sendMessage = [NSMutableDictionary dictionary];
- [sendMessage setValue:@"sendResult" forKey:@"api"];
- [sendMessage setValue:message forKey:@"content"];
- // 服务返回数据传给H5
- [weakSelf postMessage:sendMessage];
- });
- };
- self.socketManager.connectionStatus = ^(BOOL isSuccess) {
- dispatch_async(dispatch_get_main_queue(), ^{
-
- if (!isSuccess) {
- // NSLog(@"-----连接失败");
- if (weakSelf.hasSendStartMessage) {
- if (weakSelf.AQManager.isRunning) {
- NSDictionary *postParm = @{@"api" : @"cancelEvaluating",
- @"content" : @{@"reson":@"服务异常,请重试"}
- };
- [weakSelf postMessage:postParm];
- [weakSelf stopRecordService];
- weakSelf.isCompareStart = NO;
- weakSelf.isSoundCheckStart = NO;
- weakSelf.isDelayCheckStart = NO;
- }
- }
- else if (weakSelf.evaluatParm) {
- NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:weakSelf.evaluatParm];
- NSDictionary *valueDic = [weakSelf.evaluatParm ks_dictionaryValueForKey:@"content"];
- [valueDic setValue:@"服务异常,请重试" forKey:@"reson"];
- [sendParm setValue:valueDic forKey:@"content"];
- [weakSelf postMessage:sendParm];
- weakSelf.hasSendStartMessage = YES;
- weakSelf.isCompareStart = NO;
- weakSelf.isSoundCheckStart = NO;
- weakSelf.isDelayCheckStart = NO;
- }
- else { // 其他断开
- if (weakSelf.AQManager.isRunning) {
- NSDictionary *postParm = @{@"api" : @"cancelEvaluating",
- @"content" : @{@"reson":@"服务异常,请重试"}
- };
- [weakSelf postMessage:postParm];
- [weakSelf stopRecordService];
- weakSelf.isCompareStart = NO;
- weakSelf.isSoundCheckStart = NO;
- weakSelf.isDelayCheckStart = NO;
- }
- }
- }
- else {
- // NSLog(@"-----连接成功");
- if (weakSelf.hasSendStartMessage == NO && weakSelf.evaluatParm) {
- NSDictionary *content = [weakSelf.evaluatParm ks_dictionaryValueForKey:@"content"];
- NSString *sendData = [weakSelf configDataCommond:@"musicXml" body:content type:@"SOUND_COMPARE"];
- [weakSelf sendDataToSocketService:sendData];
- [weakSelf postMessage:weakSelf.evaluatParm];
- weakSelf.hasSendStartMessage = YES;
- }
- }
- });
- };
- [self.socketManager SRWebSocketOpen];
- }
- - (void)appEnterBackground {
- NSDictionary *parm = @{@"api":@"suspendPlay"};
- [self postMessage:parm];
- [self handerAudioInterruption];
- }
- - (void)appEnterForeground {
- if (self.isCameraOpen) {
- if ([self.videoRecordManager getSessionStatusisActive] == NO) {
- [self.videoRecordManager configSessiondisplayInView:self.viewContainer];
- }
- }
- }
- - (void)initWebView {
- [self.scrollView removeFromSuperview];
-
- [self.view addSubview:self.viewContainer];
- [self.viewContainer mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.top.bottom.mas_equalTo(self.view);
- }];
-
- if (self.myWebView == nil) {
-
- WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
- config.selectionGranularity = WKSelectionGranularityDynamic;
- config.allowsInlineMediaPlayback = YES;
- config.mediaTypesRequiringUserActionForPlayback = NO;
-
- config.processPool = [KSBaseWKWebViewController singleWkProcessPool];
- config.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
- [self configUserAgent:config];
-
- //自定义的WKScriptMessageHandler 是为了解决内存不释放的问题
- WeakWebViewScriptMessageDelegate *weakScriptMessageDelegate = [[WeakWebViewScriptMessageDelegate alloc] initWithDelegate:self];
- //这个类主要用来做native与JavaScript的交互管理
- WKUserContentController * wkUController = [[WKUserContentController alloc] init];
- [wkUController addScriptMessageHandler:weakScriptMessageDelegate name:SCRIPT_NAME];
- config.userContentController = wkUController;
-
- WKPreferences *preferences = [WKPreferences new];
- // 是否支出javaScript
- preferences.javaScriptEnabled = YES;
- //不通过用户交互,是否可以打开窗口
- preferences.javaScriptCanOpenWindowsAutomatically = YES;
- config.preferences = preferences;
- self.myWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
- self.myWebView.opaque = NO;
- self.myWebView.UIDelegate = self;
- self.myWebView.navigationDelegate = self;
- self.myWebView.scrollView.bounces = NO;
- self.myWebView.backgroundColor = [UIColor clearColor];
- self.myWebView.scrollView.backgroundColor = [UIColor clearColor];
- #ifdef DEBUG
- if (@available(iOS 16.4, *)) {
- self.myWebView.inspectable = YES;
- }
- #endif
- // 加载进度条和title
- [self.myWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
- [self.myWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
- [self.view addSubview:self.myWebView];
- [self.myWebView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.mas_equalTo(self.view);
- make.top.mas_equalTo(self.view.mas_top);
- make.bottom.mas_equalTo(self.view.mas_bottom);
- }];
- self.myWebView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
-
- [self setupProgress];
-
- [self loadRequest];
- }
- else {
- [self.myWebView reload];
- }
- }
- - (void)configUserAgent:(WKWebViewConfiguration *)config {
- NSString *oldUserAgent = config.applicationNameForUserAgent;
- NSString *newAgent = [NSString stringWithFormat:@"%@ %@ %@",oldUserAgent,AGENT_NAME,AGENT_DOMAIN];
- config.applicationNameForUserAgent = newAgent;
- }
- - (void)configRecordManager {
- if (self.AQManager.isRunning) {
- [self.AQManager stopRecord];
- }
- self.AQManager = [[KSAQRecordManager alloc] init];
- self.AQManager.delegate = self;
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self connectSocketService];
- if (self.isCameraOpen) {
- [self.videoRecordManager configSessiondisplayInView:self.viewContainer];
- }
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
-
- if (_socketManager) {
- [self.socketManager SRWebSocketClose];
- _socketManager = nil;
- }
- // 停止播放和录制
- [self stopSession];
- [self stopPlayAction];
- [self stopRecordService];
- [self stopTuner];
-
- BOOL isBack = [self isViewPopDismiss];
- if (isBack) { // 页面销毁才删除
- if (_AQManager) {
- [_AQManager freeAudioQueue];
- }
- // 如果退出评测页面 清除 playerEngine
- if (self.playerEngine) {
- [self.playerEngine cleanup];
- self.playerEngine = nil;
- }
- // 返回不保存视频
- [self ignorRecordVideo];
- [self freeMp3Player];
- [self removeTuner];
- }
- }
- - (void)removeTuner {
- if (_tuner) {
- [_tuner freeTuner];
- _tuner = nil;
- NSLog(@"--- free tuner ");
- }
- }
- - (void)sendDataToSocketService:(id)data {
- if (_socketManager) {
- [self.socketManager sendData:data];
- }
- }
- #pragma mark --- WKScriptMessageHandler
- - (void)userContentController:(WKUserContentController *)userContentController
- didReceiveScriptMessage:(WKScriptMessage *)message {
- if ([message.name isEqualToString:SCRIPT_NAME]) {
- NSDictionary *parm = [self convertJsonStringToNSDictionary:message.body];
- // 回到主线程
- dispatch_async(dispatch_get_main_queue(), ^{
- if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"createMusicPlayer"]) {
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- NSString *musicUrl = [content ks_stringValueForKey:@"musicSrc"];
- NSString *checkUrl = [content ks_stringValueForKey:@"tuneSrc"];
- self.playerParm = parm;
- [self initMp3Player:musicUrl checkUrl:checkUrl];
- // 下载文件
- [self downloadMp3File:musicUrl];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"startEvaluating"]) { // 开始评测
- [self configRecordManager];
- self.hasSendStartMessage = NO;
-
- [RecordCheckManager checkMicPermissionAvaiableCallback:^(PREMISSIONTYPE type) {
- if (type == PREMISSIONTYPE_YES) {
- self.evaluatParm = [NSMutableDictionary dictionaryWithDictionary:parm];
- // 如果socket 连上了
- if (self.socketManager.socketReadyState == SR_OPEN) {
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- NSString *sendData = [self configDataCommond:@"musicXml" body:content type:@"SOUND_COMPARE"];
- [self sendDataToSocketService:sendData];
- [self postMessage:parm];
- self.hasSendStartMessage = YES;
- }
- else {
- [self connectSocketService];
- }
- }
- else {
- [self responseMessage:@"storageUnable" desc:@"没有麦克风访问权限" parm:parm];
- [self showAlertWithMessage:@"请开启麦克风访问权限" type:CHECKDEVICETYPE_MIC];
- }
- }];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"endEvaluating"]) {// 停止评测
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- self.musicStartTime = 0;
- self.evaluatParm = nil;
- [self stopRecordService];
- [self postMessage:parm];
- [self sendEndMessage];
- [self stopMp3Player]; // 停止播放
- });
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cancelEvaluating"]) { // 取消评测
- self.musicStartTime = 0;
- self.evaluatParm = nil;
- [self stopRecordService];
- [self postMessage:parm];
- [self stopMp3Player]; // 停止播放
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"startRecording"]) { // 开始录制
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- self.musicStartTime = [content ks_integerValueForKey:@"firstNoteTime"];
- if ([[content allKeys] containsObject:@"accompanimentState"]) {
- BOOL mute = [content ks_boolValueForKey:@"accompanimentState"] == NO;
- self.muteAccompany = mute;
- }
- else {
- self.muteAccompany = NO;
- }
- if ([[content allKeys] containsObject:@"speedRate"]) { // 播放速度
- self.musicSpeed = [content ks_floatValueForKey:@"speedRate"];
- }
- else {
- self.musicSpeed = 1.0f;
- }
- if (self->_videoRecordManager) {
- [self.videoRecordManager clearVideoFile];
- }
- [self postMessage:parm];
- self.isCompareStart = YES;
- [self startRecordService];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"endRecording"]) { // 停止录音
- self.recordParm = nil;
- [self stopRecordService];
- [self postMessage:parm];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"pauseRecording"]) {
- [self puaseRecordService];
- [self postMessage:parm];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"resumeRecording"]) {
- [self resumeRecordService];
- [self postMessage:parm];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"isWiredHeadsetOn"]) {
- [self configAudioDeviceType:parm];
- }
- // 发送消息给socket service
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"proxyMessage"]) {
- [self sendMessageToSocket:parm];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"proxyServiceMessage"]) {
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- NSString *sendData = [content mj_JSONString];
- NSLog(@"proxyServiceMessage ------- %@",sendData);
- [self sendDataToSocketService:sendData];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"openCamera"]) { // 开启摄像头
-
- [RecordCheckManager checkCameraPremissionAvaiableCallback:^(PREMISSIONTYPE type) {
- [self afterCheckCameraCheckAlbum:type parm:parm];
- }];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"closeCamera"]) { // 关闭摄像头
- self.isCameraOpen = NO;
- if (self->_videoRecordManager) {
- [self.videoRecordManager removeDisplay];
- }
- [self postMessage:parm];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"startCapture"]) { // 开始录制
- [RecordCheckManager checkPhotoLibraryPremissionAvaiableCallback:^(PREMISSIONTYPE type) {
- if (type == PREMISSIONTYPE_YES) {
- [self.videoRecordManager setIgnoreAudio:YES];
- self.videoRecordManager.audioUrl = self.AQManager.audioUrl;
- [self.videoRecordManager startRecord];
- [self postMessage:parm];
- }
- else {
- // [self responseMessage:@"storageUnable" desc:@"没有相册存储权限" parm:parm];
- // [self showAlertWithMessage:@"开启相册存储" type:CHECKDEVICETYPE_CAMREA];
- }
- }];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"endCapture"]) { // 结束录制
- if (self->_videoRecordManager) {
- self.endRecordParm = parm;
- [self.videoRecordManager stopRecord];
- }
- else {
- [self postMessage:parm];
- }
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"setCaptureMode"]) {
- NSString *modeString = [[parm ks_dictionaryValueForKey:@"content"] ks_stringValueForKey:@"mode"];
- BOOL isIgnoreAudio = [modeString isEqualToString:@"evaluating"];
- [self postMessage:parm];
- [self.videoRecordManager setIgnoreAudio:isIgnoreAudio];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"startSoundCheck"]) { // 开始校音
- [self configRecordManager];
- [self postMessage:parm];
- self.isCompareStart = NO;
- self.isSoundCheckStart = YES;
- self.isDelayCheckStart = NO;
- [self startRecordService];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"endSoundCheck"]) {
- // 结束校音
- self.isSoundCheckStart = NO;
- [self stopRecordService];
- [self postMessage:parm];
- }
- // 音视频合成
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"openAdjustRecording"]) {
- KSAccompanyDraftViewController *ctrl = [[KSAccompanyDraftViewController alloc] init];
- ctrl.ks_landScape = YES;
- if (self.bgAudioUrl == nil) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"当前曲目无mp3伴奏"];
- }
- else {
- if (self.AQManager && self.AQManager.audioUrl) {
-
- self.recordUrl = self.AQManager.audioUrl;
-
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- ctrl.recordId = [content ks_stringValueForKey:@"recordId"];
- ctrl.songName = [content ks_stringValueForKey:@"title"];
- ctrl.coverImage = [content ks_stringValueForKey:@"coverImg"];
- if ([[content allKeys] containsObject:@"speedRate"]) {
- ctrl.musicSpeed = [content ks_floatValueForKey:@"speedRate"];
- }
- else {
- ctrl.musicSpeed = 1.0f;
- }
- MJWeakSelf;
- NSInteger micDelay = [UserDefaultObjectForKey(@"micDelay") integerValue];
- NSInteger defaultDelay = self.offsetTime + micDelay;
- [ctrl configWithVideoUrl:self.videoRecordManager.videoFileURL bgAudioUrl:self.bgAudioUrl remoteBgUrl:self.accompanyUrl recordUrl:self.recordUrl offsetTime:defaultDelay mergeCallback:^(BOOL isPublished) {
- [weakSelf appEnterForeground];
- if (isPublished) {
- [weakSelf musicPublishCallBack:content];
- }
- }];
- [self.navigationController pushViewController:ctrl animated:NO];
- }
- else {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"麦克风被占用"];
- }
- }
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"videoUpdate"]) { // 上传
- NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:parm];
- NSMutableDictionary *contentParm = [NSMutableDictionary dictionaryWithDictionary:[sendParm ks_dictionaryValueForKey:@"content"]];
- if (self.videoRecordManager) {
-
- MJWeakSelf;
- [self.videoRecordManager saveVideoCallback:^(BOOL isSuccess, NSString * _Nullable message) {
- if (isSuccess) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"已保存到相册"];
- [weakSelf uploadVideoWithParm:contentParm sendParm:sendParm];
- }
- }];
- }
- }
- #pragma mark -------- 云教练原生播放对接
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudDetail"]) { // 初始化方法
- /**
- api: 'cloudDetail',
- content: {
- midi: '',
- denominator: 4,
- numerator: 4,
- // xml整体原始速度
- originalSpeed: 90,
- // 间隔(ms)
- interval: 50
- }
- */
-
- [self configAudioSession];
- // 重置track num array
- self.configEngineParm = [parm copy];
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- NSString *midiUrl = [content ks_stringValueForKey:@"midi"];
- NSInteger denominator = [content ks_integerValueForKey:@"denominator"];
- NSInteger numerator = [content ks_integerValueForKey:@"numerator"];
- NSString *beatString = [NSString stringWithFormat:@"%zd/%zd", numerator,denominator];
- self.beatType = [self getBeatTypeFromString:beatString];
-
- float originalSpeed = [content ks_floatValueForKey:@"originalSpeed"];
- self.songOriginalSpeed = originalSpeed;
- self.currentSpeed = originalSpeed;
- NSInteger reportInterval = [content ks_integerValueForKey:@"interval"];
- // 下载midi文件
- BOOL hasSaveSong = [self checkSongHasSaveWithSongUrl:midiUrl];
- NSString *fileName = [midiUrl getUrlFileName];
- NSString *filePath = [self getFilePathWithName:fileName];
- if (hasSaveSong) {
- [self configPlayerEngineWithSong:filePath reportTime:reportInterval];
- }
- else {
- MJWeakSelf;
- [self downloadMidiFile:midiUrl success:^{
- [weakSelf configPlayerEngineWithSong:filePath reportTime:reportInterval];
- } faliure:^{
-
- }];
- }
-
- }
-
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudGetMediaStatus"]) { // 获取播放状态
- /**
- api: 'cloudGetMediaStatus',
- content: {
- status: 'init' | 'play' | 'suspend'
- }
- */
- NSString *engineStatus = @"init";
- if (self.initEngineSuccess) {
- if (self.isPlaying) {
- engineStatus = @"play";
- }
- else {
- engineStatus = @"suspend";
- }
- }
- NSMutableDictionary *valueDic = [NSMutableDictionary dictionaryWithDictionary:[parm ks_dictionaryValueForKey:@"content"]];
- [valueDic setValue:engineStatus forKey:@"status"];
- NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:parm];
- [sendParm setValue:valueDic forKey:@"content"];
- [self postMessage:sendParm];
- }
- // 播放、暂停、进度、播放结束、跳转指定位置
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudPlay"]) { // 播放
- /**
- api: 'cloudPlay',
- content: {
- // 当前曲目id
- songID: 0,
- // xml整体原始速度
- originalSpeed: 90,
- // 当前选择速度
- speed: 90,
- // 开始时间(ms)
- startTime: 0
- // 播放频率
- hertz:440
- }
- */
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- self.currentSongId = [content ks_stringValueForKey:@"songID"];
- float speed = [content ks_floatValueForKey:@"speed"];
- float originalSpeed = [content ks_floatValueForKey:@"originalSpeed"];
- double rate = speed / originalSpeed;
- self.currentSpeed = speed;
- float hertz = [content ks_floatValueForKey:@"hertz"];
- // 播放的hertz
- [self.playerEngine adjustPitchByHZ:hertz];
-
- [self.playerEngine setMusicPlayerSpeed:rate];
- Float64 startTime = [content ks_doubleValueForKey:@"startTime"];
- NSLog(@"------%@", [content ks_stringValueForKey:@"startTime"]);
- [self.playerEngine setProgressTime:(startTime/1000)];
- // 播放
- [self playAction];
- [self postMessage:parm];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudSuspend"]) { // 暂停
- /**
- api: 'cloudSuspend',
- content: {
- // 当前曲目id
- songID: 0,
- }
- */
- [self stopPlayAction];
- [self postMessage:parm];
-
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudSetCurrentTime"]) { // // 跳转指定位置
- /**
- api: 'cloudSetCurrentTime',
- content: {
- // 当前曲目id
- songID: 0,
- // 指定位置时间(ms)
- currentTime: 0
- }
- */
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- Float64 currentTime = [content ks_doubleValueForKey:@"currentTime"];
- if (self.playerEngine) {
- [self.playerEngine setProgressTime:(currentTime/1000)];
- }
- [self postMessage:parm];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudChangeSpeed"]) { // 调速 范围(45-270整数)
- /**
- api: 'cloudChangeSpeed',
- content: {
- // 当前曲目id
- songID: 0,
- // 调整速度
- speed: 90
- // xml整体原始速度
- originalSpeed: 90,
- }
- */
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- float speed = [content ks_floatValueForKey:@"speed"];
- float originalSpeed = [content ks_floatValueForKey:@"originalSpeed"];
- double rate = speed / originalSpeed;
- self.currentSpeed = speed;
- [self.playerEngine setMusicPlayerSpeed:rate];
- // 回报信息
- [self postMessage:parm];
- }
- // 设置每个轨道音量
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudVolume"]) {
- /**
- api: 'cloudVolume',
- content: {
- parts: [
- {
- name: '',
- volume: 90,//0-100
- }
- ]
- }
- */
- NSLog(@"-cloudVolume -----%@",parm);
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- int instrumentId = [content ks_intValueForKey:@"activeMidiId"];
- float volume = [content ks_floatValueForKey:@"activeMidiVolume"];
- if (instrumentId < 0) {
- [self postMessage:parm];
- [self.playerEngine getAllTrackVolume];
- return;
- }
- [self.playerEngine volumeTrackVolumeWithInstrumentId:instrumentId volume:volume/100];
- [self postMessage:parm];
- [self.playerEngine getAllTrackVolume];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudMetronome"]) { // 节拍器播放
- self.metronomeParm = [NSMutableDictionary dictionaryWithDictionary:parm];
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- NSInteger repeatCount = [content ks_integerValueForKey:@"repeat"];
- NSInteger denominator = [content ks_integerValueForKey:@"denominator"];
- NSInteger numerator = [content ks_integerValueForKey:@"numerator"];
- NSInteger supplement = [content ks_integerValueForKey:@"supplement"];
- NSString *beatString = [NSString stringWithFormat:@"%zd/%zd", numerator,denominator];
- self.beatType = [self getBeatTypeFromString:beatString];
- [self showBeatViewRepeatCount:repeatCount supplement:supplement];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudDestroy"]) { // 销毁播放器
- self.initEngineSuccess = NO;
- // 重置track num array
- self.isPlaying = NO;
- if (self.playerEngine) {
- [self.playerEngine cleanup];
- self.playerEngine = nil;
- }
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudLoading"]) { // loading
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- BOOL showLoading = [content ks_boolValueForKey:@"show"];
- if (showLoading) {
- [self showCustomLoading];
- }
- else {
- [self removeCustomLoadingView];
- }
- }
- // 跟音
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudToggleFollow"]) { // 跟音
- [RecordCheckManager checkMicPermissionAvaiableCallback:^(PREMISSIONTYPE type) {
- if (type == PREMISSIONTYPE_YES) {
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- NSString *status = [content ks_stringValueForKey:@"state"];
- if ([status isEqualToString:@"start"]) { // 开始
- [self configAudioSession];
- [self startTuner];
- }
- else if ([status isEqualToString:@"end"]) { // 结束
- [self stopTuner];
- }
- [self postMessage:parm];
- }
- else {
- [self responseMessage:@"storageUnable" desc:@"没有麦克风权限" parm:parm];
- [self showAlertWithMessage:@"请开启麦克风访问权限" type:CHECKDEVICETYPE_MIC];
- }
- }];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudAccompanyMessage"]) { // 获取伴奏 废弃⚠️
-
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"startTune"]) { // 延迟测试开始
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
- NSInteger count = [content ks_integerValueForKey:@"count"];
- if (count == 0) {
- [self.delayArray removeAllObjects];
- self.checkIndex = 0;
- }
- self.checkIndex += 1;
- [self configRecordManager];
- [self postMessage:parm];
- self.isCompareStart = NO;
- self.isSoundCheckStart = NO;
- self.isDelayCheckStart = YES;
- [self startRecordService];
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"endTune"]) { // 延迟测试停止
-
- self.isDelayCheckStart = NO;
- [self stopRecordService];
- [self postMessage:parm];
- [self stopMp3Player];
- [self sendAdjustEndMessage]; // 发送结束消息
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"finishTune"]) { // 延迟测试结束
- NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
-
- NSInteger errorCount = 0;
- NSInteger totalDelay = 0;
- for (NSInteger index = 0 ; index < self.delayArray.count; index++) {
- NSInteger micDelay = [self.delayArray[index] integerValue];
- if (micDelay > 10 && micDelay < 300) {
- totalDelay += micDelay;
- }
- else {
- errorCount++;
- }
- }
- NSMutableDictionary *contentParm = [NSMutableDictionary dictionaryWithDictionary:content];
- if (errorCount > 1) { // 错误次数过多
- [contentParm setValue:@(NO) forKey:@"result"];
- }
- else {
- [contentParm setValue:@(YES) forKey:@"result"];
- NSInteger averageDelay = totalDelay / (self.delayArray.count - errorCount);
- UserDefaultSet([NSNumber numberWithDouble:averageDelay], @"micDelay");
- }
- NSMutableDictionary *sendParm = [NSMutableDictionary dictionary];
- [sendParm setValue:@"finishTune" forKey:@"api"];
- [sendParm setValue:contentParm forKey:@"content"];
- [self postMessage:sendParm];
- [self.delayArray removeAllObjects];
- self.checkIndex = 0;
- }
- else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"getDeviceDelay"]) {
- NSInteger micDelay = [UserDefaultObjectForKey(@"micDelay") integerValue];
- NSMutableDictionary *content = [NSMutableDictionary dictionaryWithDictionary:[parm ks_dictionaryValueForKey:@"content"]];
- [content setValue:[NSNumber numberWithInteger:micDelay] forKey:@"value"];
- NSMutableDictionary *sendParm = [NSMutableDictionary dictionary];
- [sendParm setValue:@"getDeviceDelay" forKey:@"api"];
- [sendParm setValue:content forKey:@"content"];
- [self postMessage:sendParm];
- }
- else {
- [super handleScriptMessageSource:parm];
- }
-
- });
- }
- }
- - (void)afterCheckCameraCheckAlbum:(PREMISSIONTYPE)cameraType parm:(NSDictionary *)sourceParm {
-
- [RecordCheckManager checkPhotoLibraryPremissionAvaiableCallback:^(PREMISSIONTYPE type) {
- if (type == PREMISSIONTYPE_YES && cameraType == PREMISSIONTYPE_YES) {
- self.isCameraOpen = YES;
- [self.videoRecordManager setIgnoreAudio:YES];
- [self.videoRecordManager configSessiondisplayInView:self.viewContainer];
- [self postMessage:sourceParm];
- }
- else { //
-
- NSString *content = @"";
- NSString *des = @"";;
- if (cameraType == PREMISSIONTYPE_NO && type == PREMISSIONTYPE_NO) {
- des = @"没有相机和相册访问权限";
- content = @"请开启相机和相册访问权限";
- }
- else if (cameraType == PREMISSIONTYPE_NO && type == PREMISSIONTYPE_YES) {
- des = @"没有相机访问权限";
- content = @"请开启相机访问权限";
- }
- else if (cameraType == PREMISSIONTYPE_YES && type == PREMISSIONTYPE_NO) {
- des = @"没有相册访问权限";
- content = @"请开启相册访问权限";
- }
- [self responseMessage:@"storageUnable" desc:des parm:sourceParm];
- [self showAlertWithMessage:content type:CHECKDEVICETYPE_CAMREA];
- }
- }];
- }
- - (void)responseMessage:(NSString *)reson desc:(NSString *)desc parm:(NSDictionary *)parm {
- NSMutableDictionary *sendContent = [NSMutableDictionary dictionaryWithDictionary:[parm ks_dictionaryValueForKey:@"content"]];
- [sendContent setValue:reson forKey:@"reson"];
- [sendContent setValue:desc forKey:@"des"];
- NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:parm];
- [sendParm setValue:sendContent forKey:@"content"];
- [self postMessage:sendParm];
- }
- - (void)musicPublishCallBack:(NSDictionary *)content {
- NSMutableDictionary *parm = [NSMutableDictionary dictionary];
- [parm setValue:@"hideComplexButton" forKey:@"api"];
- [parm setValue:@{} forKey:@"content"];
- [self postMessage:parm];
- }
- - (void)uploadVideoWithParm:(NSMutableDictionary *)contentParm sendParm:(NSMutableDictionary *)sendParm {
- MJWeakSelf;
- [self.videoRecordManager exportRecordVideoUploadSuccess:^(NSString * _Nonnull videoFileUrl) {
-
- } failure:^(NSString * _Nonnull desc) {
-
- }];
- }
- - (void)showAlertWithMessage:(NSString *)message type:(CHECKDEVICETYPE)deviceType {
- [KSPremissionAlert shareInstanceDisplayImage:deviceType message:message showInView:self.view cancel:^{
-
- } confirm:^{
- [self openSettingView];
- }];
-
- }
- - (void)openSettingView {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
- }
- - (void)downloadUrl:(NSString *)url success:(void(^)(void))success faliure:(void(^)(void))faliure {
-
- [KSNetworkingManager downloadFileRequestWithFileUrl:url progress:^(int64_t bytesRead, int64_t totalBytes) {
-
- } success:^(NSURL * _Nonnull fileUrl) {
-
- if ([self saveAccompanyFileWithUrl:fileUrl accompanyUrl:url]) {
- if (success) {
- success();
- }
- }
- } faliure:^(NSError * _Nonnull error) {
- if (faliure) {
- faliure();
- }
- }];
- }
- - (void)configVideoRecord:(NSURL *)path {
- self.videoRecordManager.bgAudioUrl = path;
- self.bgAudioUrl = path;
- }
- - (void)showBeatViewRepeatCount:(NSInteger)repeatCount supplement:(NSInteger)supplement {
- KSCloudBeatView *beatView = [KSCloudBeatView shareInstanceWithBeatType:self.beatType speed:self.currentSpeed repeatCount:repeatCount supplement:supplement];
- MJWeakSelf;
- [beatView startPlayWithEndCallback:^(BOOL isCancle) {
- if (isCancle) { // 取消
- [weakSelf sendEndMetronomeMessage:YES];
- }
- else { // 播放完成
- [weakSelf sendEndMetronomeMessage:NO];
- }
- }];
- [self.view addSubview:beatView];
- }
- - (void)sendEndMetronomeMessage:(BOOL)isCancel {
- NSString *status = isCancel ? @"cancel" : @"finish";
- NSMutableDictionary *valueDic = [NSMutableDictionary dictionaryWithDictionary:[self.metronomeParm ks_dictionaryValueForKey:@"content"]];
- [valueDic setValue:status forKey:@"status"];
- NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:self.metronomeParm];
- [sendParm setValue:valueDic forKey:@"content"];
- [self postMessage:sendParm];
- }
- - (void)downloadMidiFile:(NSString *)midiUrl success:(void(^)(void))success faliure:(void(^)(void))faliure {
-
- [KSNetworkingManager downloadFileRequestWithFileUrl:midiUrl progress:^(int64_t bytesRead, int64_t totalBytes) {
-
- } success:^(NSURL * _Nonnull fileUrl) {
- if ([self saveMidiFileWithUrl:fileUrl midiUrl:midiUrl]) {
- if (success) {
- success();
- }
- }
- } faliure:^(NSError * _Nonnull error) {
- if (faliure) {
- faliure();
- }
- }];
- }
- - (BOOL)saveMidiFileWithUrl:(NSURL *)fileUrl midiUrl:(NSString *)midiUrl {
- NSData *sourceData = [NSData dataWithContentsOfURL:fileUrl];
- NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
- NSString *filePath= [cachePath stringByAppendingPathComponent:@"MidiSong"];
- // 先创建子目录
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if (![fileManager fileExistsAtPath:filePath]) {
- [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
- }else{
- NSLog(@"已创建文件夹");
- }
- NSString *fileName = [midiUrl getUrlFileName];
- NSString *tempPath = [filePath stringByAppendingPathComponent:fileName];
- BOOL success = [sourceData writeToFile:tempPath atomically:NO];
- return success;
- }
- - (NSString *)getFilePathWithName:(NSString *)fileName {
- NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
- NSString *filePath= [cachePath stringByAppendingPathComponent:@"MidiSong"];
- return [filePath stringByAppendingPathComponent:fileName];;
- }
- - (BOOL)checkSongHasSaveWithSongUrl:(NSString *)songUrl {
- BOOL hasSaveFile = NO;
- NSString *fileName = [songUrl getUrlFileName];
- NSString *filePath = [self getFilePathWithName:fileName];
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:filePath]) {
- hasSaveFile = YES;
- }
- return hasSaveFile;
- }
- - (void)sendMessageToSocket:(NSDictionary *)parm {
- NSString *messageHeader = @"proxyMessage";
- NSString *sendMessage = [self configDataCommond:messageHeader body:[parm ks_dictionaryValueForKey:@"content"]];
- [self sendDataToSocketService:sendMessage];
- }
- - (void)configAudioDeviceType:(NSDictionary *)parm {
- AUDIODEVICE_TYPE type = [KSAQRecordManager queryAudioOutputDeviceType];
- NSString *valueStr = @"";
- BOOL checkIsWired = NO;
- switch (type) {
- case AUDIODEVICE_TYPE_HEADPHONE:
- {
- valueStr = @"有线耳机";
- checkIsWired = YES;
- }
- break;
- case AUDIODEVICE_TYPE_BLUETOOTH:
- {
- valueStr = @"蓝牙耳机";
- checkIsWired = YES;
- }
- break;
- case AUDIODEVICE_TYPE_NONE:
- {
- valueStr = @"";
- checkIsWired = NO;
- }
- break;
- default:
- break;
- }
- NSMutableDictionary *valueDic = [NSMutableDictionary dictionaryWithDictionary:[parm ks_dictionaryValueForKey:@"content"]];
- [valueDic setValue:valueStr forKey:@"type"];
- [valueDic setValue:[NSNumber numberWithBool:checkIsWired] forKey:@"checkIsWired"];
- NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:parm];
- [sendParm setValue:valueDic forKey:@"content"];
- [self postMessage:sendParm];
- }
- - (void)startRecordService {
- if (self.AQManager.isRunning) {
- [self.AQManager stopRecord];
- }
- [self.AQManager startRecord];
- }
- - (void)stopRecordService {
- if (self.AQManager.isRunning) {
- [self.AQManager stopRecord];
- }
- }
- - (void)puaseRecordService {
- if (self.AQManager.isRunning) {
- [self.AQManager pauserRecord];
- }
- }
- - (void)resumeRecordService {
- [self.AQManager resumeRecord];
- }
- - (void)sendEndMessage {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- // 上传停止的信息 发送给服务端
- NSString *endMessage = @"recordEnd";
- NSString *endData = [self configDataCommond:endMessage body:nil type:@"SOUND_COMPARE"];
- [self sendDataToSocketService:endData];
- self.isCompareStart = NO;
- NSLog(@"---- send end message");
- });
- }
- - (void)sendAdjustEndMessage {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- // 上传停止的信息 发送给服务端
- NSString *endMessage = @"recordEnd";
- NSString *endData = [self configDataCommond:endMessage body:nil type:@"DELAY_CHECK"];
- [self sendDataToSocketService:endData];
- self.isDelayCheckStart = NO;
- NSLog(@"---- send adjust end message");
- });
- }
- - (void)sendOffsetTimeToService {
- // 上传停止的信息 发送给服务端
- NSString *offsetMessage = @"audioPlayStart";
- NSTimeInterval micDelay = [UserDefault(@"micDelay") doubleValue];
- NSDictionary *dic = @{@"offsetTime" : [NSNumber numberWithInteger:self.offsetTime], @"micDelay": [NSNumber numberWithInteger:micDelay]};
- NSString *endData = [self configDataCommond:offsetMessage body:dic type:@"SOUND_COMPARE"];
- NSLog(@"------ %@", endData);
- [self sendDataToSocketService:endData];
- self.isCompareStart = NO;
- }
- #pragma mark-------- KSAQRecordManagerDelegate
- - (void)recordInterruption {
- NSDictionary *postParm = @{@"api" : @"cancelEvaluating",
- @"content" : @{@"reson":@"录制错误,请重试"}
- };
- [self postMessage:postParm];
- }
- - (void)audioRouteChange:(AUDIODEVICE_TYPE)type {
- NSString *valueStr = @"";
- BOOL checkIsWired = NO;
- switch (type) {
- case AUDIODEVICE_TYPE_HEADPHONE:
- {
- valueStr = @"有线耳机";
- checkIsWired = YES;
- }
- break;
- case AUDIODEVICE_TYPE_BLUETOOTH:
- {
- valueStr = @"蓝牙耳机";
- checkIsWired = YES;
- }
- break;
- case AUDIODEVICE_TYPE_NONE:
- {
- valueStr = @"";
- checkIsWired = NO;
- }
- break;
- default:
- break;
- }
-
- NSDictionary *postParm = @{@"api" : @"listenerWiredStatus",
- @"content" : @{@"type":valueStr,
- @"checkIsWired":[NSNumber numberWithBool:checkIsWired]
- }
- };
- [self postMessage:postParm];
- }
- #pragma mark ------- 评测app播放
- - (void)recordDidStart:(NSTimeInterval)time { // ms
- self.hasRecordMusicOffset = NO;
- self.recordStartTime = time;
- if (self.isDelayCheckStart) {
- NSLog(@"---- delay - record did start %f", time);
- // 播放音频
- // 播放校音音频
- dispatch_main_sync_safe(^{
- [self.delayCheckPlayer seekToTimePlay:0];
- });
- }
- else if (self.isCompareStart) {
- NSLog(@"---- compare - record did start %f", time);
- if (self.playerEngine == nil && _musicPlayer) {
- dispatch_main_sync_safe(^{
- self.musicPlayer.isMute = self.muteAccompany;
- self.musicPlayer.rate = self.musicSpeed;
- // 进度跳转
- [self.musicPlayer seekToTimePlay:self.musicStartTime];
- });
- }
- }
- }
- - (void)audioRecord:(KSAQRecordManager *)audioRecord didRecordAudioData:(void *)data length:(UInt32)length {
-
- if (self.socketManager.socketReadyState != SR_OPEN) {
- return;
- }
- NSData *pushData = [[NSData alloc] initWithBytes:data length:length];
- if (self.isCompareStart) { // 发送评测开始消息
- dispatch_async(dispatch_get_main_queue(), ^{
- NSDate *date = [NSDate date];
- NSTimeInterval inteveral = [date timeIntervalSince1970];
- double beginTime = inteveral - audioRecord.sampleTime;
- NSDictionary *parm = @{
- @"api" : @"recordStartTime",
- @"content" : @{@"inteveral" : [NSNumber numberWithDouble:beginTime]}
- };
- [self postMessage:parm];
- });
-
- NSLog(@"--------- send start message");
- _isCompareStart = NO;
- NSString *startMessage = @"recordStart";
- NSString *startString = [self configDataCommond:startMessage body:nil type:@"SOUND_COMPARE"];
- [self sendDataToSocketService:startString];
- }
- else if (self.isSoundCheckStart) { // 校音开始
-
- NSLog(@"--------- send check start message");
- _isSoundCheckStart = NO;
- NSString *checkStartMessage = @"start";
- NSString *startString = [self configDataCommond:checkStartMessage body:nil type:@"PITCH_DETECTION"];
- [self sendDataToSocketService:startString];
- }
- else if (self.isDelayCheckStart) {
- NSLog(@"--------- send delay check start message");
- _isDelayCheckStart = NO;
- NSString *checkStartMessage = @"recordStart";
- NSInteger frequence = self.checkPrequence;
- NSDictionary *parm = @{@"HZ" : @(frequence)};
- NSString *startString = [self configDataCommond:checkStartMessage body:parm type:@"DELAY_CHECK"];
- [self sendDataToSocketService:startString];
- }
- // NSLog(@"--------- send audio data length %d", length);
- [self sendDataToSocketService:pushData];
- }
- - (NSString *)configDataCommond:(NSString *)commond body:(id)bodyMessage type:(NSString *)dataType {
- NSMutableDictionary *parm = [NSMutableDictionary dictionary];
- if (bodyMessage) {
- [parm setValue:bodyMessage forKey:@"body"];
- }
- NSMutableDictionary *headerParm = [NSMutableDictionary dictionary];
- if ([NSString isEmptyString:commond]) {
- [headerParm setValue:@"" forKey:@"commond"];
- }
- else {
- [headerParm setValue:commond forKey:@"commond"];
- }
- if (![NSString isEmptyString:dataType]) {
- [headerParm setValue:dataType forKey:@"type"];
- }
- [headerParm setValue:@(200) forKey:@"status"];
- [parm setValue:headerParm forKey:@"header"];
- return [parm mj_JSONString];
- }
- - (NSString *)configDataCommond:(NSString *)commond body:(id)bodyMessage {
- NSMutableDictionary *parm = [NSMutableDictionary dictionary];
- if (bodyMessage) {
- [parm setValue:bodyMessage forKey:@"body"];
- }
- if ([NSString isEmptyString:commond]) {
- [parm setValue:@{@"commond":@""} forKey:@"header"];
- }
- else {
- [parm setValue:@{@"commond":commond} forKey:@"header"];
- }
- return [parm mj_JSONString];
- }
- - (KSTargetWebSocketManager *)socketManager {
- if (!_socketManager) {
- _socketManager = [[KSTargetWebSocketManager alloc] init];
- }
- return _socketManager;
- }
- #pragma mark --- lazying
- - (UIView *)viewContainer {
- if (!_viewContainer) {
- _viewContainer = [[UIView alloc] init];
- }
- return _viewContainer;
- }
- - (KSVideoRecordManager *)videoRecordManager {
- if (!_videoRecordManager) {
- MJWeakSelf;
- _videoRecordManager = [[KSVideoRecordManager alloc] initSessionRecordCallback:^(BOOL isSuccess, NSString * _Nullable message) {
- if (isSuccess) {
- [weakSelf showSuccessMessage:message];
- }
- else {
- if (![NSString isEmptyString:message]) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:message];
- }
-
- }
- }];
- [_videoRecordManager errorMessageCallback:^(NSDictionary * _Nonnull errorParm) {
- [weakSelf uploadVideoRecordErrorMessage:errorParm];
- }];
- }
- return _videoRecordManager;
- }
- - (void)uploadVideoRecordErrorMessage:(NSDictionary *)errorParm {
- NSMutableDictionary *parm = [NSMutableDictionary dictionaryWithDictionary:errorParm];
- [parm setValue:UserDefault(UIDKey) forKey:@"userId"];
- [parm setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forKey:@"version"];
- NSString *content = [parm mj_JSONString];
- NSMutableDictionary *submitParm = [KSLogManager generateLogMessageWithContent:content type:@"ERROR"];
- NSMutableArray *uploadArray = [NSMutableArray arrayWithObject:submitParm];
- [KSNetworkingManager sysExceptionLogUpdate:KS_POST token:UserDefault(TokenKey) logArray:uploadArray success:^(NSDictionary * _Nonnull dic) {
- if ([dic ks_integerValueForKey:@"code"] == 200) {
-
- }
- } faliure:^(NSError * _Nonnull error) {
-
- }];
- }
- - (void)showSuccessMessage:(NSString *)message {
- if (![NSString isEmptyString:message]) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:message];
- }
- // 成功
- if (self.endRecordParm) {
- [self postMessage:self.endRecordParm];
- self.endRecordParm = nil;
- }
- }
- - (MetronomeType)getBeatTypeFromString:(NSString *)typeString {
- if ([typeString isEqualToString:@"1/4"]) {
- return MetronomeType1V4;
- }
- else if ([typeString isEqualToString:@"2/4"]) {
- return MetronomeType2V4;
- }
- else if ([typeString isEqualToString:@"3/4"]) {
- return MetronomeType3V4;
- }
- else if ([typeString isEqualToString:@"4/4"]) {
- return MetronomeType4V4;
- }
- else if ([typeString isEqualToString:@"3/8"]) {
- return MetronomeType3V8;
- }
- else if ([typeString isEqualToString:@"6/8"]) {
- return MetronomeType6V8;
- }
- else {
- return MetronomeType4V4;
- }
- }
- #pragma mark ------- midi 播放相关
- - (void)configPlayerEngineWithSong:(NSString *)songPath reportTime:(NSInteger)reportTime {
- self.initEngineSuccess = NO;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- self.playerEngine = [[MidiPlayerEngine alloc] init];
- self.playerEngine.reportTime = reportTime;
- self.playerEngine.delegate = self;
- [self.playerEngine configSoundFilePath:[[NSBundle mainBundle]
- pathForResource:@"synthgms" ofType:@"sf2"]];
- [self.playerEngine loadMIDIFileWithString:songPath];
- });
- }
- - (void)configAudioSession {
- self.audioSessionManager = [[KSAudioSessionManager alloc] init];
- self.audioSessionManager.delegate = self;
- [self.audioSessionManager configAudioSession:AUDIOCONFIG_PLAYANDRECORD];
- }
- #pragma mark ---- PlayerEngineDelegate
- /** 进度更新
- api: 'cloudTimeUpdae',
- content: {
- // 当前曲目id
- songID: 0,
- // 当前位置时间(ms)
- currentTime: 0
- }
- */
- /** 播放结束事件
- api: 'cloudplayed',
- content: {
- // 当前曲目id
- songID: 0,
- }
- */
- - (void)ProgressUpdated:(float)progress currentPlayTime:(MusicTimeStamp)currentPlayTime currentTime:(NSTimeInterval)currentTime {
- if (self.isPlaying == NO) {
- return;
- }
- // 回调
- NSMutableDictionary *sendParm = [NSMutableDictionary dictionary];
- [sendParm setValue:@"cloudTimeUpdae" forKey:@"api"];
- NSMutableDictionary *content = [NSMutableDictionary dictionary];
- [content setValue:self.currentSongId forKey:@"songID"];
- [content setValue:@(currentPlayTime*1000) forKey:@"currentTime"];
- [sendParm setValue:content forKey:@"content"];
- [self postMessage:sendParm];
- }
- - (void)playEnd {
- [self stopPlayAction];
- NSMutableDictionary *sendParm = [NSMutableDictionary dictionary];
- [sendParm setValue:@"cloudplayed" forKey:@"api"];
- NSMutableDictionary *content = [NSMutableDictionary dictionary];
- [content setValue:self.currentSongId forKey:@"songID"];
- [sendParm setValue:content forKey:@"content"];
- [self postMessage:sendParm];
- }
- - (void)initPlayerEngineSuccess:(float)totalTime {
- self.initEngineSuccess = YES;
- if (self.configEngineParm) {
- NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:self.configEngineParm];
- NSMutableDictionary *content = [NSMutableDictionary dictionaryWithDictionary:[sendParm ks_dictionaryValueForKey:@"content"]];
- [content setValue:@(totalTime*1000) forKey:@"midiDuration"];
- [sendParm setValue:content forKey:@"content"];
- [self postMessage:sendParm];
- self.configEngineParm = nil;
- }
- }
- // 总时长
- - (void)GetMusicTotalTime:(float)time {
-
- }
- #pragma mark ----- 播放控制
- - (void)playAction {
- if (self.playerEngine) {
- self.isPlaying = YES;
- [self.playerEngine playMIDIFile];
- }
- }
- - (void)stopPlayAction {
- if (self.playerEngine) {
- self.isPlaying = NO;
- if ([self.playerEngine isPlayingFile]) {
- [self.playerEngine stopPlayingMIDIFile];
- }
- }
- }
- #pragma mark ----- 小酷AI loading
- - (AccompanyLoadingView *)loadingView {
- if (!_loadingView) {
- _loadingView = [AccompanyLoadingView shareInstance];
- MJWeakSelf;
- [_loadingView loadingCallback:^{
- [weakSelf backAction];
- }];
- }
- return _loadingView;
- }
- - (void)showCustomLoading {
- if ([self.view.subviews containsObject:self.loadingView]) {
- return;
- }
- [self.view addSubview:self.loadingView];
- [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.right.bottom.mas_equalTo(self.view);
- }];
- [self.view bringSubviewToFront:self.loadingView];
- [self.loadingView showLoading];
- }
- - (void)removeCustomLoadingView {
- [self.loadingView stopLoading];
- }
- #pragma mark ----- 跟音模块
- - (void)startTuner {
- @try {
- if (self.isTunerRuning == NO) {
- self.isTunerRuning = YES;
- [self.tuner start];
- }
- } @catch (NSException *exception) {
- NSLog(@"----- exception --- %@", exception);
- } @finally {
-
- }
- }
- - (void)stopTuner {
- if (self.isTunerRuning) {
- self.isTunerRuning = NO;
- [self.tuner stop];
- }
- }
- - (Tuner *)tuner {
- if (!_tuner) {
- _tuner = [[Tuner alloc] initWithThreshold:0 smoothing:0.25];
- _tuner.delegate = self;
- }
- return _tuner;
- }
- - (void)tunerDidUpdate:(Tuner *)tuner output:(TunerOutput *)output {
- if (output.amplitude < 0.01) {
-
- }
- else {
- // 回调频率
- NSDictionary *parm = @{
- @"api" : @"cloudFollowTime",
- @"content" : @{@"frequency" : [NSNumber numberWithDouble:output.frequency]}
- };
- [self postMessage:parm];
- }
- NSLog(@"-------- %@%zd --- distance :%f frequence : %f" , output.pitch, output.octave, output.distance, output.frequency);
-
- }
- #pragma mark ---- 保存伴奏
- - (BOOL)saveAccompanyFileWithUrl:(NSURL *)fileUrl accompanyUrl:(NSString *)accompanyUrl {
- NSData *sourceData = [NSData dataWithContentsOfURL:fileUrl];
- NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
- NSString *filePath= [cachePath stringByAppendingPathComponent:@"AccompanySong"];
- // 先创建子目录
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if (![fileManager fileExistsAtPath:filePath]) {
- [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
- }else{
- NSLog(@"已创建文件夹");
- }
- NSString *fileName = [accompanyUrl getUrlFileName];
- NSString *tempPath = [filePath stringByAppendingPathComponent:fileName];
- BOOL success = [sourceData writeToFile:tempPath atomically:NO];
- return success;
- }
- - (NSString *)getAccompanyFilePathWithName:(NSString *)fileName {
- NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
- NSString *filePath= [cachePath stringByAppendingPathComponent:@"AccompanySong"];
- return [filePath stringByAppendingPathComponent:fileName];;
- }
- - (BOOL)checkSongHasSaveAccompanyWithSongUrl:(NSString *)songUrl {
- BOOL hasSaveFile = NO;
- NSString *fileName = [songUrl getUrlFileName];
- NSString *filePath = [self getAccompanyFilePathWithName:fileName];
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:filePath]) {
- hasSaveFile = YES;
- }
- return hasSaveFile;
- }
- #pragma mark ------- Audio Engine player
- - (AudioEnginePlayer *)delayCheckPlayer {
- if (!_delayCheckPlayer) {
- _delayCheckPlayer = [[AudioEnginePlayer alloc] init];
- _delayCheckPlayer.delegate = self;
- }
- return _delayCheckPlayer;
- }
- - (AudioEnginePlayer *)musicPlayer {
- if (!_musicPlayer) {
- _musicPlayer = [[AudioEnginePlayer alloc] init];
- _musicPlayer.delegate = self;
- }
- return _musicPlayer;
- }
- #pragma mark ---- Audio Engine player delegate
- - (void)enginePlayerIsReadyPlay:(AudioEnginePlayer *)player {
- if (player == self.delayCheckPlayer) {
- self.checkPlayerReady = YES;
- }
- else if (player == self.musicPlayer) {
- self.musicPlayerReady = YES;
- }
- // 如果都准备好
- if (self.musicPlayerReady && self.checkPlayerReady) {
- [self sendPlayerReadyMsg];
- }
- }
- // 播放进度
- - (void)updatePlayProgress:(NSInteger)playTime andTotalTime:(NSInteger)totalTime andProgress:(CGFloat)progress currentInterval:(NSTimeInterval)currentInterval inPlayer:(AudioEnginePlayer *)player {
- if (player == self.delayCheckPlayer) {
- if (playTime >= 300 && self.recordStartTime > 0 && self.hasRecordMusicOffset == NO) {
- self.hasRecordMusicOffset = YES;
- NSLog(@" --- check player start play time %f", currentInterval - playTime);
- self.playerStartTime = currentInterval - playTime;
- self.offsetTime = self.playerStartTime - self.recordStartTime;
- NSLog(@"--------- check player offset time -- %zd", self.offsetTime);
- }
- }
- // 如果未记录延迟
- if (playTime >= (self.musicStartTime + 300) && player == self.musicPlayer && self.hasRecordMusicOffset == NO) {
- if (self.recordStartTime > 0) {
- self.hasRecordMusicOffset = YES;
- NSInteger newPlayTime = (playTime - self.musicStartTime) / player.rate; // 选段
- NSLog(@" --- music player start play time %f", currentInterval - newPlayTime);
- self.playerStartTime = currentInterval - newPlayTime;
- self.offsetTime = self.playerStartTime - self.recordStartTime;
- NSLog(@"--------- music play offset time -- %zd", self.offsetTime);
- [self sendOffsetTimeToService];
- }
- NSLog(@"------- record start time %f", self.recordStartTime);
- }
-
- // 回调进度
- if (player == self.musicPlayer) {
- // NSLog(@"------ music play progress - %f", progress);
- // 回调进度
- NSDictionary *parm = @{
- @"api" : @"playProgress",
- @"content" : @{@"currentTime" : [NSNumber numberWithInteger:playTime],
- @"totalDuration" : [NSNumber numberWithInteger:totalTime],
- }
- };
- // NSLog(@" -----music play progress %@---- ", parm);
- [self postMessage:parm];
- }
-
- }
- // 错误
- - (void)enginePlayerDidError:(AudioEnginePlayer *)player error:(NSError *)error {
- [self stopRecordService];
- [self stopMp3Player]; // 停止播放
- // 播放出现问题
- NSDictionary *postParm = @{@"api" : @"cancelEvaluating",
- @"content" : @{@"reson":@"播放已停止"}
- };
- [self postMessage:postParm];
-
- if (error) {
- NSLog(@"-- error desc - %@", error.description);
- NSMutableDictionary *parm = [NSMutableDictionary dictionary];
- [parm setValue:UserDefault(UIDKey) forKey:@"userId"];
- [parm setValue:@"KSCloudWebViewController_MP3Player" forKey:@"Location"];
- [parm setValue:@(error.code) forKey:@"errorCode"];
- [parm setValue:error.description forKey:@"errorDesc"];
- [parm setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forKey:@"version"];
- NSString *content = [parm mj_JSONString];
- NSMutableDictionary *submitParm = [KSLogManager generateLogMessageWithContent:content type:@"ERROR"];
- NSMutableArray *uploadArray = [NSMutableArray arrayWithObject:submitParm];
- [KSNetworkingManager sysExceptionLogUpdate:KS_POST token:UserDefault(TokenKey) logArray:uploadArray success:^(NSDictionary * _Nonnull dic) {
- if ([dic ks_integerValueForKey:@"code"] == 200) {
-
- }
- } faliure:^(NSError * _Nonnull error) {
-
- }];
- }
- }
- - (void)sendPlayerReadyMsg {
- if (self.playerParm) {
- [self postMessage:self.playerParm];
- }
- }
- - (NSMutableArray *)delayArray {
- if (!_delayArray) {
- _delayArray = [NSMutableArray array];
- }
- return _delayArray;
- }
- /*
- #pragma mark - Navigation
-
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|