KSAccompanyWebViewController.m 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  1. //
  2. // KSAccompanyWebViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/20.
  6. //
  7. #import "KSAccompanyWebViewController.h"
  8. #import "WebViewBaseConfig.h" // 基础配置
  9. #import "SwiftImportHeader.h" // swift 桥接
  10. #import <KSAudioSessionManager.h> // audio session
  11. #import <KSAQRecordManager.h> // 录音
  12. #import "KSTargetWebSocketManager.h" // web socket
  13. #import <KSVideoRecordManager.h> // 视频录制
  14. #import <KSCloudBeatView.h> // 节拍器
  15. #import <MidiPlayerEngine.h> // midi 播放
  16. #import "AccompanyLoadingView.h"
  17. #define KSMidiSongFileKey (@"KSDownloadMidiSong")
  18. // 合成
  19. #import <KSMediaEditor.h>
  20. #import "KSAccompanyDraftViewController.h"
  21. #import <KSTunerLibrary/KSTunerLibrary-Swift.h>
  22. #import "KSLogManager.h"
  23. #import "AudioEnginePlayer.h"
  24. @interface KSAccompanyWebViewController ()<KSAQRecordManagerDelegate,KSAudioSessionManagerDelegate,PlayerEngineDelegate,TunerDelegate,AudioEnginePlayerDelegate>
  25. @property (nonatomic, strong) KSAudioSessionManager *audioSessionManager;
  26. @property (nonatomic, assign) MetronomeType beatType;
  27. @property (nonatomic, strong) MidiPlayerEngine *playerEngine; // player
  28. @property (nonatomic, assign) BOOL initEngineSuccess; // 加载引擎是否成功
  29. @property (nonatomic, assign) float songOriginalSpeed;
  30. @property (nonatomic, assign) float currentSpeed; // 当前播放的速度
  31. @property (nonatomic, assign) BOOL isPlaying; // 是否正在播放的状态
  32. @property (nonatomic, strong) NSString *currentSongId; // song id
  33. @property (nonatomic, strong) NSDictionary *configEngineParm; // 初始化mid播放引擎的参数
  34. @property (nonatomic, strong) NSMutableDictionary *metronomeParm; // 节拍器播放的参数
  35. // 是否发送了musicXML信息
  36. @property (nonatomic, assign) BOOL hasSendStartMessage;
  37. @property (nonatomic, strong) KSAQRecordManager *AQManager;
  38. @property (nonatomic, strong) KSTargetWebSocketManager *socketManager;
  39. @property (nonatomic, strong) NSMutableDictionary *evaluatParm;
  40. // 录制的message
  41. @property (nonatomic, strong) NSMutableDictionary *recordParm;
  42. // 评测开始时发送recordStart消息的标识
  43. @property (nonatomic, assign) BOOL isCompareStart;
  44. // 校音开始时发送 checkStart消息标识
  45. @property (nonatomic, assign) BOOL isSoundCheckStart;
  46. // 自定义UI控件容器
  47. @property (nonatomic, strong) UIView *viewContainer;
  48. @property (nonatomic, strong) KSVideoRecordManager *videoRecordManager;
  49. @property (nonatomic, strong) NSDictionary *endRecordParm;
  50. @property (nonatomic, assign) BOOL isCameraOpen;
  51. @property (nonatomic, strong) AccompanyLoadingView *loadingView;
  52. @property (nonatomic, assign) BOOL isTunerRuning;
  53. @property (nonatomic, strong) Tuner *tuner;
  54. // 延迟校准开始时发送 AdjustStart消息标识
  55. @property (nonatomic, assign) BOOL isDelayCheckStart;
  56. // 检测延迟播放器
  57. @property (nonatomic, strong) AudioEnginePlayer *delayCheckPlayer;
  58. // 音频播放器
  59. @property (nonatomic, strong) AudioEnginePlayer *musicPlayer;
  60. @property (nonatomic, assign) float musicSpeed; //播放速度
  61. // 录音开始时间
  62. @property (nonatomic, assign) NSTimeInterval recordStartTime;
  63. // 播放开始时间
  64. @property (nonatomic, assign) NSTimeInterval playerStartTime;
  65. // 播放延迟
  66. @property (nonatomic, assign) NSInteger offsetTime;
  67. @property (nonatomic, assign) BOOL checkPlayerReady;
  68. @property (nonatomic, assign) BOOL musicPlayerReady;
  69. @property (nonatomic, strong) NSDictionary *playerParm;
  70. @property (nonatomic, strong) NSMutableArray *delayArray;
  71. @property (nonatomic, assign) NSInteger checkIndex;
  72. @property (nonatomic, assign) NSInteger checkPrequence;
  73. @property (nonatomic, strong) NSURL *bgAudioUrl;
  74. @property (nonatomic, strong) NSURL *recordUrl;
  75. @property (nonatomic, strong) NSString *accompanyUrl;
  76. @property (nonatomic, assign) BOOL muteAccompany; // 是否静音
  77. @property (nonatomic, assign) NSInteger musicStartTime; // 开始播放的时间
  78. @property (nonatomic, assign) BOOL hasRecordMusicOffset; // 是否记录了播放延迟
  79. @end
  80. @implementation KSAccompanyWebViewController
  81. - (void)handerAudioInterruption {
  82. if (_playerEngine) {
  83. [self stopPlayAction];
  84. }
  85. if (_musicPlayer) {
  86. [self stopMp3Player];
  87. }
  88. }
  89. - (void)resumeAudioSession {
  90. // 恢复
  91. if (_playerEngine) {
  92. [self.playerEngine resumeAUGraph];
  93. }
  94. }
  95. // 打断处理
  96. - (void)audioInterruption {
  97. if (_videoRecordManager) {
  98. [self.videoRecordManager resetSession];
  99. }
  100. [self handerAudioInterruption];
  101. }
  102. - (void)ignorRecordVideo {
  103. if (_videoRecordManager) {
  104. self.videoRecordManager.skipSaveRecord = YES;
  105. }
  106. }
  107. - (void)stopSession {
  108. if (_videoRecordManager) {
  109. [self.videoRecordManager stopSession];
  110. }
  111. }
  112. - (void)resetPlayerConfig {
  113. [self freeMp3Player]; // 若之前有播放器,剔除
  114. self.checkPrequence = 800;
  115. self.checkPlayerReady = NO;
  116. self.musicPlayerReady = NO;
  117. self.recordStartTime = 0;
  118. self.playerStartTime = 0;
  119. self.offsetTime = 0;
  120. }
  121. - (void)downloadMp3File:(NSString *)musicUrl {
  122. if (![NSString isEmptyString:musicUrl]) {
  123. NSString *fileName = [musicUrl getUrlFileName];
  124. NSString *filePath = [self getAccompanyFilePathWithName:fileName];
  125. NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:filePath];
  126. if ([self checkSongHasSaveAccompanyWithSongUrl:musicUrl]) {
  127. [self configVideoRecord:fileUrl];
  128. [self initMusicPlayer:fileUrl];
  129. }
  130. else {
  131. MJWeakSelf;
  132. [self downloadUrl:musicUrl success:^{
  133. [weakSelf configVideoRecord:fileUrl];
  134. [weakSelf initMusicPlayer:fileUrl];
  135. } faliure:^{
  136. }];
  137. }
  138. }
  139. else {
  140. [self configVideoRecord:nil];
  141. }
  142. }
  143. - (void)initMusicPlayer:(NSURL *)musicUrl {
  144. [self.musicPlayer prepareNativeSongWithUrl:musicUrl];
  145. }
  146. - (void)downloadCheckMusic:(NSString *)checkMusicUrl {
  147. if (![NSString isEmptyString:checkMusicUrl]) {
  148. NSString *fileName = [checkMusicUrl getUrlFileName];
  149. NSString *filePath = [self getAccompanyFilePathWithName:fileName];
  150. NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:filePath];
  151. if ([self checkSongHasSaveAccompanyWithSongUrl:checkMusicUrl]) {
  152. [self initCheckPlayer:fileUrl];
  153. }
  154. else {
  155. MJWeakSelf;
  156. [self downloadUrl:checkMusicUrl success:^{
  157. [weakSelf initCheckPlayer:fileUrl];
  158. } faliure:^{
  159. }];
  160. }
  161. }
  162. }
  163. - (void)initCheckPlayer:(NSURL *)checkUrl {
  164. [self.delayCheckPlayer prepareNativeSongWithUrl:checkUrl];
  165. }
  166. - (void)initMp3Player:(NSString *)musicUrl checkUrl:(NSString *)checkUrl {
  167. [self resetPlayerConfig];
  168. self.accompanyUrl = musicUrl;
  169. [self downloadCheckMusic:checkUrl];
  170. }
  171. - (void)freeMp3Player {
  172. if (_delayCheckPlayer) {
  173. [self.delayCheckPlayer freePlayer];
  174. }
  175. if (_musicPlayer) {
  176. [self.musicPlayer freePlayer];
  177. }
  178. }
  179. - (void)stopMp3Player {
  180. if (_delayCheckPlayer) {
  181. [self.delayCheckPlayer stopPlay];
  182. }
  183. if (_musicPlayer) {
  184. [self.musicPlayer stopPlay];
  185. }
  186. }
  187. - (void)viewDidLoad {
  188. [super viewDidLoad];
  189. // Do any additional setup after loading the view.
  190. [self configAudioSession];
  191. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterBackground) name:@"appEnterBackground" object:nil];
  192. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterForeground) name:@"appEnterForeground" object:nil];
  193. }
  194. - (void)checkMessage:(id)message {
  195. NSDictionary *result = [message mj_JSONObject];
  196. NSString *type = [[result ks_dictionaryValueForKey:@"header"] ks_stringValueForKey:@"type"];
  197. NSString *commond = [[result ks_dictionaryValueForKey:@"header"] ks_stringValueForKey:@"commond"];
  198. if ([type isEqualToString:@"DELAY_CHECK"] && [commond isEqualToString:@"recordEnd"]) {
  199. NSDictionary *body = [result ks_dictionaryValueForKey:@"body"];
  200. NSTimeInterval micDelay = [body ks_doubleValueForKey:@"firstNoteDelayDuration"] - self.offsetTime;
  201. if (micDelay > 0) {
  202. [self.delayArray addObject:[NSNumber numberWithDouble:micDelay]];
  203. }
  204. }
  205. }
  206. - (void)connectSocketService {
  207. MJWeakSelf;
  208. self.socketManager.didReceiveMessage = ^(id _Nonnull message) {
  209. dispatch_async(dispatch_get_main_queue(), ^{
  210. [weakSelf checkMessage:message];
  211. // api
  212. NSMutableDictionary *sendMessage = [NSMutableDictionary dictionary];
  213. [sendMessage setValue:@"sendResult" forKey:@"api"];
  214. [sendMessage setValue:message forKey:@"content"];
  215. // 服务返回数据传给H5
  216. [weakSelf postMessage:sendMessage];
  217. });
  218. };
  219. self.socketManager.connectionStatus = ^(BOOL isSuccess) {
  220. dispatch_async(dispatch_get_main_queue(), ^{
  221. if (!isSuccess) {
  222. // NSLog(@"-----连接失败");
  223. if (weakSelf.hasSendStartMessage) {
  224. if (weakSelf.AQManager.isRunning) {
  225. NSDictionary *postParm = @{@"api" : @"cancelEvaluating",
  226. @"content" : @{@"reson":@"服务异常,请重试"}
  227. };
  228. [weakSelf postMessage:postParm];
  229. [weakSelf stopRecordService];
  230. weakSelf.isCompareStart = NO;
  231. weakSelf.isSoundCheckStart = NO;
  232. weakSelf.isDelayCheckStart = NO;
  233. }
  234. }
  235. else if (weakSelf.evaluatParm) {
  236. NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:weakSelf.evaluatParm];
  237. NSDictionary *valueDic = [weakSelf.evaluatParm ks_dictionaryValueForKey:@"content"];
  238. [valueDic setValue:@"服务异常,请重试" forKey:@"reson"];
  239. [sendParm setValue:valueDic forKey:@"content"];
  240. [weakSelf postMessage:sendParm];
  241. weakSelf.hasSendStartMessage = YES;
  242. weakSelf.isCompareStart = NO;
  243. weakSelf.isSoundCheckStart = NO;
  244. weakSelf.isDelayCheckStart = NO;
  245. }
  246. else { // 其他断开
  247. if (weakSelf.AQManager.isRunning) {
  248. NSDictionary *postParm = @{@"api" : @"cancelEvaluating",
  249. @"content" : @{@"reson":@"服务异常,请重试"}
  250. };
  251. [weakSelf postMessage:postParm];
  252. [weakSelf stopRecordService];
  253. weakSelf.isCompareStart = NO;
  254. weakSelf.isSoundCheckStart = NO;
  255. weakSelf.isDelayCheckStart = NO;
  256. }
  257. }
  258. }
  259. else {
  260. // NSLog(@"-----连接成功");
  261. if (weakSelf.hasSendStartMessage == NO && weakSelf.evaluatParm) {
  262. NSDictionary *content = [weakSelf.evaluatParm ks_dictionaryValueForKey:@"content"];
  263. NSString *sendData = [weakSelf configDataCommond:@"musicXml" body:content type:@"SOUND_COMPARE"];
  264. [weakSelf sendDataToSocketService:sendData];
  265. [weakSelf postMessage:weakSelf.evaluatParm];
  266. weakSelf.hasSendStartMessage = YES;
  267. }
  268. }
  269. });
  270. };
  271. [self.socketManager SRWebSocketOpen];
  272. }
  273. - (void)appEnterBackground {
  274. NSDictionary *parm = @{@"api":@"suspendPlay"};
  275. [self postMessage:parm];
  276. [self handerAudioInterruption];
  277. }
  278. - (void)appEnterForeground {
  279. if (self.isCameraOpen) {
  280. if ([self.videoRecordManager getSessionStatusisActive] == NO) {
  281. [self.videoRecordManager configSessiondisplayInView:self.viewContainer];
  282. }
  283. }
  284. }
  285. - (void)initWebView {
  286. [self.scrollView removeFromSuperview];
  287. [self.view addSubview:self.viewContainer];
  288. [self.viewContainer mas_makeConstraints:^(MASConstraintMaker *make) {
  289. make.left.right.top.bottom.mas_equalTo(self.view);
  290. }];
  291. if (self.myWebView == nil) {
  292. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  293. config.selectionGranularity = WKSelectionGranularityDynamic;
  294. config.allowsInlineMediaPlayback = YES;
  295. config.mediaTypesRequiringUserActionForPlayback = NO;
  296. config.processPool = [KSBaseWKWebViewController singleWkProcessPool];
  297. config.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
  298. [self configUserAgent:config];
  299. //自定义的WKScriptMessageHandler 是为了解决内存不释放的问题
  300. WeakWebViewScriptMessageDelegate *weakScriptMessageDelegate = [[WeakWebViewScriptMessageDelegate alloc] initWithDelegate:self];
  301. //这个类主要用来做native与JavaScript的交互管理
  302. WKUserContentController * wkUController = [[WKUserContentController alloc] init];
  303. [wkUController addScriptMessageHandler:weakScriptMessageDelegate name:SCRIPT_NAME];
  304. config.userContentController = wkUController;
  305. WKPreferences *preferences = [WKPreferences new];
  306. // 是否支出javaScript
  307. preferences.javaScriptEnabled = YES;
  308. //不通过用户交互,是否可以打开窗口
  309. preferences.javaScriptCanOpenWindowsAutomatically = YES;
  310. config.preferences = preferences;
  311. self.myWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
  312. self.myWebView.opaque = NO;
  313. self.myWebView.UIDelegate = self;
  314. self.myWebView.navigationDelegate = self;
  315. self.myWebView.scrollView.bounces = NO;
  316. self.myWebView.backgroundColor = [UIColor clearColor];
  317. self.myWebView.scrollView.backgroundColor = [UIColor clearColor];
  318. #ifdef DEBUG
  319. if (@available(iOS 16.4, *)) {
  320. self.myWebView.inspectable = YES;
  321. }
  322. #endif
  323. // 加载进度条和title
  324. [self.myWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
  325. [self.myWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
  326. [self.view addSubview:self.myWebView];
  327. [self.myWebView mas_makeConstraints:^(MASConstraintMaker *make) {
  328. make.left.right.mas_equalTo(self.view);
  329. make.top.mas_equalTo(self.view.mas_top);
  330. make.bottom.mas_equalTo(self.view.mas_bottom);
  331. }];
  332. self.myWebView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  333. [self setupProgress];
  334. [self loadRequest];
  335. }
  336. else {
  337. [self.myWebView reload];
  338. }
  339. }
  340. - (void)configUserAgent:(WKWebViewConfiguration *)config {
  341. NSString *oldUserAgent = config.applicationNameForUserAgent;
  342. NSString *newAgent = [NSString stringWithFormat:@"%@ %@ %@",oldUserAgent,AGENT_NAME,AGENT_DOMAIN];
  343. config.applicationNameForUserAgent = newAgent;
  344. }
  345. - (void)configRecordManager {
  346. if (self.AQManager.isRunning) {
  347. [self.AQManager stopRecord];
  348. }
  349. self.AQManager = [[KSAQRecordManager alloc] init];
  350. self.AQManager.delegate = self;
  351. }
  352. - (void)viewWillAppear:(BOOL)animated {
  353. [super viewWillAppear:animated];
  354. [self connectSocketService];
  355. if (self.isCameraOpen) {
  356. [self.videoRecordManager configSessiondisplayInView:self.viewContainer];
  357. }
  358. }
  359. - (void)viewWillDisappear:(BOOL)animated {
  360. [super viewWillDisappear:animated];
  361. if (_socketManager) {
  362. [self.socketManager SRWebSocketClose];
  363. _socketManager = nil;
  364. }
  365. // 停止播放和录制
  366. [self stopSession];
  367. [self stopPlayAction];
  368. [self stopRecordService];
  369. [self stopTuner];
  370. BOOL isBack = [self isViewPopDismiss];
  371. if (isBack) { // 页面销毁才删除
  372. if (_AQManager) {
  373. [_AQManager freeAudioQueue];
  374. }
  375. // 如果退出评测页面 清除 playerEngine
  376. if (self.playerEngine) {
  377. [self.playerEngine cleanup];
  378. self.playerEngine = nil;
  379. }
  380. // 返回不保存视频
  381. [self ignorRecordVideo];
  382. [self freeMp3Player];
  383. [self removeTuner];
  384. }
  385. }
  386. - (void)removeTuner {
  387. if (_tuner) {
  388. [_tuner freeTuner];
  389. _tuner = nil;
  390. NSLog(@"--- free tuner ");
  391. }
  392. }
  393. - (void)sendDataToSocketService:(id)data {
  394. if (_socketManager) {
  395. [self.socketManager sendData:data];
  396. }
  397. }
  398. #pragma mark --- WKScriptMessageHandler
  399. - (void)userContentController:(WKUserContentController *)userContentController
  400. didReceiveScriptMessage:(WKScriptMessage *)message {
  401. if ([message.name isEqualToString:SCRIPT_NAME]) {
  402. NSDictionary *parm = [self convertJsonStringToNSDictionary:message.body];
  403. // 回到主线程
  404. dispatch_async(dispatch_get_main_queue(), ^{
  405. if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"createMusicPlayer"]) {
  406. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  407. NSString *musicUrl = [content ks_stringValueForKey:@"musicSrc"];
  408. NSString *checkUrl = [content ks_stringValueForKey:@"tuneSrc"];
  409. self.playerParm = parm;
  410. [self initMp3Player:musicUrl checkUrl:checkUrl];
  411. // 下载文件
  412. [self downloadMp3File:musicUrl];
  413. }
  414. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"startEvaluating"]) { // 开始评测
  415. [self configRecordManager];
  416. self.hasSendStartMessage = NO;
  417. [RecordCheckManager checkMicPermissionAvaiableCallback:^(PREMISSIONTYPE type) {
  418. if (type == PREMISSIONTYPE_YES) {
  419. self.evaluatParm = [NSMutableDictionary dictionaryWithDictionary:parm];
  420. // 如果socket 连上了
  421. if (self.socketManager.socketReadyState == SR_OPEN) {
  422. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  423. NSString *sendData = [self configDataCommond:@"musicXml" body:content type:@"SOUND_COMPARE"];
  424. [self sendDataToSocketService:sendData];
  425. [self postMessage:parm];
  426. self.hasSendStartMessage = YES;
  427. }
  428. else {
  429. [self connectSocketService];
  430. }
  431. }
  432. else {
  433. [self responseMessage:@"storageUnable" desc:@"没有麦克风访问权限" parm:parm];
  434. [self showAlertWithMessage:@"请开启麦克风访问权限" type:CHECKDEVICETYPE_MIC];
  435. }
  436. }];
  437. }
  438. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"endEvaluating"]) {// 停止评测
  439. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  440. self.musicStartTime = 0;
  441. self.evaluatParm = nil;
  442. [self stopRecordService];
  443. [self postMessage:parm];
  444. [self sendEndMessage];
  445. [self stopMp3Player]; // 停止播放
  446. });
  447. }
  448. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cancelEvaluating"]) { // 取消评测
  449. self.musicStartTime = 0;
  450. self.evaluatParm = nil;
  451. [self stopRecordService];
  452. [self postMessage:parm];
  453. [self stopMp3Player]; // 停止播放
  454. }
  455. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"startRecording"]) { // 开始录制
  456. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  457. self.musicStartTime = [content ks_integerValueForKey:@"firstNoteTime"];
  458. if ([[content allKeys] containsObject:@"accompanimentState"]) {
  459. BOOL mute = [content ks_boolValueForKey:@"accompanimentState"] == NO;
  460. self.muteAccompany = mute;
  461. }
  462. else {
  463. self.muteAccompany = NO;
  464. }
  465. if ([[content allKeys] containsObject:@"speedRate"]) { // 播放速度
  466. self.musicSpeed = [content ks_floatValueForKey:@"speedRate"];
  467. }
  468. else {
  469. self.musicSpeed = 1.0f;
  470. }
  471. if (self->_videoRecordManager) {
  472. [self.videoRecordManager clearVideoFile];
  473. }
  474. [self postMessage:parm];
  475. self.isCompareStart = YES;
  476. [self startRecordService];
  477. }
  478. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"endRecording"]) { // 停止录音
  479. self.recordParm = nil;
  480. [self stopRecordService];
  481. [self postMessage:parm];
  482. }
  483. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"pauseRecording"]) {
  484. [self puaseRecordService];
  485. [self postMessage:parm];
  486. }
  487. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"resumeRecording"]) {
  488. [self resumeRecordService];
  489. [self postMessage:parm];
  490. }
  491. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"isWiredHeadsetOn"]) {
  492. [self configAudioDeviceType:parm];
  493. }
  494. // 发送消息给socket service
  495. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"proxyMessage"]) {
  496. [self sendMessageToSocket:parm];
  497. }
  498. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"proxyServiceMessage"]) {
  499. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  500. NSString *sendData = [content mj_JSONString];
  501. NSLog(@"proxyServiceMessage ------- %@",sendData);
  502. [self sendDataToSocketService:sendData];
  503. }
  504. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"openCamera"]) { // 开启摄像头
  505. [RecordCheckManager checkCameraPremissionAvaiableCallback:^(PREMISSIONTYPE type) {
  506. [self afterCheckCameraCheckAlbum:type parm:parm];
  507. }];
  508. }
  509. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"closeCamera"]) { // 关闭摄像头
  510. self.isCameraOpen = NO;
  511. if (self->_videoRecordManager) {
  512. [self.videoRecordManager removeDisplay];
  513. }
  514. [self postMessage:parm];
  515. }
  516. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"startCapture"]) { // 开始录制
  517. [RecordCheckManager checkPhotoLibraryPremissionAvaiableCallback:^(PREMISSIONTYPE type) {
  518. if (type == PREMISSIONTYPE_YES) {
  519. [self.videoRecordManager setIgnoreAudio:YES];
  520. self.videoRecordManager.audioUrl = self.AQManager.audioUrl;
  521. [self.videoRecordManager startRecord];
  522. [self postMessage:parm];
  523. }
  524. else {
  525. // [self responseMessage:@"storageUnable" desc:@"没有相册存储权限" parm:parm];
  526. // [self showAlertWithMessage:@"开启相册存储" type:CHECKDEVICETYPE_CAMREA];
  527. }
  528. }];
  529. }
  530. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"endCapture"]) { // 结束录制
  531. if (self->_videoRecordManager) {
  532. self.endRecordParm = parm;
  533. [self.videoRecordManager stopRecord];
  534. }
  535. else {
  536. [self postMessage:parm];
  537. }
  538. }
  539. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"setCaptureMode"]) {
  540. NSString *modeString = [[parm ks_dictionaryValueForKey:@"content"] ks_stringValueForKey:@"mode"];
  541. BOOL isIgnoreAudio = [modeString isEqualToString:@"evaluating"];
  542. [self postMessage:parm];
  543. [self.videoRecordManager setIgnoreAudio:isIgnoreAudio];
  544. }
  545. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"startSoundCheck"]) { // 开始校音
  546. [self configRecordManager];
  547. [self postMessage:parm];
  548. self.isCompareStart = NO;
  549. self.isSoundCheckStart = YES;
  550. self.isDelayCheckStart = NO;
  551. [self startRecordService];
  552. }
  553. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"endSoundCheck"]) {
  554. // 结束校音
  555. self.isSoundCheckStart = NO;
  556. [self stopRecordService];
  557. [self postMessage:parm];
  558. }
  559. // 音视频合成
  560. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"openAdjustRecording"]) {
  561. KSAccompanyDraftViewController *ctrl = [[KSAccompanyDraftViewController alloc] init];
  562. ctrl.ks_landScape = YES;
  563. if (self.bgAudioUrl == nil) {
  564. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"当前曲目无mp3伴奏"];
  565. }
  566. else {
  567. if (self.AQManager && self.AQManager.audioUrl) {
  568. self.recordUrl = self.AQManager.audioUrl;
  569. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  570. ctrl.recordId = [content ks_stringValueForKey:@"recordId"];
  571. ctrl.songName = [content ks_stringValueForKey:@"title"];
  572. ctrl.coverImage = [content ks_stringValueForKey:@"coverImg"];
  573. if ([[content allKeys] containsObject:@"speedRate"]) {
  574. ctrl.musicSpeed = [content ks_floatValueForKey:@"speedRate"];
  575. }
  576. else {
  577. ctrl.musicSpeed = 1.0f;
  578. }
  579. MJWeakSelf;
  580. NSInteger micDelay = [UserDefaultObjectForKey(@"micDelay") integerValue];
  581. NSInteger defaultDelay = self.offsetTime + micDelay;
  582. [ctrl configWithVideoUrl:self.videoRecordManager.videoFileURL bgAudioUrl:self.bgAudioUrl remoteBgUrl:self.accompanyUrl recordUrl:self.recordUrl offsetTime:defaultDelay mergeCallback:^(BOOL isPublished) {
  583. [weakSelf appEnterForeground];
  584. if (isPublished) {
  585. [weakSelf musicPublishCallBack:content];
  586. }
  587. }];
  588. [self.navigationController pushViewController:ctrl animated:NO];
  589. }
  590. else {
  591. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"麦克风被占用"];
  592. }
  593. }
  594. }
  595. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"videoUpdate"]) { // 上传
  596. NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:parm];
  597. NSMutableDictionary *contentParm = [NSMutableDictionary dictionaryWithDictionary:[sendParm ks_dictionaryValueForKey:@"content"]];
  598. if (self.videoRecordManager) {
  599. MJWeakSelf;
  600. [self.videoRecordManager saveVideoCallback:^(BOOL isSuccess, NSString * _Nullable message) {
  601. if (isSuccess) {
  602. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"已保存到相册"];
  603. [weakSelf uploadVideoWithParm:contentParm sendParm:sendParm];
  604. }
  605. }];
  606. }
  607. }
  608. #pragma mark -------- 云教练原生播放对接
  609. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudDetail"]) { // 初始化方法
  610. /**
  611. api: 'cloudDetail',
  612. content: {
  613. midi: '',
  614. denominator: 4,
  615. numerator: 4,
  616. // xml整体原始速度
  617. originalSpeed: 90,
  618. // 间隔(ms)
  619. interval: 50
  620. }
  621. */
  622. [self configAudioSession];
  623. // 重置track num array
  624. self.configEngineParm = [parm copy];
  625. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  626. NSString *midiUrl = [content ks_stringValueForKey:@"midi"];
  627. NSInteger denominator = [content ks_integerValueForKey:@"denominator"];
  628. NSInteger numerator = [content ks_integerValueForKey:@"numerator"];
  629. NSString *beatString = [NSString stringWithFormat:@"%zd/%zd", numerator,denominator];
  630. self.beatType = [self getBeatTypeFromString:beatString];
  631. float originalSpeed = [content ks_floatValueForKey:@"originalSpeed"];
  632. self.songOriginalSpeed = originalSpeed;
  633. self.currentSpeed = originalSpeed;
  634. NSInteger reportInterval = [content ks_integerValueForKey:@"interval"];
  635. // 下载midi文件
  636. BOOL hasSaveSong = [self checkSongHasSaveWithSongUrl:midiUrl];
  637. NSString *fileName = [midiUrl getUrlFileName];
  638. NSString *filePath = [self getFilePathWithName:fileName];
  639. if (hasSaveSong) {
  640. [self configPlayerEngineWithSong:filePath reportTime:reportInterval];
  641. }
  642. else {
  643. MJWeakSelf;
  644. [self downloadMidiFile:midiUrl success:^{
  645. [weakSelf configPlayerEngineWithSong:filePath reportTime:reportInterval];
  646. } faliure:^{
  647. }];
  648. }
  649. }
  650. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudGetMediaStatus"]) { // 获取播放状态
  651. /**
  652. api: 'cloudGetMediaStatus',
  653. content: {
  654. status: 'init' | 'play' | 'suspend'
  655. }
  656. */
  657. NSString *engineStatus = @"init";
  658. if (self.initEngineSuccess) {
  659. if (self.isPlaying) {
  660. engineStatus = @"play";
  661. }
  662. else {
  663. engineStatus = @"suspend";
  664. }
  665. }
  666. NSMutableDictionary *valueDic = [NSMutableDictionary dictionaryWithDictionary:[parm ks_dictionaryValueForKey:@"content"]];
  667. [valueDic setValue:engineStatus forKey:@"status"];
  668. NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:parm];
  669. [sendParm setValue:valueDic forKey:@"content"];
  670. [self postMessage:sendParm];
  671. }
  672. // 播放、暂停、进度、播放结束、跳转指定位置
  673. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudPlay"]) { // 播放
  674. /**
  675. api: 'cloudPlay',
  676. content: {
  677. // 当前曲目id
  678. songID: 0,
  679. // xml整体原始速度
  680. originalSpeed: 90,
  681. // 当前选择速度
  682. speed: 90,
  683. // 开始时间(ms)
  684. startTime: 0
  685. // 播放频率
  686. hertz:440
  687. }
  688. */
  689. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  690. self.currentSongId = [content ks_stringValueForKey:@"songID"];
  691. float speed = [content ks_floatValueForKey:@"speed"];
  692. float originalSpeed = [content ks_floatValueForKey:@"originalSpeed"];
  693. double rate = speed / originalSpeed;
  694. self.currentSpeed = speed;
  695. float hertz = [content ks_floatValueForKey:@"hertz"];
  696. // 播放的hertz
  697. [self.playerEngine adjustPitchByHZ:hertz];
  698. [self.playerEngine setMusicPlayerSpeed:rate];
  699. Float64 startTime = [content ks_doubleValueForKey:@"startTime"];
  700. NSLog(@"------%@", [content ks_stringValueForKey:@"startTime"]);
  701. [self.playerEngine setProgressTime:(startTime/1000)];
  702. // 播放
  703. [self playAction];
  704. [self postMessage:parm];
  705. }
  706. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudSuspend"]) { // 暂停
  707. /**
  708. api: 'cloudSuspend',
  709. content: {
  710. // 当前曲目id
  711. songID: 0,
  712. }
  713. */
  714. [self stopPlayAction];
  715. [self postMessage:parm];
  716. }
  717. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudSetCurrentTime"]) { // // 跳转指定位置
  718. /**
  719. api: 'cloudSetCurrentTime',
  720. content: {
  721. // 当前曲目id
  722. songID: 0,
  723. // 指定位置时间(ms)
  724. currentTime: 0
  725. }
  726. */
  727. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  728. Float64 currentTime = [content ks_doubleValueForKey:@"currentTime"];
  729. if (self.playerEngine) {
  730. [self.playerEngine setProgressTime:(currentTime/1000)];
  731. }
  732. [self postMessage:parm];
  733. }
  734. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudChangeSpeed"]) { // 调速 范围(45-270整数)
  735. /**
  736. api: 'cloudChangeSpeed',
  737. content: {
  738. // 当前曲目id
  739. songID: 0,
  740. // 调整速度
  741. speed: 90
  742. // xml整体原始速度
  743. originalSpeed: 90,
  744. }
  745. */
  746. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  747. float speed = [content ks_floatValueForKey:@"speed"];
  748. float originalSpeed = [content ks_floatValueForKey:@"originalSpeed"];
  749. double rate = speed / originalSpeed;
  750. self.currentSpeed = speed;
  751. [self.playerEngine setMusicPlayerSpeed:rate];
  752. // 回报信息
  753. [self postMessage:parm];
  754. }
  755. // 设置每个轨道音量
  756. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudVolume"]) {
  757. /**
  758. api: 'cloudVolume',
  759. content: {
  760. parts: [
  761. {
  762. name: '',
  763. volume: 90,//0-100
  764. }
  765. ]
  766. }
  767. */
  768. NSLog(@"-cloudVolume -----%@",parm);
  769. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  770. int instrumentId = [content ks_intValueForKey:@"activeMidiId"];
  771. float volume = [content ks_floatValueForKey:@"activeMidiVolume"];
  772. if (instrumentId < 0) {
  773. [self postMessage:parm];
  774. [self.playerEngine getAllTrackVolume];
  775. return;
  776. }
  777. [self.playerEngine volumeTrackVolumeWithInstrumentId:instrumentId volume:volume/100];
  778. [self postMessage:parm];
  779. [self.playerEngine getAllTrackVolume];
  780. }
  781. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudMetronome"]) { // 节拍器播放
  782. self.metronomeParm = [NSMutableDictionary dictionaryWithDictionary:parm];
  783. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  784. NSInteger repeatCount = [content ks_integerValueForKey:@"repeat"];
  785. NSInteger denominator = [content ks_integerValueForKey:@"denominator"];
  786. NSInteger numerator = [content ks_integerValueForKey:@"numerator"];
  787. NSInteger supplement = [content ks_integerValueForKey:@"supplement"];
  788. NSString *beatString = [NSString stringWithFormat:@"%zd/%zd", numerator,denominator];
  789. self.beatType = [self getBeatTypeFromString:beatString];
  790. [self showBeatViewRepeatCount:repeatCount supplement:supplement];
  791. }
  792. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudDestroy"]) { // 销毁播放器
  793. self.initEngineSuccess = NO;
  794. // 重置track num array
  795. self.isPlaying = NO;
  796. if (self.playerEngine) {
  797. [self.playerEngine cleanup];
  798. self.playerEngine = nil;
  799. }
  800. }
  801. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudLoading"]) { // loading
  802. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  803. BOOL showLoading = [content ks_boolValueForKey:@"show"];
  804. if (showLoading) {
  805. [self showCustomLoading];
  806. }
  807. else {
  808. [self removeCustomLoadingView];
  809. }
  810. }
  811. // 跟音
  812. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudToggleFollow"]) { // 跟音
  813. [RecordCheckManager checkMicPermissionAvaiableCallback:^(PREMISSIONTYPE type) {
  814. if (type == PREMISSIONTYPE_YES) {
  815. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  816. NSString *status = [content ks_stringValueForKey:@"state"];
  817. if ([status isEqualToString:@"start"]) { // 开始
  818. [self configAudioSession];
  819. [self startTuner];
  820. }
  821. else if ([status isEqualToString:@"end"]) { // 结束
  822. [self stopTuner];
  823. }
  824. [self postMessage:parm];
  825. }
  826. else {
  827. [self responseMessage:@"storageUnable" desc:@"没有麦克风权限" parm:parm];
  828. [self showAlertWithMessage:@"请开启麦克风访问权限" type:CHECKDEVICETYPE_MIC];
  829. }
  830. }];
  831. }
  832. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"cloudAccompanyMessage"]) { // 获取伴奏 废弃⚠️
  833. }
  834. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"startTune"]) { // 延迟测试开始
  835. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  836. NSInteger count = [content ks_integerValueForKey:@"count"];
  837. if (count == 0) {
  838. [self.delayArray removeAllObjects];
  839. self.checkIndex = 0;
  840. }
  841. self.checkIndex += 1;
  842. [self configRecordManager];
  843. [self postMessage:parm];
  844. self.isCompareStart = NO;
  845. self.isSoundCheckStart = NO;
  846. self.isDelayCheckStart = YES;
  847. [self startRecordService];
  848. }
  849. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"endTune"]) { // 延迟测试停止
  850. self.isDelayCheckStart = NO;
  851. [self stopRecordService];
  852. [self postMessage:parm];
  853. [self stopMp3Player];
  854. [self sendAdjustEndMessage]; // 发送结束消息
  855. }
  856. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"finishTune"]) { // 延迟测试结束
  857. NSDictionary *content = [parm ks_dictionaryValueForKey:@"content"];
  858. NSInteger errorCount = 0;
  859. NSInteger totalDelay = 0;
  860. for (NSInteger index = 0 ; index < self.delayArray.count; index++) {
  861. NSInteger micDelay = [self.delayArray[index] integerValue];
  862. if (micDelay > 10 && micDelay < 300) {
  863. totalDelay += micDelay;
  864. }
  865. else {
  866. errorCount++;
  867. }
  868. }
  869. NSMutableDictionary *contentParm = [NSMutableDictionary dictionaryWithDictionary:content];
  870. if (errorCount > 1) { // 错误次数过多
  871. [contentParm setValue:@(NO) forKey:@"result"];
  872. }
  873. else {
  874. [contentParm setValue:@(YES) forKey:@"result"];
  875. NSInteger averageDelay = totalDelay / (self.delayArray.count - errorCount);
  876. UserDefaultSet([NSNumber numberWithDouble:averageDelay], @"micDelay");
  877. }
  878. NSMutableDictionary *sendParm = [NSMutableDictionary dictionary];
  879. [sendParm setValue:@"finishTune" forKey:@"api"];
  880. [sendParm setValue:contentParm forKey:@"content"];
  881. [self postMessage:sendParm];
  882. [self.delayArray removeAllObjects];
  883. self.checkIndex = 0;
  884. }
  885. else if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"getDeviceDelay"]) {
  886. NSInteger micDelay = [UserDefaultObjectForKey(@"micDelay") integerValue];
  887. NSMutableDictionary *content = [NSMutableDictionary dictionaryWithDictionary:[parm ks_dictionaryValueForKey:@"content"]];
  888. [content setValue:[NSNumber numberWithInteger:micDelay] forKey:@"value"];
  889. NSMutableDictionary *sendParm = [NSMutableDictionary dictionary];
  890. [sendParm setValue:@"getDeviceDelay" forKey:@"api"];
  891. [sendParm setValue:content forKey:@"content"];
  892. [self postMessage:sendParm];
  893. }
  894. else {
  895. [super handleScriptMessageSource:parm];
  896. }
  897. });
  898. }
  899. }
  900. - (void)afterCheckCameraCheckAlbum:(PREMISSIONTYPE)cameraType parm:(NSDictionary *)sourceParm {
  901. [RecordCheckManager checkPhotoLibraryPremissionAvaiableCallback:^(PREMISSIONTYPE type) {
  902. if (type == PREMISSIONTYPE_YES && cameraType == PREMISSIONTYPE_YES) {
  903. self.isCameraOpen = YES;
  904. [self.videoRecordManager setIgnoreAudio:YES];
  905. [self.videoRecordManager configSessiondisplayInView:self.viewContainer];
  906. [self postMessage:sourceParm];
  907. }
  908. else { //
  909. NSString *content = @"";
  910. NSString *des = @"";;
  911. if (cameraType == PREMISSIONTYPE_NO && type == PREMISSIONTYPE_NO) {
  912. des = @"没有相机和相册访问权限";
  913. content = @"请开启相机和相册访问权限";
  914. }
  915. else if (cameraType == PREMISSIONTYPE_NO && type == PREMISSIONTYPE_YES) {
  916. des = @"没有相机访问权限";
  917. content = @"请开启相机访问权限";
  918. }
  919. else if (cameraType == PREMISSIONTYPE_YES && type == PREMISSIONTYPE_NO) {
  920. des = @"没有相册访问权限";
  921. content = @"请开启相册访问权限";
  922. }
  923. [self responseMessage:@"storageUnable" desc:des parm:sourceParm];
  924. [self showAlertWithMessage:content type:CHECKDEVICETYPE_CAMREA];
  925. }
  926. }];
  927. }
  928. - (void)responseMessage:(NSString *)reson desc:(NSString *)desc parm:(NSDictionary *)parm {
  929. NSMutableDictionary *sendContent = [NSMutableDictionary dictionaryWithDictionary:[parm ks_dictionaryValueForKey:@"content"]];
  930. [sendContent setValue:reson forKey:@"reson"];
  931. [sendContent setValue:desc forKey:@"des"];
  932. NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:parm];
  933. [sendParm setValue:sendContent forKey:@"content"];
  934. [self postMessage:sendParm];
  935. }
  936. - (void)musicPublishCallBack:(NSDictionary *)content {
  937. NSMutableDictionary *parm = [NSMutableDictionary dictionary];
  938. [parm setValue:@"hideComplexButton" forKey:@"api"];
  939. [parm setValue:@{} forKey:@"content"];
  940. [self postMessage:parm];
  941. }
  942. - (void)uploadVideoWithParm:(NSMutableDictionary *)contentParm sendParm:(NSMutableDictionary *)sendParm {
  943. MJWeakSelf;
  944. [self.videoRecordManager exportRecordVideoUploadSuccess:^(NSString * _Nonnull videoFileUrl) {
  945. } failure:^(NSString * _Nonnull desc) {
  946. }];
  947. }
  948. - (void)showAlertWithMessage:(NSString *)message type:(CHECKDEVICETYPE)deviceType {
  949. [KSPremissionAlert shareInstanceDisplayImage:deviceType message:message showInView:self.view cancel:^{
  950. } confirm:^{
  951. [self openSettingView];
  952. }];
  953. }
  954. - (void)openSettingView {
  955. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
  956. }
  957. - (void)downloadUrl:(NSString *)url success:(void(^)(void))success faliure:(void(^)(void))faliure {
  958. [KSNetworkingManager downloadFileRequestWithFileUrl:url progress:^(int64_t bytesRead, int64_t totalBytes) {
  959. } success:^(NSURL * _Nonnull fileUrl) {
  960. if ([self saveAccompanyFileWithUrl:fileUrl accompanyUrl:url]) {
  961. if (success) {
  962. success();
  963. }
  964. }
  965. } faliure:^(NSError * _Nonnull error) {
  966. if (faliure) {
  967. faliure();
  968. }
  969. }];
  970. }
  971. - (void)configVideoRecord:(NSURL *)path {
  972. self.videoRecordManager.bgAudioUrl = path;
  973. self.bgAudioUrl = path;
  974. }
  975. - (void)showBeatViewRepeatCount:(NSInteger)repeatCount supplement:(NSInteger)supplement {
  976. KSCloudBeatView *beatView = [KSCloudBeatView shareInstanceWithBeatType:self.beatType speed:self.currentSpeed repeatCount:repeatCount supplement:supplement];
  977. MJWeakSelf;
  978. [beatView startPlayWithEndCallback:^(BOOL isCancle) {
  979. if (isCancle) { // 取消
  980. [weakSelf sendEndMetronomeMessage:YES];
  981. }
  982. else { // 播放完成
  983. [weakSelf sendEndMetronomeMessage:NO];
  984. }
  985. }];
  986. [self.view addSubview:beatView];
  987. }
  988. - (void)sendEndMetronomeMessage:(BOOL)isCancel {
  989. NSString *status = isCancel ? @"cancel" : @"finish";
  990. NSMutableDictionary *valueDic = [NSMutableDictionary dictionaryWithDictionary:[self.metronomeParm ks_dictionaryValueForKey:@"content"]];
  991. [valueDic setValue:status forKey:@"status"];
  992. NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:self.metronomeParm];
  993. [sendParm setValue:valueDic forKey:@"content"];
  994. [self postMessage:sendParm];
  995. }
  996. - (void)downloadMidiFile:(NSString *)midiUrl success:(void(^)(void))success faliure:(void(^)(void))faliure {
  997. [KSNetworkingManager downloadFileRequestWithFileUrl:midiUrl progress:^(int64_t bytesRead, int64_t totalBytes) {
  998. } success:^(NSURL * _Nonnull fileUrl) {
  999. if ([self saveMidiFileWithUrl:fileUrl midiUrl:midiUrl]) {
  1000. if (success) {
  1001. success();
  1002. }
  1003. }
  1004. } faliure:^(NSError * _Nonnull error) {
  1005. if (faliure) {
  1006. faliure();
  1007. }
  1008. }];
  1009. }
  1010. - (BOOL)saveMidiFileWithUrl:(NSURL *)fileUrl midiUrl:(NSString *)midiUrl {
  1011. NSData *sourceData = [NSData dataWithContentsOfURL:fileUrl];
  1012. NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  1013. NSString *filePath= [cachePath stringByAppendingPathComponent:@"MidiSong"];
  1014. // 先创建子目录
  1015. NSFileManager *fileManager = [NSFileManager defaultManager];
  1016. if (![fileManager fileExistsAtPath:filePath]) {
  1017. [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
  1018. }else{
  1019. NSLog(@"已创建文件夹");
  1020. }
  1021. NSString *fileName = [midiUrl getUrlFileName];
  1022. NSString *tempPath = [filePath stringByAppendingPathComponent:fileName];
  1023. BOOL success = [sourceData writeToFile:tempPath atomically:NO];
  1024. return success;
  1025. }
  1026. - (NSString *)getFilePathWithName:(NSString *)fileName {
  1027. NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  1028. NSString *filePath= [cachePath stringByAppendingPathComponent:@"MidiSong"];
  1029. return [filePath stringByAppendingPathComponent:fileName];;
  1030. }
  1031. - (BOOL)checkSongHasSaveWithSongUrl:(NSString *)songUrl {
  1032. BOOL hasSaveFile = NO;
  1033. NSString *fileName = [songUrl getUrlFileName];
  1034. NSString *filePath = [self getFilePathWithName:fileName];
  1035. NSFileManager *fileManager = [NSFileManager defaultManager];
  1036. if ([fileManager fileExistsAtPath:filePath]) {
  1037. hasSaveFile = YES;
  1038. }
  1039. return hasSaveFile;
  1040. }
  1041. - (void)sendMessageToSocket:(NSDictionary *)parm {
  1042. NSString *messageHeader = @"proxyMessage";
  1043. NSString *sendMessage = [self configDataCommond:messageHeader body:[parm ks_dictionaryValueForKey:@"content"]];
  1044. [self sendDataToSocketService:sendMessage];
  1045. }
  1046. - (void)configAudioDeviceType:(NSDictionary *)parm {
  1047. AUDIODEVICE_TYPE type = [KSAQRecordManager queryAudioOutputDeviceType];
  1048. NSString *valueStr = @"";
  1049. BOOL checkIsWired = NO;
  1050. switch (type) {
  1051. case AUDIODEVICE_TYPE_HEADPHONE:
  1052. {
  1053. valueStr = @"有线耳机";
  1054. checkIsWired = YES;
  1055. }
  1056. break;
  1057. case AUDIODEVICE_TYPE_BLUETOOTH:
  1058. {
  1059. valueStr = @"蓝牙耳机";
  1060. checkIsWired = YES;
  1061. }
  1062. break;
  1063. case AUDIODEVICE_TYPE_NONE:
  1064. {
  1065. valueStr = @"";
  1066. checkIsWired = NO;
  1067. }
  1068. break;
  1069. default:
  1070. break;
  1071. }
  1072. NSMutableDictionary *valueDic = [NSMutableDictionary dictionaryWithDictionary:[parm ks_dictionaryValueForKey:@"content"]];
  1073. [valueDic setValue:valueStr forKey:@"type"];
  1074. [valueDic setValue:[NSNumber numberWithBool:checkIsWired] forKey:@"checkIsWired"];
  1075. NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:parm];
  1076. [sendParm setValue:valueDic forKey:@"content"];
  1077. [self postMessage:sendParm];
  1078. }
  1079. - (void)startRecordService {
  1080. if (self.AQManager.isRunning) {
  1081. [self.AQManager stopRecord];
  1082. }
  1083. [self.AQManager startRecord];
  1084. }
  1085. - (void)stopRecordService {
  1086. if (self.AQManager.isRunning) {
  1087. [self.AQManager stopRecord];
  1088. }
  1089. }
  1090. - (void)puaseRecordService {
  1091. if (self.AQManager.isRunning) {
  1092. [self.AQManager pauserRecord];
  1093. }
  1094. }
  1095. - (void)resumeRecordService {
  1096. [self.AQManager resumeRecord];
  1097. }
  1098. - (void)sendEndMessage {
  1099. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1100. // 上传停止的信息 发送给服务端
  1101. NSString *endMessage = @"recordEnd";
  1102. NSString *endData = [self configDataCommond:endMessage body:nil type:@"SOUND_COMPARE"];
  1103. [self sendDataToSocketService:endData];
  1104. self.isCompareStart = NO;
  1105. NSLog(@"---- send end message");
  1106. });
  1107. }
  1108. - (void)sendAdjustEndMessage {
  1109. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1110. // 上传停止的信息 发送给服务端
  1111. NSString *endMessage = @"recordEnd";
  1112. NSString *endData = [self configDataCommond:endMessage body:nil type:@"DELAY_CHECK"];
  1113. [self sendDataToSocketService:endData];
  1114. self.isDelayCheckStart = NO;
  1115. NSLog(@"---- send adjust end message");
  1116. });
  1117. }
  1118. - (void)sendOffsetTimeToService {
  1119. // 上传停止的信息 发送给服务端
  1120. NSString *offsetMessage = @"audioPlayStart";
  1121. NSTimeInterval micDelay = [UserDefault(@"micDelay") doubleValue];
  1122. NSDictionary *dic = @{@"offsetTime" : [NSNumber numberWithInteger:self.offsetTime], @"micDelay": [NSNumber numberWithInteger:micDelay]};
  1123. NSString *endData = [self configDataCommond:offsetMessage body:dic type:@"SOUND_COMPARE"];
  1124. NSLog(@"------ %@", endData);
  1125. [self sendDataToSocketService:endData];
  1126. self.isCompareStart = NO;
  1127. }
  1128. #pragma mark-------- KSAQRecordManagerDelegate
  1129. - (void)recordInterruption {
  1130. NSDictionary *postParm = @{@"api" : @"cancelEvaluating",
  1131. @"content" : @{@"reson":@"录制错误,请重试"}
  1132. };
  1133. [self postMessage:postParm];
  1134. }
  1135. - (void)audioRouteChange:(AUDIODEVICE_TYPE)type {
  1136. NSString *valueStr = @"";
  1137. BOOL checkIsWired = NO;
  1138. switch (type) {
  1139. case AUDIODEVICE_TYPE_HEADPHONE:
  1140. {
  1141. valueStr = @"有线耳机";
  1142. checkIsWired = YES;
  1143. }
  1144. break;
  1145. case AUDIODEVICE_TYPE_BLUETOOTH:
  1146. {
  1147. valueStr = @"蓝牙耳机";
  1148. checkIsWired = YES;
  1149. }
  1150. break;
  1151. case AUDIODEVICE_TYPE_NONE:
  1152. {
  1153. valueStr = @"";
  1154. checkIsWired = NO;
  1155. }
  1156. break;
  1157. default:
  1158. break;
  1159. }
  1160. NSDictionary *postParm = @{@"api" : @"listenerWiredStatus",
  1161. @"content" : @{@"type":valueStr,
  1162. @"checkIsWired":[NSNumber numberWithBool:checkIsWired]
  1163. }
  1164. };
  1165. [self postMessage:postParm];
  1166. }
  1167. #pragma mark ------- 评测app播放
  1168. - (void)recordDidStart:(NSTimeInterval)time { // ms
  1169. self.hasRecordMusicOffset = NO;
  1170. self.recordStartTime = time;
  1171. if (self.isDelayCheckStart) {
  1172. NSLog(@"---- delay - record did start %f", time);
  1173. // 播放音频
  1174. // 播放校音音频
  1175. dispatch_main_sync_safe(^{
  1176. [self.delayCheckPlayer seekToTimePlay:0];
  1177. });
  1178. }
  1179. else if (self.isCompareStart) {
  1180. NSLog(@"---- compare - record did start %f", time);
  1181. if (self.playerEngine == nil && _musicPlayer) {
  1182. dispatch_main_sync_safe(^{
  1183. self.musicPlayer.isMute = self.muteAccompany;
  1184. self.musicPlayer.rate = self.musicSpeed;
  1185. // 进度跳转
  1186. [self.musicPlayer seekToTimePlay:self.musicStartTime];
  1187. });
  1188. }
  1189. }
  1190. }
  1191. - (void)audioRecord:(KSAQRecordManager *)audioRecord didRecordAudioData:(void *)data length:(UInt32)length {
  1192. if (self.socketManager.socketReadyState != SR_OPEN) {
  1193. return;
  1194. }
  1195. NSData *pushData = [[NSData alloc] initWithBytes:data length:length];
  1196. if (self.isCompareStart) { // 发送评测开始消息
  1197. dispatch_async(dispatch_get_main_queue(), ^{
  1198. NSDate *date = [NSDate date];
  1199. NSTimeInterval inteveral = [date timeIntervalSince1970];
  1200. double beginTime = inteveral - audioRecord.sampleTime;
  1201. NSDictionary *parm = @{
  1202. @"api" : @"recordStartTime",
  1203. @"content" : @{@"inteveral" : [NSNumber numberWithDouble:beginTime]}
  1204. };
  1205. [self postMessage:parm];
  1206. });
  1207. NSLog(@"--------- send start message");
  1208. _isCompareStart = NO;
  1209. NSString *startMessage = @"recordStart";
  1210. NSString *startString = [self configDataCommond:startMessage body:nil type:@"SOUND_COMPARE"];
  1211. [self sendDataToSocketService:startString];
  1212. }
  1213. else if (self.isSoundCheckStart) { // 校音开始
  1214. NSLog(@"--------- send check start message");
  1215. _isSoundCheckStart = NO;
  1216. NSString *checkStartMessage = @"start";
  1217. NSString *startString = [self configDataCommond:checkStartMessage body:nil type:@"PITCH_DETECTION"];
  1218. [self sendDataToSocketService:startString];
  1219. }
  1220. else if (self.isDelayCheckStart) {
  1221. NSLog(@"--------- send delay check start message");
  1222. _isDelayCheckStart = NO;
  1223. NSString *checkStartMessage = @"recordStart";
  1224. NSInteger frequence = self.checkPrequence;
  1225. NSDictionary *parm = @{@"HZ" : @(frequence)};
  1226. NSString *startString = [self configDataCommond:checkStartMessage body:parm type:@"DELAY_CHECK"];
  1227. [self sendDataToSocketService:startString];
  1228. }
  1229. // NSLog(@"--------- send audio data length %d", length);
  1230. [self sendDataToSocketService:pushData];
  1231. }
  1232. - (NSString *)configDataCommond:(NSString *)commond body:(id)bodyMessage type:(NSString *)dataType {
  1233. NSMutableDictionary *parm = [NSMutableDictionary dictionary];
  1234. if (bodyMessage) {
  1235. [parm setValue:bodyMessage forKey:@"body"];
  1236. }
  1237. NSMutableDictionary *headerParm = [NSMutableDictionary dictionary];
  1238. if ([NSString isEmptyString:commond]) {
  1239. [headerParm setValue:@"" forKey:@"commond"];
  1240. }
  1241. else {
  1242. [headerParm setValue:commond forKey:@"commond"];
  1243. }
  1244. if (![NSString isEmptyString:dataType]) {
  1245. [headerParm setValue:dataType forKey:@"type"];
  1246. }
  1247. [headerParm setValue:@(200) forKey:@"status"];
  1248. [parm setValue:headerParm forKey:@"header"];
  1249. return [parm mj_JSONString];
  1250. }
  1251. - (NSString *)configDataCommond:(NSString *)commond body:(id)bodyMessage {
  1252. NSMutableDictionary *parm = [NSMutableDictionary dictionary];
  1253. if (bodyMessage) {
  1254. [parm setValue:bodyMessage forKey:@"body"];
  1255. }
  1256. if ([NSString isEmptyString:commond]) {
  1257. [parm setValue:@{@"commond":@""} forKey:@"header"];
  1258. }
  1259. else {
  1260. [parm setValue:@{@"commond":commond} forKey:@"header"];
  1261. }
  1262. return [parm mj_JSONString];
  1263. }
  1264. - (KSTargetWebSocketManager *)socketManager {
  1265. if (!_socketManager) {
  1266. _socketManager = [[KSTargetWebSocketManager alloc] init];
  1267. }
  1268. return _socketManager;
  1269. }
  1270. #pragma mark --- lazying
  1271. - (UIView *)viewContainer {
  1272. if (!_viewContainer) {
  1273. _viewContainer = [[UIView alloc] init];
  1274. }
  1275. return _viewContainer;
  1276. }
  1277. - (KSVideoRecordManager *)videoRecordManager {
  1278. if (!_videoRecordManager) {
  1279. MJWeakSelf;
  1280. _videoRecordManager = [[KSVideoRecordManager alloc] initSessionRecordCallback:^(BOOL isSuccess, NSString * _Nullable message) {
  1281. if (isSuccess) {
  1282. [weakSelf showSuccessMessage:message];
  1283. }
  1284. else {
  1285. if (![NSString isEmptyString:message]) {
  1286. [LOADING_MANAGER MBShowAUTOHidingInWindow:message];
  1287. }
  1288. }
  1289. }];
  1290. [_videoRecordManager errorMessageCallback:^(NSDictionary * _Nonnull errorParm) {
  1291. [weakSelf uploadVideoRecordErrorMessage:errorParm];
  1292. }];
  1293. }
  1294. return _videoRecordManager;
  1295. }
  1296. - (void)uploadVideoRecordErrorMessage:(NSDictionary *)errorParm {
  1297. NSMutableDictionary *parm = [NSMutableDictionary dictionaryWithDictionary:errorParm];
  1298. [parm setValue:UserDefault(UIDKey) forKey:@"userId"];
  1299. [parm setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forKey:@"version"];
  1300. NSString *content = [parm mj_JSONString];
  1301. NSMutableDictionary *submitParm = [KSLogManager generateLogMessageWithContent:content type:@"ERROR"];
  1302. NSMutableArray *uploadArray = [NSMutableArray arrayWithObject:submitParm];
  1303. [KSNetworkingManager sysExceptionLogUpdate:KS_POST token:UserDefault(TokenKey) logArray:uploadArray success:^(NSDictionary * _Nonnull dic) {
  1304. if ([dic ks_integerValueForKey:@"code"] == 200) {
  1305. }
  1306. } faliure:^(NSError * _Nonnull error) {
  1307. }];
  1308. }
  1309. - (void)showSuccessMessage:(NSString *)message {
  1310. if (![NSString isEmptyString:message]) {
  1311. [LOADING_MANAGER MBShowAUTOHidingInWindow:message];
  1312. }
  1313. // 成功
  1314. if (self.endRecordParm) {
  1315. [self postMessage:self.endRecordParm];
  1316. self.endRecordParm = nil;
  1317. }
  1318. }
  1319. - (MetronomeType)getBeatTypeFromString:(NSString *)typeString {
  1320. if ([typeString isEqualToString:@"1/4"]) {
  1321. return MetronomeType1V4;
  1322. }
  1323. else if ([typeString isEqualToString:@"2/4"]) {
  1324. return MetronomeType2V4;
  1325. }
  1326. else if ([typeString isEqualToString:@"3/4"]) {
  1327. return MetronomeType3V4;
  1328. }
  1329. else if ([typeString isEqualToString:@"4/4"]) {
  1330. return MetronomeType4V4;
  1331. }
  1332. else if ([typeString isEqualToString:@"3/8"]) {
  1333. return MetronomeType3V8;
  1334. }
  1335. else if ([typeString isEqualToString:@"6/8"]) {
  1336. return MetronomeType6V8;
  1337. }
  1338. else {
  1339. return MetronomeType4V4;
  1340. }
  1341. }
  1342. #pragma mark ------- midi 播放相关
  1343. - (void)configPlayerEngineWithSong:(NSString *)songPath reportTime:(NSInteger)reportTime {
  1344. self.initEngineSuccess = NO;
  1345. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1346. self.playerEngine = [[MidiPlayerEngine alloc] init];
  1347. self.playerEngine.reportTime = reportTime;
  1348. self.playerEngine.delegate = self;
  1349. [self.playerEngine configSoundFilePath:[[NSBundle mainBundle]
  1350. pathForResource:@"synthgms" ofType:@"sf2"]];
  1351. [self.playerEngine loadMIDIFileWithString:songPath];
  1352. });
  1353. }
  1354. - (void)configAudioSession {
  1355. self.audioSessionManager = [[KSAudioSessionManager alloc] init];
  1356. self.audioSessionManager.delegate = self;
  1357. [self.audioSessionManager configAudioSession:AUDIOCONFIG_PLAYANDRECORD];
  1358. }
  1359. #pragma mark ---- PlayerEngineDelegate
  1360. /** 进度更新
  1361. api: 'cloudTimeUpdae',
  1362. content: {
  1363. // 当前曲目id
  1364. songID: 0,
  1365. // 当前位置时间(ms)
  1366. currentTime: 0
  1367. }
  1368. */
  1369. /** 播放结束事件
  1370. api: 'cloudplayed',
  1371. content: {
  1372. // 当前曲目id
  1373. songID: 0,
  1374. }
  1375. */
  1376. - (void)ProgressUpdated:(float)progress currentPlayTime:(MusicTimeStamp)currentPlayTime currentTime:(NSTimeInterval)currentTime {
  1377. if (self.isPlaying == NO) {
  1378. return;
  1379. }
  1380. // 回调
  1381. NSMutableDictionary *sendParm = [NSMutableDictionary dictionary];
  1382. [sendParm setValue:@"cloudTimeUpdae" forKey:@"api"];
  1383. NSMutableDictionary *content = [NSMutableDictionary dictionary];
  1384. [content setValue:self.currentSongId forKey:@"songID"];
  1385. [content setValue:@(currentPlayTime*1000) forKey:@"currentTime"];
  1386. [sendParm setValue:content forKey:@"content"];
  1387. [self postMessage:sendParm];
  1388. }
  1389. - (void)playEnd {
  1390. [self stopPlayAction];
  1391. NSMutableDictionary *sendParm = [NSMutableDictionary dictionary];
  1392. [sendParm setValue:@"cloudplayed" forKey:@"api"];
  1393. NSMutableDictionary *content = [NSMutableDictionary dictionary];
  1394. [content setValue:self.currentSongId forKey:@"songID"];
  1395. [sendParm setValue:content forKey:@"content"];
  1396. [self postMessage:sendParm];
  1397. }
  1398. - (void)initPlayerEngineSuccess:(float)totalTime {
  1399. self.initEngineSuccess = YES;
  1400. if (self.configEngineParm) {
  1401. NSMutableDictionary *sendParm = [NSMutableDictionary dictionaryWithDictionary:self.configEngineParm];
  1402. NSMutableDictionary *content = [NSMutableDictionary dictionaryWithDictionary:[sendParm ks_dictionaryValueForKey:@"content"]];
  1403. [content setValue:@(totalTime*1000) forKey:@"midiDuration"];
  1404. [sendParm setValue:content forKey:@"content"];
  1405. [self postMessage:sendParm];
  1406. self.configEngineParm = nil;
  1407. }
  1408. }
  1409. // 总时长
  1410. - (void)GetMusicTotalTime:(float)time {
  1411. }
  1412. #pragma mark ----- 播放控制
  1413. - (void)playAction {
  1414. if (self.playerEngine) {
  1415. self.isPlaying = YES;
  1416. [self.playerEngine playMIDIFile];
  1417. }
  1418. }
  1419. - (void)stopPlayAction {
  1420. if (self.playerEngine) {
  1421. self.isPlaying = NO;
  1422. if ([self.playerEngine isPlayingFile]) {
  1423. [self.playerEngine stopPlayingMIDIFile];
  1424. }
  1425. }
  1426. }
  1427. #pragma mark ----- 小酷AI loading
  1428. - (AccompanyLoadingView *)loadingView {
  1429. if (!_loadingView) {
  1430. _loadingView = [AccompanyLoadingView shareInstance];
  1431. MJWeakSelf;
  1432. [_loadingView loadingCallback:^{
  1433. [weakSelf backAction];
  1434. }];
  1435. }
  1436. return _loadingView;
  1437. }
  1438. - (void)showCustomLoading {
  1439. if ([self.view.subviews containsObject:self.loadingView]) {
  1440. return;
  1441. }
  1442. [self.view addSubview:self.loadingView];
  1443. [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
  1444. make.left.top.right.bottom.mas_equalTo(self.view);
  1445. }];
  1446. [self.view bringSubviewToFront:self.loadingView];
  1447. [self.loadingView showLoading];
  1448. }
  1449. - (void)removeCustomLoadingView {
  1450. [self.loadingView stopLoading];
  1451. }
  1452. #pragma mark ----- 跟音模块
  1453. - (void)startTuner {
  1454. @try {
  1455. if (self.isTunerRuning == NO) {
  1456. self.isTunerRuning = YES;
  1457. [self.tuner start];
  1458. }
  1459. } @catch (NSException *exception) {
  1460. NSLog(@"----- exception --- %@", exception);
  1461. } @finally {
  1462. }
  1463. }
  1464. - (void)stopTuner {
  1465. if (self.isTunerRuning) {
  1466. self.isTunerRuning = NO;
  1467. [self.tuner stop];
  1468. }
  1469. }
  1470. - (Tuner *)tuner {
  1471. if (!_tuner) {
  1472. _tuner = [[Tuner alloc] initWithThreshold:0 smoothing:0.25];
  1473. _tuner.delegate = self;
  1474. }
  1475. return _tuner;
  1476. }
  1477. - (void)tunerDidUpdate:(Tuner *)tuner output:(TunerOutput *)output {
  1478. if (output.amplitude < 0.01) {
  1479. }
  1480. else {
  1481. // 回调频率
  1482. NSDictionary *parm = @{
  1483. @"api" : @"cloudFollowTime",
  1484. @"content" : @{@"frequency" : [NSNumber numberWithDouble:output.frequency]}
  1485. };
  1486. [self postMessage:parm];
  1487. }
  1488. NSLog(@"-------- %@%zd --- distance :%f frequence : %f" , output.pitch, output.octave, output.distance, output.frequency);
  1489. }
  1490. #pragma mark ---- 保存伴奏
  1491. - (BOOL)saveAccompanyFileWithUrl:(NSURL *)fileUrl accompanyUrl:(NSString *)accompanyUrl {
  1492. NSData *sourceData = [NSData dataWithContentsOfURL:fileUrl];
  1493. NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  1494. NSString *filePath= [cachePath stringByAppendingPathComponent:@"AccompanySong"];
  1495. // 先创建子目录
  1496. NSFileManager *fileManager = [NSFileManager defaultManager];
  1497. if (![fileManager fileExistsAtPath:filePath]) {
  1498. [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
  1499. }else{
  1500. NSLog(@"已创建文件夹");
  1501. }
  1502. NSString *fileName = [accompanyUrl getUrlFileName];
  1503. NSString *tempPath = [filePath stringByAppendingPathComponent:fileName];
  1504. BOOL success = [sourceData writeToFile:tempPath atomically:NO];
  1505. return success;
  1506. }
  1507. - (NSString *)getAccompanyFilePathWithName:(NSString *)fileName {
  1508. NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  1509. NSString *filePath= [cachePath stringByAppendingPathComponent:@"AccompanySong"];
  1510. return [filePath stringByAppendingPathComponent:fileName];;
  1511. }
  1512. - (BOOL)checkSongHasSaveAccompanyWithSongUrl:(NSString *)songUrl {
  1513. BOOL hasSaveFile = NO;
  1514. NSString *fileName = [songUrl getUrlFileName];
  1515. NSString *filePath = [self getAccompanyFilePathWithName:fileName];
  1516. NSFileManager *fileManager = [NSFileManager defaultManager];
  1517. if ([fileManager fileExistsAtPath:filePath]) {
  1518. hasSaveFile = YES;
  1519. }
  1520. return hasSaveFile;
  1521. }
  1522. #pragma mark ------- Audio Engine player
  1523. - (AudioEnginePlayer *)delayCheckPlayer {
  1524. if (!_delayCheckPlayer) {
  1525. _delayCheckPlayer = [[AudioEnginePlayer alloc] init];
  1526. _delayCheckPlayer.delegate = self;
  1527. }
  1528. return _delayCheckPlayer;
  1529. }
  1530. - (AudioEnginePlayer *)musicPlayer {
  1531. if (!_musicPlayer) {
  1532. _musicPlayer = [[AudioEnginePlayer alloc] init];
  1533. _musicPlayer.delegate = self;
  1534. }
  1535. return _musicPlayer;
  1536. }
  1537. #pragma mark ---- Audio Engine player delegate
  1538. - (void)enginePlayerIsReadyPlay:(AudioEnginePlayer *)player {
  1539. if (player == self.delayCheckPlayer) {
  1540. self.checkPlayerReady = YES;
  1541. }
  1542. else if (player == self.musicPlayer) {
  1543. self.musicPlayerReady = YES;
  1544. }
  1545. // 如果都准备好
  1546. if (self.musicPlayerReady && self.checkPlayerReady) {
  1547. [self sendPlayerReadyMsg];
  1548. }
  1549. }
  1550. // 播放进度
  1551. - (void)updatePlayProgress:(NSInteger)playTime andTotalTime:(NSInteger)totalTime andProgress:(CGFloat)progress currentInterval:(NSTimeInterval)currentInterval inPlayer:(AudioEnginePlayer *)player {
  1552. if (player == self.delayCheckPlayer) {
  1553. if (playTime >= 300 && self.recordStartTime > 0 && self.hasRecordMusicOffset == NO) {
  1554. self.hasRecordMusicOffset = YES;
  1555. NSLog(@" --- check player start play time %f", currentInterval - playTime);
  1556. self.playerStartTime = currentInterval - playTime;
  1557. self.offsetTime = self.playerStartTime - self.recordStartTime;
  1558. NSLog(@"--------- check player offset time -- %zd", self.offsetTime);
  1559. }
  1560. }
  1561. // 如果未记录延迟
  1562. if (playTime >= (self.musicStartTime + 300) && player == self.musicPlayer && self.hasRecordMusicOffset == NO) {
  1563. if (self.recordStartTime > 0) {
  1564. self.hasRecordMusicOffset = YES;
  1565. NSInteger newPlayTime = (playTime - self.musicStartTime) / player.rate; // 选段
  1566. NSLog(@" --- music player start play time %f", currentInterval - newPlayTime);
  1567. self.playerStartTime = currentInterval - newPlayTime;
  1568. self.offsetTime = self.playerStartTime - self.recordStartTime;
  1569. NSLog(@"--------- music play offset time -- %zd", self.offsetTime);
  1570. [self sendOffsetTimeToService];
  1571. }
  1572. NSLog(@"------- record start time %f", self.recordStartTime);
  1573. }
  1574. // 回调进度
  1575. if (player == self.musicPlayer) {
  1576. // NSLog(@"------ music play progress - %f", progress);
  1577. // 回调进度
  1578. NSDictionary *parm = @{
  1579. @"api" : @"playProgress",
  1580. @"content" : @{@"currentTime" : [NSNumber numberWithInteger:playTime],
  1581. @"totalDuration" : [NSNumber numberWithInteger:totalTime],
  1582. }
  1583. };
  1584. // NSLog(@" -----music play progress %@---- ", parm);
  1585. [self postMessage:parm];
  1586. }
  1587. }
  1588. // 错误
  1589. - (void)enginePlayerDidError:(AudioEnginePlayer *)player error:(NSError *)error {
  1590. [self stopRecordService];
  1591. [self stopMp3Player]; // 停止播放
  1592. // 播放出现问题
  1593. NSDictionary *postParm = @{@"api" : @"cancelEvaluating",
  1594. @"content" : @{@"reson":@"播放已停止"}
  1595. };
  1596. [self postMessage:postParm];
  1597. if (error) {
  1598. NSLog(@"-- error desc - %@", error.description);
  1599. NSMutableDictionary *parm = [NSMutableDictionary dictionary];
  1600. [parm setValue:UserDefault(UIDKey) forKey:@"userId"];
  1601. [parm setValue:@"KSCloudWebViewController_MP3Player" forKey:@"Location"];
  1602. [parm setValue:@(error.code) forKey:@"errorCode"];
  1603. [parm setValue:error.description forKey:@"errorDesc"];
  1604. [parm setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forKey:@"version"];
  1605. NSString *content = [parm mj_JSONString];
  1606. NSMutableDictionary *submitParm = [KSLogManager generateLogMessageWithContent:content type:@"ERROR"];
  1607. NSMutableArray *uploadArray = [NSMutableArray arrayWithObject:submitParm];
  1608. [KSNetworkingManager sysExceptionLogUpdate:KS_POST token:UserDefault(TokenKey) logArray:uploadArray success:^(NSDictionary * _Nonnull dic) {
  1609. if ([dic ks_integerValueForKey:@"code"] == 200) {
  1610. }
  1611. } faliure:^(NSError * _Nonnull error) {
  1612. }];
  1613. }
  1614. }
  1615. - (void)sendPlayerReadyMsg {
  1616. if (self.playerParm) {
  1617. [self postMessage:self.playerParm];
  1618. }
  1619. }
  1620. - (NSMutableArray *)delayArray {
  1621. if (!_delayArray) {
  1622. _delayArray = [NSMutableArray array];
  1623. }
  1624. return _delayArray;
  1625. }
  1626. /*
  1627. #pragma mark - Navigation
  1628. // In a storyboard-based application, you will often want to do a little preparation before navigation
  1629. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  1630. // Get the new view controller using [segue destinationViewController].
  1631. // Pass the selected object to the new view controller.
  1632. }
  1633. */
  1634. @end