From 31c7d5bf5e813487edf573b9bf1324fc24384aab Mon Sep 17 00:00:00 2001 From: sunlian Date: Thu, 19 Sep 2024 09:24:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=AD=8C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sunlian --- scenario/MusicPlayerOnline/README_zh.md | 3 +- .../main/ets/constants/ServerConstants.ets | 9 +- .../src/main/ets/manager/PlayerManager.ets | 86 ++++++++++++++++- .../entry/src/main/ets/model/AudioItem.ets | 2 +- .../entry/src/main/ets/pages/Index.ets | 47 ++++++++- .../entry/src/main/ets/view/PlayerBar.ets | 2 +- .../entry/src/main/ets/view/PlayerDetail.ets | 87 ++++++++++++++++- .../src/main/ets/view/addToListDialog.ets | 95 +++++++++++++++++++ 8 files changed, 315 insertions(+), 16 deletions(-) create mode 100644 scenario/MusicPlayerOnline/entry/src/main/ets/view/addToListDialog.ets diff --git a/scenario/MusicPlayerOnline/README_zh.md b/scenario/MusicPlayerOnline/README_zh.md index f36c9fcd..793f755d 100644 --- a/scenario/MusicPlayerOnline/README_zh.md +++ b/scenario/MusicPlayerOnline/README_zh.md @@ -99,7 +99,8 @@ 17.2. 其他设置:仅wifi、边听边存、保持登录 18. 分布式播放 19. 性能优化:加载大量歌单和歌曲的列表(1000以上容易appfreeze),应用优化+服务器控制数据量 - + 20. 一多适配:平板、pc + 21. 异常处理:检测网络状态、播放器异常等 #### 未计划功能: 1. 社交(用户信息,好友,分享,评论) diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets index b3973869..233bfb9f 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets @@ -61,6 +61,10 @@ export default class ServerConstants { * set favourite */ static readonly SET_FAVOURITE = this.SERVER_HOST + 'set_favourite/'; + /** + * add song to playlist + */ + static readonly ADD_TO_PLAYLIST = this.SERVER_HOST + 'add_to_playlist/'; /** * Login URL */ @@ -89,6 +93,9 @@ export default class ServerConstants { static readonly UPDATE_TIME_EVENT_ID_DETAIL = '4'; static readonly UPDATE_STATE_EVENT_ID_DETAIL = '5'; static readonly UPDATE_FAVOR_EVENT_ID_DETAIL = '6'; + static readonly UPDATE_USER_LIST = '7'; + static readonly UPDATE_USER_LIST_CUSTOM = '1'; + static readonly UPDATE_USER_LIST_RECOMMEND = '2'; /** * PLAYER LIST */ @@ -104,7 +111,7 @@ export default class ServerConstants { static readonly LOGIN_INFO_ING = '···'; static readonly LOGIN_INFO_FAILED = '登录失败'; static readonly REGISTER_INFO_FAILED = '注册失败'; - static readonly REGISTER_INFO_SUCCESS = '注册成功'; + static readonly REGISTER_INFO_SUCCESS = '注册成功,自动登录...'; static readonly LOGIN_INFO_DONE = ' 你好!'; static readonly SET_FAVOURITE_ALREADY = "重复收藏"; static readonly SET_FAVOURITE_SUCCESS = "收藏成功"; diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets index 2b4118f9..6ff527e4 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets @@ -24,6 +24,7 @@ import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager' import wantAgent, { WantAgent } from '@ohos.app.ability.wantAgent'; import common from '@ohos.app.ability.common'; import http from '@ohos.net.http' +import prompt from '@ohos.promptAction'; export default class PlayerManager { @@ -369,9 +370,17 @@ export default class PlayerManager { console.info('HttpResponse type:' + JSON.stringify(data.resultType)); console.info('HttpResponse header:' + JSON.stringify(data.header)); console.info('HttpResponse cookies:' + data.cookies); - + let eventData: emitter.EventData = { + data: { + "message": ServerConstants.UPDATE_USER_LIST_RECOMMEND, + } + }; + emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); } else { console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) } }); } catch (err) { @@ -381,6 +390,7 @@ export default class PlayerManager { set_favourite() { let loginCookie: string | undefined = AppStorage.get('loginCookie'); + let msg: string = '' try { let is_favor: string = this.item.isFavor ? '0' : '1'; console.info('HttpResponse start:' + ServerConstants.SET_FAVOURITE + this.item.id + '/' + is_favor); @@ -413,24 +423,90 @@ export default class PlayerManager { this.item.isFavor = false; break; } + emitter.emit(ServerConstants.UPDATE_FAVOR_EVENT_ID_DETAIL, this.emitterOptions); + prompt.showToast({ + message: jsonObject["message"] + }) let eventData: emitter.EventData = { data: { - "message": jsonObject["message"], + "message": ServerConstants.UPDATE_USER_LIST_RECOMMEND, } }; - console.info('HttpResponse msg:' + jsonObject["message"]); - //emitter.emit(ServerConstants.UPDATE_FAVOR_EVENT_ID, this.emitterOptions, eventData); - emitter.emit(ServerConstants.UPDATE_FAVOR_EVENT_ID_DETAIL, this.emitterOptions, eventData); + emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); } catch (error) { console.error("JSON :", error.message); + msg = JSON.stringify(error); + } + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + msg = JSON.stringify(err); + } + if (msg !== '') { + prompt.showToast({ + message: msg + }) + } + } + + add_to_playlist(song_id: string, playlist_name: string) { + let loginCookie: string | undefined = AppStorage.get('loginCookie'); + let msg: string = '' + try { + console.info('HttpResponse start:' + ServerConstants.ADD_TO_PLAYLIST + song_id + '/' + playlist_name); + this.httpRequest.request(ServerConstants.ADD_TO_PLAYLIST + song_id + '/' + playlist_name, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': loginCookie + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse Result:' + data.result); + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + console.info('HttpResponse cookies:' + data.cookies); + try { + const jsonObject: object = JSON.parse(data.result as string); + console.info('HttpResponse msg:' + jsonObject["message"]); + let eventData: emitter.EventData = { + data: { + "message": ServerConstants.UPDATE_USER_LIST_CUSTOM, + } + }; + emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); + prompt.showToast({ + message: jsonObject["message"] + }) + } catch (error) { + console.error("JSON :", error.message); + msg = JSON.stringify(error); } } else { console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) } }); } catch (err) { console.info('HttpRequest error:' + JSON.stringify(err)); + msg = JSON.stringify(err); + } + console.info('HttpRequest msg:' + msg); + if (msg !== '') { + prompt.showToast({ + message: msg + }) } } diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/model/AudioItem.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/model/AudioItem.ets index 86c0a50d..e6ad86f7 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/model/AudioItem.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/model/AudioItem.ets @@ -20,7 +20,7 @@ export default class AudioItem { title: string = ''; artist: string = ''; - id: string = '0' + id: string = '' isPlaying: boolean = false; isFavor: boolean = false; diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets index 42b6d722..33871e4f 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets @@ -114,12 +114,26 @@ struct Index { this.getRecommendListFromServer(); } }); + emitter.on(ServerConstants.UPDATE_USER_LIST, (eventData: emitter.EventData) => { + if (eventData !== undefined && eventData.data !== undefined) { + if(eventData.data.message === ServerConstants.UPDATE_USER_LIST_CUSTOM){ + this.getCustomListFromServer(); + } + if(eventData.data.message === ServerConstants.UPDATE_USER_LIST_RECOMMEND){ + this.getRecommendListFromServer(); + } + } + }); } aboutToDisappear(): void { emitter.off(ServerConstants.UPDATE_TIME_EVENT_ID); emitter.off(ServerConstants.UPDATE_STATE_EVENT_ID); - this.dialogController = null; + emitter.off(ServerConstants.UPDATE_USER_LIST); + if (this.dialogController !== null) { + this.dialogController.close(); + this.dialogController = null; + } } build() { @@ -296,7 +310,7 @@ struct Index { GridItem() { PlayListItem({ item }) } - .id('my_list_item') + .id('search_list_item') .transition({ type: TransitionType.All, opacity: 0 }) }) } @@ -405,7 +419,12 @@ struct Index { }) if (this.isShowPlayerDetail) { - PlayerDetail({ isShowPlayList: $isShowPlayList, isShowPlayerDetail: $isShowPlayerDetail }) + PlayerDetail({ + isShowPlayList: $isShowPlayList, + isShowPlayerDetail: $isShowPlayerDetail, + customPlayLists: this.customPlayLists, + loginInfo: this.loginInfo, + }) .width('100%') .height('100%') .transition(TransitionEffect.OPACITY @@ -531,6 +550,9 @@ struct Index { this.getCustomListFromServer(); } else { console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) this.loginFailed(); } }); @@ -541,6 +563,9 @@ struct Index { } } else { console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) this.loginFailed(); } }); @@ -594,6 +619,10 @@ struct Index { prompt.showToast({ message: ServerConstants.REGISTER_INFO_SUCCESS }) + this.login(userName.trim(), pwd.trim()); + if (this.dialogController) { + this.dialogController.close(); + } } else { console.info('HttpResponse error:' + JSON.stringify(err)); prompt.showToast({ @@ -672,10 +701,16 @@ struct Index { console.info('HttpResponse cookies:' + data.cookies); } else { console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) } }); } catch (err) { console.info('HttpRequest error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) } } @@ -730,6 +765,9 @@ struct Index { } } else { console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) } }); } catch (err) { @@ -789,6 +827,9 @@ struct Index { console.info('HttpResponse cookies:' + data.cookies); } else { console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) this.searchSongTitle = ServerConstants.SEARCH_DEFAULT; } }); diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets index fa2ddf8a..2b9944ba 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerBar.ets @@ -34,7 +34,7 @@ export default struct PlayerBar { @Link isShowPlayList: boolean; @Link isShowPlayerDetail: boolean; private PlayerManager: PlayerManager = AppStorage.get('PlayerManager') as PlayerManager; - private spaces: string = ' ' + private spaces: string = ' ' aboutToAppear() { Logger.info(this.tag, 'aboutToAppear'); diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets index 14df9b55..b50ce189 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets @@ -22,6 +22,9 @@ import ServerConstants from '../constants/ServerConstants'; import UIConstants from '../constants/UIConstants'; import AudioItem from '../model/AudioItem'; import { PLAY_MODE } from '../constants/PlayMode'; +import { addToListDialog } from './addToListDialog'; +import PlayListData from '../model/PlayListData'; +import prompt from '@ohos.promptAction'; /** * Setting tab content @@ -44,8 +47,48 @@ export default struct PlayerDetail { @State noItem: boolean = false; @Link isShowPlayList: boolean; @Link isShowPlayerDetail: boolean; + @Prop customPlayLists: PlayListData[]; + @Prop loginInfo: string; + @State listOptions: Array = []; + @State inputListName: string = ''; private scroller: Scroller = new Scroller() private tabsController: TabsController = new TabsController(); + private isDialogOpened: boolean = false; + private songIdToAdd: string = ''; + dialogController: CustomDialogController | null = new CustomDialogController({ + builder: addToListDialog({ + cancel: () => { + if (this.dialogController !== null) { + this.dialogController.close(); + this.isDialogOpened = false; + } + }, + confirm: () => { + if (this.inputListName.trim()) { + if (this.songIdToAdd !== '') { + this.PlayerManager.add_to_playlist(this.songIdToAdd, this.inputListName); + this.songIdToAdd = ''; + if (this.dialogController !== null) { + this.dialogController.close(); + this.isDialogOpened = false; + } + } + } else { + prompt.showToast({ + message: '歌单名不能为空' + }); + } + }, + inputListName: $inputListName, + listOptions: $listOptions, + }), + autoCancel: true, + alignment: DialogAlignment.Bottom, + offset: { dx: 0, dy: -20 }, + gridCount: 4, + customStyle: false, + cornerRadius: 10, + }) @Builder TabBuilder(title: Resource, index: number, selectedImg: Resource, normalImg: Resource) { @@ -73,11 +116,11 @@ export default struct PlayerDetail { } aboutToAppear() { - emitter.on(ServerConstants.UPDATE_FAVOR_EVENT_ID_DETAIL, (eventData: emitter.EventData) => { - if (eventData !== undefined && eventData.data !== undefined) { + emitter.on(ServerConstants.UPDATE_FAVOR_EVENT_ID_DETAIL, () => { + animateTo({ duration: 2000 }, () => { this.item = this.PlayerManager.getItem(); - this.noItem = false; - } + }) + this.noItem = false; }); emitter.on(ServerConstants.UPDATE_TIME_EVENT_ID_DETAIL, (eventData: emitter.EventData) => { if (eventData !== undefined && eventData.data !== undefined) { @@ -132,6 +175,21 @@ export default struct PlayerDetail { emitter.off(ServerConstants.UPDATE_TIME_EVENT_ID_DETAIL); emitter.off(ServerConstants.UPDATE_STATE_EVENT_ID_DETAIL); emitter.off(ServerConstants.UPDATE_FAVOR_EVENT_ID_DETAIL); + if (this.dialogController !== null) { + this.dialogController.close(); + this.isDialogOpened = false; + this.dialogController = null; + } + } + + onBackPress() { + if (this.isDialogOpened && this.dialogController !== null) { + this.dialogController.close(); + this.isDialogOpened = false; + return true; + } else { + return false; + } } build() { @@ -179,6 +237,27 @@ export default struct PlayerDetail { .width('36vp') .clickEffect(UIConstants.CLICK_EFFECT) .onClick(() => { + if (this.loginInfo === ServerConstants.LOGIN_INFO_NO) { + prompt.showToast({ + message: '请先登录!' + }) + return; + } + if (this.item.id === '') { + prompt.showToast({ + message: '请先播放一首歌曲!' + }); + return; + } + if (this.dialogController != null) { + this.listOptions = []; + for (let item of this.customPlayLists) { + this.listOptions.push({ value: item.title }); + } + this.dialogController.open(); + this.songIdToAdd = this.item.id; + this.isDialogOpened = true; + } }) } .width('90%') diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/addToListDialog.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/addToListDialog.ets new file mode 100644 index 00000000..28648afc --- /dev/null +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/addToListDialog.ets @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { Font } from '@ohos.arkui.UIContext'; + +@CustomDialog +export struct addToListDialog { + @Link inputListName: string; + @State title: string = '将歌曲加入[新建歌单]或[已有歌单]'; + @State text: string = "选择已有歌单"; + @State index: number = 0; + @State space: number = 8; + @State arrowPosition: ArrowPosition = ArrowPosition.END; + @Link listOptions: Array; + controller?: CustomDialogController; + cancel: () => void = () => { + } + confirm: () => void = () => { + } + + build() { + Column() { + Text(this.title) + .fontSize(20) + .margin({ top: 10, bottom: 10 }) + .fontWeight(FontWeight.Lighter) + Row() { + TextInput({ placeholder: '输入新建歌单名称', text: this.inputListName }) + .height(42) + .width('40%') + .margin(8) + .onChange((value: string) => { + this.inputListName = value + }) + .id('list_name') + if (this.listOptions.length > 0) { + Select(this.listOptions) + .selected(this.index) + .value(this.text) + .font({ size: 16, weight: 500 }) + .selectedOptionFont({ size: 16, weight: 400 }) + .optionFont({ size: 16, weight: 400 }) + .space(this.space) + .margin(8) + .arrowPosition(this.arrowPosition) + .menuAlign(MenuAlignType.START, { dx: 0, dy: 0 }) + .optionWidth(200) + .optionHeight(300) + .onSelect((index: number, text?: string | undefined) => { + this.index = index; + if (text) { + this.text = text; + this.inputListName = text; + } + }) + .width('40%') + } + } + + Flex({ justifyContent: FlexAlign.SpaceAround }) { + Button('取消') + .backgroundColor(Color.White) + .fontColor(Color.Black) + .fontWeight(FontWeight.Lighter) + .fontSize(18) + .onClick(() => { + if (this.controller != undefined) { + this.controller.close() + this.cancel() + } + }) + Button('确定') + .backgroundColor(Color.White) + .fontWeight(FontWeight.Lighter) + .fontSize(18) + .fontColor(Color.Red) + .id('login_button') + .onClick(() => { + this.confirm() + }) + }.margin({ bottom: 12, top: 12 }) + }.borderRadius(8) + } +} \ No newline at end of file -- Gitee From bfd77801614c69eb8338512ff354ce7ec1054085 Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 24 Sep 2024 09:59:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=84=E7=90=86=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sunlian --- scenario/MusicPlayerOnline/README_zh.md | 5 +- .../main/ets/constants/ServerConstants.ets | 18 ++- .../src/main/ets/manager/PlayerManager.ets | 27 +++- .../entry/src/main/ets/model/AudioItem.ets | 1 + .../entry/src/main/ets/pages/Index.ets | 150 ++++++++++++++++-- .../entry/src/main/ets/view/PlayerDetail.ets | 4 + scenario/MyMusic/README_zh.md | 2 +- 7 files changed, 176 insertions(+), 31 deletions(-) diff --git a/scenario/MusicPlayerOnline/README_zh.md b/scenario/MusicPlayerOnline/README_zh.md index 793f755d..dbea8b08 100644 --- a/scenario/MusicPlayerOnline/README_zh.md +++ b/scenario/MusicPlayerOnline/README_zh.md @@ -78,8 +78,8 @@ 3. 搜索歌曲展示页点击播放 (已完成) 4. 歌曲详情页收藏/取消收藏功能 (已完成) 5. 歌单 - 5.1 从歌曲详情页加入创建并加入歌单 - 5.2 从歌曲详情页加入已有歌单 + 5.1 从歌曲详情页加入创建并加入歌单 (已完成) + 5.2 从歌曲详情页加入已有歌单 (已完成) 5.3 删除歌单 5.4 从歌单删除歌曲 5.5 歌单改名 @@ -107,6 +107,7 @@ 2. 数据分析(统计播放历史、排行榜、歌曲偏好等) 3. 修改密码,忘记密码,手机号注册等 4. 安全(传输加密) + 5. 推送:服务器信息变更通知 #### 已开发用例 详见用例文档:[ohosTest.md](ohosTest.md) diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets index 233bfb9f..a60181d1 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/constants/ServerConstants.ets @@ -34,9 +34,17 @@ export default class ServerConstants { */ static readonly SEARCH_SONG_URL = this.SERVER_HOST + 'search_song/'; /** - * custom list URL + * recommend list URL */ static readonly RECOMMEND_LIST_URL = this.SERVER_HOST + 'get_recommend_playlists/'; + /** + * recent list URL + */ + static readonly RECENT_LIST_URL = this.SERVER_HOST + 'get_recent_playlists/'; + /** + * favor list URL + */ + static readonly FAVOR_LIST_URL = this.SERVER_HOST + 'get_favor_playlists/'; /** * English songs URL */ @@ -89,13 +97,13 @@ export default class ServerConstants { */ static readonly UPDATE_TIME_EVENT_ID = '1'; static readonly UPDATE_STATE_EVENT_ID = '2'; - static readonly UPDATE_FAVOR_EVENT_ID = '3'; static readonly UPDATE_TIME_EVENT_ID_DETAIL = '4'; static readonly UPDATE_STATE_EVENT_ID_DETAIL = '5'; static readonly UPDATE_FAVOR_EVENT_ID_DETAIL = '6'; - static readonly UPDATE_USER_LIST = '7'; - static readonly UPDATE_USER_LIST_CUSTOM = '1'; - static readonly UPDATE_USER_LIST_RECOMMEND = '2'; + static readonly UPDATE_USER_LIST = '3'; + static readonly UPDATE_USER_LIST_CUSTOM = '31'; + static readonly UPDATE_USER_LIST_RECENT = '32'; + static readonly UPDATE_USER_LIST_FAVOR = '33'; /** * PLAYER LIST */ diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets index 6ff527e4..79f3c75c 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/manager/PlayerManager.ets @@ -128,6 +128,14 @@ export default class PlayerManager { break; default: console.info('PlayerManager state unknown called.'); + if (this.avPlayer !== undefined) { + try { + this.avPlayer.release(); + } catch (err) { + console.info('PlayerManager state unknown called.' + JSON.stringify(err)); + } + this.avPlayer = undefined; + } break; } }) @@ -160,10 +168,10 @@ export default class PlayerManager { * 播放 */ resume(): void { - if (this.state === ServerConstants.PLAYER_STATE_PAUSED) { - if (this.avPlayer !== undefined) { - this.avPlayer.play(); - } + if (this.state === ServerConstants.PLAYER_STATE_PAUSED && this.avPlayer !== undefined) { + this.avPlayer.play(); + } else { + this.play(this.item); } } @@ -370,9 +378,16 @@ export default class PlayerManager { console.info('HttpResponse type:' + JSON.stringify(data.resultType)); console.info('HttpResponse header:' + JSON.stringify(data.header)); console.info('HttpResponse cookies:' + data.cookies); + try { + const jsonObject: object = JSON.parse(data.result as string); + this.item.quality = jsonObject['message']; + emitter.emit(ServerConstants.UPDATE_FAVOR_EVENT_ID_DETAIL, this.emitterOptions); + } catch (err) { + console.info('HttpResponse error:' + JSON.stringify(err)); + } let eventData: emitter.EventData = { data: { - "message": ServerConstants.UPDATE_USER_LIST_RECOMMEND, + "message": ServerConstants.UPDATE_USER_LIST_RECENT, } }; emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); @@ -429,7 +444,7 @@ export default class PlayerManager { }) let eventData: emitter.EventData = { data: { - "message": ServerConstants.UPDATE_USER_LIST_RECOMMEND, + "message": ServerConstants.UPDATE_USER_LIST_FAVOR, } }; emitter.emit(ServerConstants.UPDATE_USER_LIST, this.emitterOptions, eventData); diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/model/AudioItem.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/model/AudioItem.ets index e6ad86f7..b53eaef2 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/model/AudioItem.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/model/AudioItem.ets @@ -23,6 +23,7 @@ export default class AudioItem { id: string = '' isPlaying: boolean = false; isFavor: boolean = false; + quality = '.mp3'; constructor(title: string, artist: string, id:string) { this.title = title; diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets index 33871e4f..fa80dcd1 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/pages/Index.ets @@ -106,21 +106,20 @@ struct Index { } else { this.customPlayLists = []; } - emitter.on(ServerConstants.UPDATE_FAVOR_EVENT_ID, (eventData: emitter.EventData) => { - if (eventData !== undefined && eventData.data !== undefined) { - prompt.showToast({ - message: eventData.data.message - }) - this.getRecommendListFromServer(); - } - }); emitter.on(ServerConstants.UPDATE_USER_LIST, (eventData: emitter.EventData) => { if (eventData !== undefined && eventData.data !== undefined) { - if(eventData.data.message === ServerConstants.UPDATE_USER_LIST_CUSTOM){ - this.getCustomListFromServer(); - } - if(eventData.data.message === ServerConstants.UPDATE_USER_LIST_RECOMMEND){ - this.getRecommendListFromServer(); + switch (eventData.data.message) { + case ServerConstants.UPDATE_USER_LIST_CUSTOM: + this.getCustomListFromServer(); + break; + case ServerConstants.UPDATE_USER_LIST_RECENT: + this.getRecentListFromServer(); + break; + case ServerConstants.UPDATE_USER_LIST_FAVOR: + this.getFavorListFromServer(); + break; + default: + break; } } }); @@ -661,7 +660,6 @@ struct Index { getRecommendListFromServer() { animateTo({ duration: 2000 }, () => { this.recommendPlayLists = []; - this.PlayerManager.clearFavourList(); }) try { this.httpRequest.request(ServerConstants.RECOMMEND_LIST_URL, @@ -687,9 +685,125 @@ struct Index { this.recommendPlayLists.push(new PlayListData(key, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, aPlayingList, '')); }) - if (key === ServerConstants.LIST_SONG_FAVOURITE) { - this.PlayerManager.setFavourList(aPlayingList); - } + }); + } + catch (error) { + console.error("JSON :", error.message); + } + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + console.info('HttpResponse cookies:' + data.cookies); + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + } + + getFavorListFromServer() { + try { + this.httpRequest.request(ServerConstants.FAVOR_LIST_URL, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': AppStorage.get('loginCookie') + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse getRecentListFromServer Result:' + data.result); + try { + let existingIndex = this.customPlayLists.findIndex(item => item.title === ServerConstants.LIST_SONG_FAVOURITE); + let aPlayingList: AudioItem[] = Array(); + const jsonObject: object = JSON.parse(data.result as string); + if (jsonObject[ServerConstants.LIST_SONG_FAVOURITE]) { + Object.keys(jsonObject[ServerConstants.LIST_SONG_FAVOURITE]).forEach((key) => { + aPlayingList.push(new AudioItem(jsonObject[ServerConstants.LIST_SONG_FAVOURITE][key].name, jsonObject[ServerConstants.LIST_SONG_FAVOURITE][key].singer, jsonObject[ServerConstants.LIST_SONG_FAVOURITE][key].id)); + }); + animateTo( + { duration: 1000 }, + () => { + if (aPlayingList.length !== 0) { + let favorItem = new PlayListData(ServerConstants.LIST_SONG_FAVOURITE, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, + aPlayingList, ''); + if (existingIndex !== -1) { + this.customPlayLists[existingIndex] = favorItem; + } else { + this.customPlayLists.unshift(favorItem); + } + } else { + if (existingIndex !== -1) { + this.customPlayLists.splice(existingIndex, 1); + } + } + }) + } else { + this.customPlayLists.splice(existingIndex, 1); + } + this.PlayerManager.setFavourList(aPlayingList); + } + catch (error) { + console.error("JSON :", error.message); + } + console.info('HttpResponse code:' + data.responseCode); + console.info('HttpResponse type:' + JSON.stringify(data.resultType)); + console.info('HttpResponse header:' + JSON.stringify(data.header)); + console.info('HttpResponse cookies:' + data.cookies); + } else { + console.info('HttpResponse error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + }); + } catch (err) { + console.info('HttpRequest error:' + JSON.stringify(err)); + prompt.showToast({ + message: JSON.stringify(err) + }) + } + } + + getRecentListFromServer() { + try { + this.httpRequest.request(ServerConstants.RECENT_LIST_URL, + { + method: http.RequestMethod.GET, + header: { + 'Cookie': AppStorage.get('loginCookie') + } + }, + (err: Error, data: http.HttpResponse) => { + if (!err) { + console.info('HttpResponse getRecentListFromServer Result:' + data.result); + try { + const jsonObject: object = JSON.parse(data.result as string); + Object.keys(jsonObject).forEach((key) => { + let aPlayingList: AudioItem[] = Array(); + Object.keys(jsonObject[key]).forEach((key2) => { + aPlayingList.push(new AudioItem(jsonObject[key][key2].name, jsonObject[key][key2].singer, jsonObject[key][key2].id)); + }); + animateTo( + { duration: 1000 }, + () => { + let recentItem = new PlayListData(key, $r('app.media.icon'), aPlayingList.length + ServerConstants.LIST_SONG_COUNT, + aPlayingList, ''); + let existingIndex = this.customPlayLists.findIndex(item => item.title === recentItem.title); + if (existingIndex !== -1) { + this.customPlayLists[existingIndex] = recentItem; + } else { + this.customPlayLists.unshift(recentItem); + } + }) }); } catch (error) { @@ -759,6 +873,8 @@ struct Index { aPlayingList, '')); }) }); + this.getRecentListFromServer(); + this.getFavorListFromServer(); } catch (error) { console.error("JSON :", error.message); diff --git a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets index b50ce189..7fde4661 100644 --- a/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets +++ b/scenario/MusicPlayerOnline/entry/src/main/ets/view/PlayerDetail.ets @@ -399,6 +399,10 @@ export default struct PlayerDetail { Row() { Text(CommonUtils.formatTime(this.currentTime / 1000)).fontSize('12fp') .id('detail_time_current') + if (this.item.quality === '.flac'){ + Text('无损') + .id('high_quality') + } Text(CommonUtils.formatTime(this.durationTime / 1000)).fontSize('12fp') .id('detail_time_duration') }.width('80%') diff --git a/scenario/MyMusic/README_zh.md b/scenario/MyMusic/README_zh.md index b6f62f11..61f2da37 100644 --- a/scenario/MyMusic/README_zh.md +++ b/scenario/MyMusic/README_zh.md @@ -94,7 +94,7 @@ ``` git init git config core.sparsecheckout true -echo scenario/arkui/MyMusic/ > .git/info/sparse-checkout +echo scenario/MyMusic/ > .git/info/sparse-checkout git remote add origin https://gitee.com/openharmony-sig/ostest_integration_test.git git pull origin master ``` \ No newline at end of file -- Gitee