解决手机APP批量删除本地音乐时,当前正在播放的音乐UI状态被改变问题
authorluozheng <1732036298@qq.com>
Fri, 6 Dec 2024 02:38:39 +0000 (10:38 +0800)
committerluozheng <1732036298@qq.com>
Fri, 6 Dec 2024 02:38:39 +0000 (10:38 +0800)
code/sdk/application/wearable/fd230app/nativeui/player/include/PlayerListDelSongView.h
code/sdk/application/wearable/fd230app/nativeui/player/include/PlayersModel.h
code/sdk/application/wearable/fd230app/nativeui/player/src/PlayerListDelSongView.cpp
code/sdk/application/wearable/fd230app/nativeui/player/src/PlayersCaseListAdapter.cpp
code/sdk/application/wearable/fd230app/nativeui/player/src/PlayersModel.cpp

index 9e8c3c9..f98e1b7 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-08-05 15:20:03
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-12-05 21:03:56
+ * @LastEditTime: 2024-12-05 21:50:19
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/player/include/PlayerListDelSongView.h
  */
 
@@ -44,7 +44,7 @@ namespace OHOS
         virtual ~PlayerListDelSongView();
         static PlayerListDelSongView *GetInstance(void);
         UIViewGroup *CreatePlayerListDelSongView();
-        void SetTheDelSongLabelStyle(const char *songName,int index);
+        void SetTheDelSongLabelStyle(const char *songName);
         void DeleteSongNameFile(void);
         UIButton *GetEnsureDeleteButton(void);
         UIButton *GetCancelDeleteButton(void);
@@ -65,7 +65,6 @@ namespace OHOS
         UIButton *cancelDeleteButton{nullptr};
 
         std::string delSongName;
-        int deleteSongIndex{0};
         UIView::OnDragListener *dragListener{nullptr};
         UIView::OnClickListener *buttonClickListener{nullptr};
 
index 3ece4e2..9710ac9 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-08-05 15:20:03
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-11-28 11:04:43
+ * @LastEditTime: 2024-12-05 21:56:01
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/player/include/PlayersModel.h
  */
 
@@ -114,12 +114,15 @@ public:
     bool get_a2dp_sink_playing_state(void);
     void HandPausePhoneMusic(void);
     void HandPlayPhoneMusic(void);
+    std::string getcurrentplayingmusicname(void);
+    void setcurrentplayingmusicname(std::string name);
     // /* -------------------------------------------------------------------------- */
     std::unordered_map<uint16_t, std::pair<std::string,std::string>> &getIdToMusicNameMap(void);
     std::string getRealMusicNameById(int id);
     std::string getShowMusicNameById(int id);
     uint16_t getIdFromMusicName(const std::string &musicName);
     std::string getHexMusicNameFromMusicName(const std::string &musicName);
+    std::string getMusicNameFromHexMusicName(const std::string &musicName);
     int getIdToMusicNameMapNumber(void);
     void showIdToMusicNameMapData(void);
     int getPreShowView(void);
@@ -153,6 +156,7 @@ private:
     void RandomPlayout(void);
     void NextSong(int16_t index);
     int16_t currentIndex{0}; //当前在播哪一首歌的索引
+    std::string currentplayingmusicname; //当前在播哪一首歌的名称
     int16_t playerModel{0};
     char *currentSongName{nullptr};
     bool isStopAndReset{false};
index 3750833..b040741 100644 (file)
@@ -85,10 +85,9 @@ namespace OHOS
 
     /// @brief 设置歌曲名标签样式
     /// @param songName 歌曲名
-    void PlayerListDelSongView::SetTheDelSongLabelStyle(const char *songName,int index)
+    void PlayerListDelSongView::SetTheDelSongLabelStyle(const char *songName)
     {
         delSongName = songName;
-        deleteSongIndex = index;
         if (songNameLabel != nullptr)
         {
             songNameLabel->SetText(PlayersModel::GetInstance()->getHexMusicNameFromMusicName(songName).c_str());
@@ -105,19 +104,6 @@ namespace OHOS
         if (uapi_delete_files(path.c_str()))
         {
             WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Deleted file: %s successed!", path.c_str());
-            int index = PlayersModel::GetInstance()->GetCurrentIndex();
-            if(deleteSongIndex < index)
-            {
-                if(index - 1 < 0)
-                {
-                    index = 0;
-                }
-                else
-                {
-                    index--;
-                }
-                PlayersModel::GetInstance()->SetCurrentIndex(index);
-            }
         }
         else
         {
index 9fc2d12..18f5e2b 100644 (file)
@@ -56,7 +56,7 @@ namespace OHOS
             playersView->ChangeView(PlayersView::MUSIC_LIST_VIEW, PlayersView::MUSIC_DELETE_VIEW);\r
             if (playersView->GetPlayerListDelSongView())\r
             {\r
-                playersView->GetPlayerListDelSongView()->SetTheDelSongLabelStyle(musicPlayName, view.GetViewIndex());\r
+                playersView->GetPlayerListDelSongView()->SetTheDelSongLabelStyle(musicPlayName);\r
             }\r
             return true;\r
         }\r
@@ -75,6 +75,7 @@ namespace OHOS
         std::string strSrcMusic(APP_MUSIC_PATH);\r
         std::string tempStr("/");\r
         std::string strName = musicPlayName;\r
+        PlayersModel::GetInstance()->setcurrentplayingmusicname(musicPlayName); \r
         PlayersModel::GetInstance()->SetPlayerUri(strSrcMusic + tempStr + strName); // 设置播放路径Uri\r
 \r
         state = playersView->GetPlayerButtonState();\r
@@ -172,7 +173,7 @@ namespace OHOS
         {\r
             item = static_cast<PlayerListItemView *>(inView);\r
         }\r
-        if (PlayersModel::GetInstance()->GetCurrentIndex() == index && PlayersModel::GetInstance()->GetCurrentMusicIsPlaying())\r
+        if (PlayersModel::GetInstance()->getcurrentplayingmusicname() == node->data_.sliceId  && PlayersModel::GetInstance()->GetCurrentMusicIsPlaying())\r
         {\r
             item->UpdateItemViewStyle(true);\r
         }\r
index 1cde3a6..7dd2ad8 100644 (file)
@@ -250,6 +250,7 @@ int PlayersModel::PlayerInit(void)
         char strFile[BUFFER_SIZE] = "No music files";
         sliceId = strFile;
     }
+    currentplayingmusicname = sliceId;//记录当前播放的歌曲的名称
     std::string strSongName = sliceId;
     std::string pureName = strSongName.substr(0, strSongName.rfind("."));
     const char *songName = pureName.c_str();
@@ -279,6 +280,7 @@ void PlayersModel::NextSong(int16_t index)
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetCase %d sliceId err!!!", index);
         return;
     }
+    currentplayingmusicname = sliceId;//记录当前播放的歌曲的名称
     WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GetCase sliceId %s.", sliceId);
     std::string strSrcMusic(APP_MUSIC_PATH);
     std::string tempStr("/");
@@ -1129,6 +1131,19 @@ bool PlayersModel::GetCurrentMusicIsPlaying(void)
         }
         return "";
     }
+
+    //根据UTF-8文件名查找对应的16进制文件名
+    std::string PlayersModel::getMusicNameFromHexMusicName(const std::string &musicName)
+    {
+        for (const auto &entry : idToMusicNameMap)
+        {
+            if (entry.second.second == musicName)
+            {
+                return entry.second.first; // 返回对应音乐名
+            }
+        }
+        return "";
+    }
     
     //获取 idToMusicNameMap 的 zize 大小 
     int PlayersModel::getIdToMusicNameMapNumber(void)
@@ -1151,6 +1166,16 @@ bool PlayersModel::GetCurrentMusicIsPlaying(void)
     {
         preshowView = index;  
     }
+    //获取当前播放的音乐名字
+    std::string PlayersModel::getcurrentplayingmusicname()
+    {
+        return currentplayingmusicname;
+    }
+    //设置当前播放的音乐名字
+    void PlayersModel::setcurrentplayingmusicname(std::string name)
+    {
+       currentplayingmusicname =  name;
+    }
 }
 
 #ifdef __cplusplus