音乐播放

来自Labplus盛思维基百科
Jiangzhaohui讨论 | 贡献2018年1月8日 (一) 08:17的版本 (创建页面,内容为“=== Arduino示例 === <pre style="color:blue"> #include <SoftwareSerial.h> #include "kt540b.h" uint8_t inBuff[3] = { 0 }; uint8_t inByte; Kt540bClass mp3Play(5, 6);…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

Arduino示例

#include <SoftwareSerial.h>
#include "kt540b.h"

uint8_t inBuff[3] = { 0 };
uint8_t inByte;
Kt540bClass mp3Play(5, 6);


void setup()
{
  Serial.begin(115200);
  mp3Play.begin();
  mp3Play.setVolume(50);
    mp3Play.play(1);

}

void loop()
{
  for (uint8_t i = 0; i < 3; i++)
    inBuff[i] = 0;

  while(Serial.available())
  {
    inByte = Serial.read();
    delay(2);
    if (inByte == 0xff)
    {
      inBuff[0] = inByte;
      Serial.readBytes(&inBuff[1], 2);
    }

    //for (uint8_t i = 0; i < 3; i++)

    //{
    //  Serial.write(inBuff[i]);
    //}
    if (inBuff[1] == 0x01)   // 播放指定歌曲
    {
      mp3Play.play(inBuff[2]);
    }
    if (inBuff[1] == 0x02)   // 播放上下首
    {
      switch (inBuff[2])
      {
      case 0x01:
        mp3Play.playNext();   
        break;
      case 0x02:
        mp3Play.playPrev();
        break;
      default:
        break;
      }
    }

    if (inBuff[1] == 0x03)   // 设置音量大小
    {
      mp3Play.setVolume(inBuff[2]);
    }

    if (inBuff[1] == 0x04)   // 音量增加减
    {
      switch (inBuff[2])
      {
      case 0x01:
        mp3Play.volumeInc();
        break;
      case 0x02:
        mp3Play.volumeDec();
        break;
      default:
        break;
      }
    }

    if (inBuff[1] == 0x05)   // 暂停、停止播放
    {
      switch (inBuff[2])
      {
      case 0x01:
        mp3Play.pause();   //暂停
        break;
      case 0x02:
        mp3Play.play();   //恢复播放
        break;
      case 0x03:
        mp3Play.stop();   //停止播放
        break;
      default:
        break;
      }
    }  
  }
}