实现故事的:播发指定文件,暂停播放,恢复播放接口
authorcheng <chengyf@zhihuishixun.com>
Sat, 13 Sep 2025 10:33:38 +0000 (18:33 +0800)
committercheng <chengyf@zhihuishixun.com>
Sat, 13 Sep 2025 10:33:38 +0000 (18:33 +0800)
code/sdk/application/wearable/fd230/nativeapp/nativeui/leiniao/include/ln_story_player_simple.h
code/sdk/application/wearable/fd230/nativeapp/nativeui/leiniao/src/ln_story_player_simple.cpp
code/sdk/zhihui/leiniao/imsl_ln/leiniao_api_at.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api.h

index b558268..e5ba5d8 100644 (file)
@@ -56,6 +56,46 @@ int ln_story_player_simple_get_current_index(void);
  */
 int ln_story_player_simple_get_file_count(void);
 
+/**
+ * @brief 播放指定音频文件
+ * @param file_path 音频文件完整路径
+ * @return true 成功,false 失败
+ * 
+ * 功能说明:
+ * - 播放指定的单个音频文件
+ * - 不会自动播放下一个文件
+ * - 需要手动调用停止或暂停
+ */
+bool ln_story_player_simple_play_file(const char *file_path);
+
+/**
+ * @brief 暂停播放
+ * @return true 成功,false 失败
+ * 
+ * 功能说明:
+ * - 暂停当前正在播放的音频
+ * - 保持播放器状态,可以恢复播放
+ * - 不会清理音频资源
+ */
+bool ln_story_player_simple_pause(void);
+
+/**
+ * @brief 恢复播放
+ * @return true 成功,false 失败
+ * 
+ * 功能说明:
+ * - 恢复之前暂停的音频播放
+ * - 只能在暂停状态下调用
+ * - 如果当前没有暂停的音频,返回失败
+ */
+bool ln_story_player_simple_resume(void);
+
+/**
+ * @brief 检查是否暂停
+ * @return true 已暂停,false 未暂停
+ */
+bool ln_story_player_simple_is_paused(void);
+
 /**
  * @brief 处理播放器消息队列命令(内部使用)
  * @param cmd 命令类型
index d8ffe7d..441ae6e 100644 (file)
@@ -58,6 +58,7 @@ typedef struct
     /* 状态标志 */
     bool is_playing;
     bool should_stop;
+    bool is_paused;  // 暂停状态标志
 } ln_story_player_simple_t;
 
 
@@ -86,6 +87,7 @@ public:
             else
             {
                 printf("[SIMPLE_PLAYER] all files played, stopping(所有文件播放完成)\n");
+                do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_AUDIO_STOP, NULL, 0);
                 impl_->is_playing = false;
             }
         }
@@ -432,6 +434,7 @@ static bool ln_story_player_simple_player_stat(ln_story_player_simple_t *impl, i
     }
     
     impl->is_playing = true;
+    impl->is_paused = false;  // 重置暂停状态
     printf("[SIMPLE_PLAYER] started playing file %d\n", file_index);
     return true;
 }
@@ -530,6 +533,7 @@ void ln_story_player_simple_stop(void)
     
     g_simple_player.should_stop = true;
     g_simple_player.is_playing = false;
+    g_simple_player.is_paused = false;
     
     if (!g_simple_player.player)
     {
@@ -610,6 +614,116 @@ int ln_story_player_simple_get_file_count(void)
     return g_simple_player.file_count;
 }
 
+// 播放指定音频文件
+bool ln_story_player_simple_play_file(const char *file_path)
+{
+    if (!file_path)
+    {
+        printf("[SIMPLE_PLAYER] invalid file path\n");
+        return false;
+    }
+    
+    printf("[SIMPLE_PLAYER] playing single file: %s\n", file_path);
+    
+    // 如果已经在播放,先停止
+    if (g_simple_player.is_playing)
+    {
+        printf("[SIMPLE_PLAYER] stopping current playback\n");
+        ln_story_player_simple_stop();
+    }
+    
+    // 初始化音频(只初始化一次)
+    if (!AudioInit())
+    {
+        printf("[SIMPLE_PLAYER] audio init failed\n");
+        return false;
+    }
+    
+    // 设置单个文件播放模式
+    g_simple_player.file_count = 1;
+    g_simple_player.current_index = 0;
+    g_simple_player.should_stop = false;
+    g_simple_player.is_paused = false;
+    strncpy(g_simple_player.file_paths[0], file_path, sizeof(g_simple_player.file_paths[0]) - 1);
+    g_simple_player.file_paths[0][sizeof(g_simple_player.file_paths[0]) - 1] = '\0';
+    
+    // 播放文件
+    if (!ln_story_player_simple_play_file(&g_simple_player, 0))
+    {
+        printf("[SIMPLE_PLAYER] failed to start playing single file\n");
+        return false;
+    }
+    
+    printf("[SIMPLE_PLAYER] single file playback started successfully\n");
+    return true;
+}
+
+// 暂停播放
+bool ln_story_player_simple_pause(void)
+{
+    if (!g_simple_player.player)
+    {
+        printf("[SIMPLE_PLAYER] player not initialized\n");
+        return false;
+    }
+    
+    if (!g_simple_player.is_playing)
+    {
+        printf("[SIMPLE_PLAYER] not currently playing\n");
+        return false;
+    }
+    
+    if (g_simple_player.is_paused)
+    {
+        printf("[SIMPLE_PLAYER] already paused\n");
+        return false;
+    }
+    
+    int32_t ret = g_simple_player.player->Pause();
+    if (ret != 0)
+    {
+        printf("[SIMPLE_PLAYER] pause failed: %d\n", ret);
+        return false;
+    }
+    
+    g_simple_player.is_paused = true;
+    printf("[SIMPLE_PLAYER] playback paused\n");
+    return true;
+}
+
+// 恢复播放
+bool ln_story_player_simple_resume(void)
+{
+    if (!g_simple_player.player)
+    {
+        printf("[SIMPLE_PLAYER] player not initialized\n");
+        return false;
+    }
+    
+    if (!g_simple_player.is_paused)
+    {
+        printf("[SIMPLE_PLAYER] not currently paused\n");
+        return false;
+    }
+    
+    int32_t ret = g_simple_player.player->Play();
+    if (ret != 0)
+    {
+        printf("[SIMPLE_PLAYER] resume failed: %d\n", ret);
+        return false;
+    }
+    
+    g_simple_player.is_paused = false;
+    printf("[SIMPLE_PLAYER] playback resumed\n");
+    return true;
+}
+
+// 检查是否暂停
+bool ln_story_player_simple_is_paused(void)
+{
+    return g_simple_player.is_paused;
+}
+
 #ifdef __cplusplus
 #if __cplusplus
 }
index 0ca99aa..7b48c97 100644 (file)
@@ -645,7 +645,7 @@ static uint32_t ln_audio_play_at(uint8_t *para, uint32_t paraLen, char *strCommo
     return ERRCODE_SUCC;
 }
 
-/* 音频停止: AT^LN_STORY_PLAY_PREV */
+/* 音频停止: AT^LN_STORY_STOP_AT */
 static uint32_t ln_audio_stop_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
 {
     VAD_UNUSED(para); VAD_UNUSED(paraLen); VAD_UNUSED(strCommonCmd);
@@ -654,6 +654,42 @@ static uint32_t ln_audio_stop_at(uint8_t *para, uint32_t paraLen, char *strCommo
     return ERRCODE_SUCC;
 }
 
+/* 播放指定文件: AT^LN_AUDIO_PLAY_FILE=file_path */
+static uint32_t ln_audio_play_file_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
+{
+    VAD_UNUSED(strCommonCmd);
+    printf("[LN][AUDIO_PLAY_FILE]\n");
+    if (para && paraLen > 0)
+    {
+        // 将参数作为文件路径发送
+        do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_AUDIO_PLAY_FILE, (char*)para, paraLen);
+    }
+    else
+    {
+        printf("[LN] Error: No file path provided\n");
+        return ERRCODE_FAIL;
+    }
+    return ERRCODE_SUCC;
+}
+
+/* 暂停播放: AT^LN_AUDIO_PAUSE */
+static uint32_t ln_audio_pause_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
+{
+    VAD_UNUSED(para); VAD_UNUSED(paraLen); VAD_UNUSED(strCommonCmd);
+    printf("[LN][AUDIO_PAUSE]\n");
+    do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_AUDIO_PAUSE, NULL, 0);
+    return ERRCODE_SUCC;
+}
+
+/* 恢复播放: AT^LN_AUDIO_RESUME */
+static uint32_t ln_audio_resume_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
+{
+    VAD_UNUSED(para); VAD_UNUSED(paraLen); VAD_UNUSED(strCommonCmd);
+    printf("[LN][AUDIO_RESUME]\n");
+    do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_AUDIO_RESUME, NULL, 0);
+    return ERRCODE_SUCC;
+}
+
 
 static const at_cmd_table_t g_ln_api_cmd[] = {
     /* ----------------------------------- VAD ---------------------------------- */
@@ -681,6 +717,9 @@ static const at_cmd_table_t g_ln_api_cmd[] = {
     /* 音频播放 */
     {(char *)"AT^LN_STORY_PLAY_AT", ln_audio_play_at, (char *)"播放音频: AT^LN_STORY_PLAY_AT"},
     {(char *)"AT^LN_STORY_STOP_AT", ln_audio_stop_at, (char *)"停止播放音频: AT^LN_STORY_STOP_AT"},
+    {(char *)"AT^LN_AUDIO_PLAY_FILE", ln_audio_play_file_at, (char *)"播放指定文件: AT^LN_AUDIO_PLAY_FILE=/path/to/file.mp3"},
+    {(char *)"AT^LN_AUDIO_PAUSE", ln_audio_pause_at, (char *)"暂停播放: AT^LN_AUDIO_PAUSE"},
+    {(char *)"AT^LN_AUDIO_RESUME", ln_audio_resume_at, (char *)"恢复播放: AT^LN_AUDIO_RESUME"},
 };
 
 at_cmd_table_t *get_leiniao_api_cmd_table(uint32_t *cmd_count)
index 0f1d47a..bcb0a6f 100644 (file)
@@ -472,6 +472,57 @@ static void ln_http_server_send_task(void const *arg)
             ln_story_player_simple_handle_message(LN_HTTP_SERVICE_CMD_AUDIO_PLAY_NEXT, NULL, 0);
             #endif
         }
+        else if (mail.cmd == LN_HTTP_SERVICE_CMD_AUDIO_PLAY_FILE)
+        {
+            printf("LN_HTTP_SERVICE_CMD_AUDIO_PLAY_FILE:播放指定文件\n");
+            #ifdef BOARD_N9
+            if (mail.data != NULL)
+            {
+                char *file_path = (char*)mail.data;
+                printf("Playing file: %s\n", file_path);
+                if (ln_story_player_simple_play_file(file_path))
+                {
+                    printf("File playback started successfully\n");
+                }
+                else
+                {
+                    printf("Failed to start file playback\n");
+                }
+            }
+            else
+            {
+                printf("Error: No file path provided\n");
+            }
+            #endif
+        }
+        else if (mail.cmd == LN_HTTP_SERVICE_CMD_AUDIO_PAUSE)
+        {
+            printf("LN_HTTP_SERVICE_CMD_AUDIO_PAUSE:暂停播放\n");
+            #ifdef BOARD_N9
+            if (ln_story_player_simple_pause())
+            {
+                printf("Playback paused successfully\n");
+            }
+            else
+            {
+                printf("Failed to pause playback\n");
+            }
+            #endif
+        }
+        else if (mail.cmd == LN_HTTP_SERVICE_CMD_AUDIO_RESUME)
+        {
+            printf("LN_HTTP_SERVICE_CMD_AUDIO_RESUME:恢复播放\n");
+            #ifdef BOARD_N9
+            if (ln_story_player_simple_resume())
+            {
+                printf("Playback resumed successfully\n");
+            }
+            else
+            {
+                printf("Failed to resume playback\n");
+            }
+            #endif
+        }
         if (mail.data != NULL)
         {
             printf("free(mail.data)\n");
index 982a3c3..6bde4a0 100644 (file)
@@ -81,6 +81,9 @@ typedef struct
 #define LN_HTTP_SERVICE_CMD_AUDIO_PLAY 0x1001 /* 播放音频 */
 #define LN_HTTP_SERVICE_CMD_AUDIO_STOP 0x1002 /* 停止播放音频 */
 #define LN_HTTP_SERVICE_CMD_AUDIO_PLAY_NEXT 0x1003 /* 播放下一首 */
+#define LN_HTTP_SERVICE_CMD_AUDIO_PLAY_FILE 0x1004 /* 播放指定文件 */
+#define LN_HTTP_SERVICE_CMD_AUDIO_PAUSE 0x1005 /* 暂停播放 */
+#define LN_HTTP_SERVICE_CMD_AUDIO_RESUME 0x1006 /* 恢复播放 */
 
 typedef struct
 {