player_bg.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "player_bg.h"
  2. #include "player_ui.h"
  3. #include <string.h>
  4. static rt_mq_t player_thread_mq;
  5. rt_bool_t is_playing = RT_FALSE;
  6. rt_bool_t player_is_playing()
  7. {
  8. return is_playing;
  9. }
  10. void player_play_file(const char* fn)
  11. {
  12. struct player_request request;
  13. request.type = PLAYER_REQUEST_PLAY_SINGLE_FILE;
  14. strncpy(request.fn, fn, sizeof(request.fn));
  15. /* send to message queue */
  16. rt_mq_send(player_thread_mq, (void*)&request, sizeof(struct player_request));
  17. }
  18. void player_stop()
  19. {
  20. is_playing = RT_FALSE;
  21. }
  22. void player_thread(void* parameter)
  23. {
  24. rt_err_t result;
  25. struct player_request request;
  26. while(1)
  27. {
  28. /* get request from message queue */
  29. result = rt_mq_recv(player_thread_mq, (void*)&request,
  30. sizeof(struct player_request), RT_WAITING_FOREVER);
  31. if (result == RT_EOK)
  32. {
  33. switch (request.type)
  34. {
  35. case PLAYER_REQUEST_PLAY_SINGLE_FILE:
  36. if ((strstr(request.fn, ".mp3") != RT_NULL) ||
  37. (strstr(request.fn, ".MP3") != RT_NULL))
  38. {
  39. is_playing = RT_TRUE;
  40. player_notify_play();
  41. /* get music tag information */
  42. mp3(request.fn);
  43. player_notify_stop();
  44. is_playing = RT_FALSE;
  45. }
  46. else if ((strstr(request.fn, ".wav") != RT_NULL) ||
  47. (strstr(request.fn, ".WAV") != RT_NULL))
  48. {
  49. is_playing = RT_TRUE;
  50. wav(request.fn);
  51. is_playing = RT_FALSE;
  52. }
  53. break;
  54. }
  55. }
  56. }
  57. }
  58. void player_init()
  59. {
  60. rt_thread_t tid;
  61. /* create player thread */
  62. player_thread_mq = rt_mq_create("player", sizeof(struct player_request),
  63. 8, RT_IPC_FLAG_FIFO);
  64. RT_ASSERT(player_thread_mq != RT_NULL);
  65. tid = rt_thread_create("ply_bg", player_thread, RT_NULL,
  66. 2048, 20, 5);
  67. if (tid != RT_NULL) rt_thread_startup(tid);
  68. player_ui_init();
  69. }