通過藍牙協議播放音樂,有的時候需要顯示歌詞,這里就是a2dp庫獲取了歌詞
值得注意的是要想正確獲取到歌詞,必須打開各種播放器的字幕(歌詞)開關
本項目用了三個開源庫 a2dp,tft_espi,xfont.
a2dp :藍牙傳輸音頻資料和字幕信息
tft_espi : tft 屏幕驅動
xfont : 顯示漢字 ,詳細使用請參考 GitHub - StarCompute/tftziku: 這是一個通過單片機在各種屏幕上顯示中文的解決方案
注意,xfont的初始化必須先執行。
項目內的代碼通過網易云音樂 播放正常獲取到歌詞。
?
項目整體開源到了: https://github.com/StarCompute/bluemusic
?
#include <Arduino.h>
// #include "AudioTools.h"
#include "a2dp/BluetoothA2DPSink.h"
#include "xfont.h"// #define TFT_SCLK 22
// #define TFT_MOSI 21
// #define TFT_RST 25
// #define TFT_DC 14
// #define TFT_CS 15XFont *_xFont;
BluetoothA2DPSink a2dp_sink;
// Then somewhere in your sketch:
void read_data_stream(const uint8_t *data, uint32_t length)
{i2s_write(I2S_NUM_0,data,length,NULL,0);
}
void rssi(esp_bt_gap_cb_param_t::read_rssi_delta_param &rssiParam)
{Serial.print("rssi value: ");Serial.println(rssiParam.rssi_delta);
}String song_singer = "";
String song_album = "";
float totalSongTime=1.0;// 顯示播放的百分比,其實可以優化的更好
void avrc_rn_play_pos_callback(uint32_t play_pos) {Serial.printf("Play position is %d (%d seconds)\n", play_pos, (int)round(play_pos/1000.0));if(totalSongTime>0){float per=play_pos/totalSongTime*100;_xFont->tft.fillRect(0, 102, 400, 40, TFT_BLACK);_xFont->DrawChineseEx(0, 102, "" + String(per,2)+"%", TFT_SILVER,TFT_BLACK);}
}void avrc_metadata_callback(uint8_t data1, const uint8_t *data2)
{// Serial.printf("AVRC metadata rsp: attribute id 0x%x, %s\n", data1, data2);String strData2 = (const char *)data2;if(strData2!="0")Serial.printf("data1: %d data2: %s \n",data1,strData2);if (data1 == 2){// Serial.printf("歌名:%s", data2);if (song_singer != strData2){_xFont->tft.fillRect(0, 2, 400, 40, TFT_BLACK);_xFont->DrawChineseEx(0, 2, "" + strData2,TFT_GREEN,TFT_BLACK);song_singer = strData2;}}if (data1 == 4){// Serial.printf("歌手:%s", data2);if (song_album != strData2){_xFont->tft.fillRect(0, 46, 400, 40, TFT_BLACK);_xFont->DrawChineseEx(0, 46, "" + strData2, TFT_RED,TFT_BLACK);song_album = strData2;}}if (data1 == 1){// Serial.printf("專輯:%s", data2);_xFont->tft.fillRect(0, 71, 400, 30, TFT_BLACK);_xFont->DrawChineseEx(0, 71, "" + strData2, TFT_LIGHTGREY,TFT_BLACK);}if(data1==64)totalSongTime=strData2.toFloat();// 0x8 第幾首歌// 0x10 總共多少首歌// 0x20
}void connection_state_changed(esp_a2d_connection_state_t state, void *ptr)
{Serial.println(a2dp_sink.to_str(state));
}void audio_state_changed(esp_a2d_audio_state_t state, void *ptr)
{Serial.println(a2dp_sink.to_str(state));
}void setup()
{Serial.begin(115200);_xFont = new XFont(true);_xFont->DrawChineseEx(0, 2, "歡迎使用。。", TFT_GREEN);a2dp_sink.set_avrc_metadata_attribute_mask(ESP_AVRC_MD_ATTR_TITLE | ESP_AVRC_MD_ATTR_ARTIST | ESP_AVRC_MD_ATTR_ALBUM | ESP_AVRC_MD_ATTR_TRACK_NUM | ESP_AVRC_MD_ATTR_NUM_TRACKS|ESP_AVRC_MD_ATTR_PLAYING_TIME|ESP_AVRC_MD_ATTR_GENRE);// a2dp_sink.a2dp_sink.set_avrc_metadata_callback(avrc_metadata_callback);a2dp_sink.set_on_connection_state_changed(connection_state_changed);a2dp_sink.set_on_audio_state_changed(audio_state_changed);a2dp_sink.set_avrc_rn_play_pos_callback(avrc_rn_play_pos_callback);i2s_pin_config_t my_pin_config = {.bck_io_num = 32,//輸入.ws_io_num = 33,//輸入.data_out_num = 23,//輸出.data_in_num = I2S_PIN_NO_CHANGE};a2dp_sink.set_pin_config(my_pin_config);a2dp_sink.start("my music");}void loop()
{delay(100);
}
?由于歌詞內容不確定,所以使用了開源中文字庫 :
GitHub - StarCompute/tftziku: 這是一個通過單片機在各種屏幕上顯示中文的解決方案
?
?
?