ESP32 通过蓝牙显示歌词代码示例

通过蓝牙协议播放音乐,有的时候需要显示歌词,这里就是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   	 15


XFont *_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: 这是一个通过单片机在各种屏幕上显示中文的解决方案

 

 

 

使用Arduino、ESP32和OLED显示屏实现蓝牙歌词显示是一个有趣且实用的项目。以下是实现这个项目的步骤和所需的主要组件: 1. **硬件组件**: - ESP32开发板 - OLED显示屏(如SSD1306) - 蓝牙模块(如HC-05,但ESP32内置蓝牙功能) - 连接线 2. **软件组件**: - Arduino IDE - 必要的库(如Adafruit SSD1306库、Adafruit GFX库、ESP32 BLE库) ### 实现步骤 1. **连接硬件**: - 将OLED显示屏连接到ESP32开发板。常见的连接方式如下: - VCC -> 3.3V - GND -> GND - SCL -> GPIO 22 - SDA -> GPIO 21 2. **配置Arduino IDE**: - 在Arduino IDE中添加ESP32开发板支持。 - 安装必要的库:Adafruit SSD1306库和Adafruit GFX库。 3. **编写代码**: - 初始化OLED显示屏和蓝牙功能。 - 接收蓝牙数据并解析歌词。 - 将解析后的歌词显示在OLED显示屏上。 ```cpp #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define SERVICE_UUID "uuid_of_service" #define CHARACTERISTIC_UUID "uuid_of_characteristic" void setup() { Serial.begin(115200); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } display.display(); delay(2000); display.clearDisplay(); BLEDevice::init("ESP32 Lyric Display"); BLEServer *pServer = BLEDevice::createServer(); BLEService *pService = pServer->createService(SERVICE_UUID); BLECharacteristic *pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE ); pCharacteristic->setValue("Hello World"); pService->start(); BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(false); pAdvertising->setMinPreferred(0x0); pAdvertising->setMinPreferred(0x0); BLEDevice::startAdvertising(); Serial.println("Waiting for a client connection..."); } void loop() { BLECharacteristic *pCharacteristic = BLEDevice::getCharacteristic(CHARACTERISTIC_UUID); if(pCharacteristic->canRead()) { std::string value = pCharacteristic->readValue(); display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0, 10); display.println(value.c_str()); display.display(); } delay(1000); } ``` ### 说明 - 上述代码初始化了OLED显示屏和蓝牙功能。 - 当接收到蓝牙数据时,解析并显示在OLED显示屏上。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值