查看“播放MP3”的源代码
←
播放MP3
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
{| style="width: 800px;" |- | <p style="color: #4F4E4E;font-size:220%">'''AudioPro播放MP3示例'''</p> <br> 在下面的示例中,使用Core+核心控制AudioPro模块从SD卡中读取MP3播放。 <br> <br> <p style="color: #E87E05;font-size:155%">所需硬件</p> *[[MCookie-Core+]] *[[MBattery+]] *[[MCookie-Module AudioPro]] *[[MCookie-SD]] *一张TF卡/MicroSD卡(存有音乐) <br> <p style="color: #E87E05;font-size:155%">电路搭建</p> 将Battery、Core、MCookie_SD堆叠在一起,并插入SD卡,通过MicroUSB数据线接入电脑。 <br> <p style="color: #E87E05;font-size:155%">代码</p> <source lang="cpp"> /* Microduino_AudioPro支持库例程 模块WIKI:https://wiki.microduino.cn/index.php/MCookie-Module_AudioPro 串口控制的简易MP3,需配合Microduino_SD模块使用 详情见help */ #include <Microduino_AudioPro.h> #include <SD.h> AudioPro_FilePlayer musicPlayer = AudioPro_FilePlayer(SD); uint8_t fileNum = 0; //文件数量 void playNum(uint8_t num) { if (num > musicPlayer.getMusicNum() - 1) { return; } if (!musicPlayer.stopped()) { musicPlayer.stopPlaying(); //必要,否则SD类得不到关闭,内存溢出 } musicPlayer.flushCancel(both); //清缓存,播放MIDI等格式文件时必要 String _name = musicPlayer.getMusicName(num); Serial.print(F("Playing:")); if (!musicPlayer.playMP3(_name)) { Serial.println(F("ERROR")); } else { Serial.print(F("OK \t File: ")); Serial.println(_name); } } void setup() { Serial.begin(115200); Serial.println(F("AudioPro(VS1053) Simple Test")); pinMode(SD_PIN_SEL, OUTPUT); //先初始化AudioPro,所以先使能SD卡 digitalWrite(SD_PIN_SEL, HIGH); delay(500); if (! musicPlayer.begin()) { // initialise the music player Serial.println(F("Couldn't find VS1053, do you have the right pins defined?")); while (1); } Serial.println(F("VS1053 found")); if (!SD.begin(SD_PIN_SEL)) { Serial.println(F("initialization failed!")); return; } Serial.println(F("initialization done.")); musicPlayer.setVolume(96); //left & right 0-127 //or //musicPlayer.setVolume(96, 96); //left right 0-127 // If DREQ is on an interrupt pin, we can do background musicPlayer.useInterrupt(VS1053_PIN_DREQ); // DREQ int help(); } void loop() { if (Serial.available()) { char c = Serial.read(); if ((0x20 <= c) && (c <= 0x126)) { // strip off non-ASCII, such as CR or LF if (c >= '0' && c <= '9') { int num = c - 48; playNum(num); } else if (c == 'a') { //控制运放开关 if (! musicPlayer.getAmplifier()) { Serial.println(F("Amplifier On")); musicPlayer.setAmplifier(true); } else { Serial.println(F("Amplifier Off")); musicPlayer.setAmplifier(false); } } else if (c == 's') { //停止播放音乐 Serial.println(F("Stopping")); musicPlayer.stopPlaying(); } // if we get an 'p' on the serial console, pause/unpause! else if (c == 'p') { //暂停播放音乐 if (musicPlayer.stopped()) { Serial.println(F("Player is Stopping.")); } else if (! musicPlayer.paused()) { Serial.println(F("Paused")); musicPlayer.pausePlaying(true); } else { Serial.println(F("Resumed")); musicPlayer.pausePlaying(false); } } else if ((c == '-') || (c == '+')) { //调整音量 int _volume; if (c == '-') { // note dB is negative _volume = musicPlayer.volumeDown(); } else { _volume = musicPlayer.volumeUp(); } Serial.print(F("Volume changed to ")); Serial.println(_volume); } else if (c == 'i') { //显示系统常见信息 Serial.print(F("getVolume:")); Serial.println(musicPlayer.getVolume()); Serial.print(F("getAmplifier:")); Serial.println(musicPlayer.getAmplifier()); Serial.print(F("getPlaySpeed:")); Serial.println(musicPlayer.getPlaySpeed()); Serial.print(F("decodeTime:")); Serial.println(musicPlayer.decodeTime()); } else if ((c == '>') || (c == '<')) { //控制播放速度 uint16_t playspeed = musicPlayer.getPlaySpeed(); // create key_command existing variable // note playspeed of Zero is equal to ONE, normal speed. if (c == '>') { // note dB is negative playspeed ++; // keep it simpler with whole dB's } else { playspeed --; } playspeed = constrain(playspeed, 1, 127); musicPlayer.setPlaySpeed(playspeed); // commit new playspeed Serial.print(F("playspeed to ")); Serial.println(playspeed, DEC); } else if (c == 'm') { //立体声 uint16_t monostate = musicPlayer.getMonoMode(); Serial.print(F("Mono Mode ")); if (monostate == 0) { musicPlayer.setMonoMode(1); Serial.println(F("Enabled.")); } else { musicPlayer.setMonoMode(0); Serial.println(F("Disabled.")); } } else if (c == 'd') { //差分输出 uint16_t diff_state = musicPlayer.getDifferentialOutput(); Serial.print(F("Differential Mode ")); if (diff_state == 0) { musicPlayer.setDifferentialOutput(1); Serial.println(F("Enabled.")); } else { musicPlayer.setDifferentialOutput(0); Serial.println(F("Disabled.")); } } else if (c == 'r') { //复位VS1053 Serial.println(F("Reset Player.")); musicPlayer.reset(); } else if (c == 'n') { //初始化VS1053 Serial.println(F("begin Player.")); musicPlayer.begin(); } else if (c == 'f') { //关闭VS1053 Serial.println(F("End Player.")); musicPlayer.end(); } else if (c == 'l') { //列出音乐文件清单 if (musicPlayer.paused() || musicPlayer.stopped()) { Serial.println(F("Enter Index of File to play")); fileNum = musicPlayer.getMusicNum(); //可以获取SD卡中曲目列表以及数量 Serial.print(F("Music Files : ")); Serial.println(fileNum); for (uint8_t a = 0; a < fileNum; a++) { Serial.print(F("\t File[")); Serial.print(a); Serial.print(F("]: ")); Serial.println(musicPlayer.getMusicName(a)); } } else { Serial.println(F("Busy Playing Files, try again later.")); } } else if (c == 'h') { //显示帮助 help(); } } } delay(100); } void help() { Serial.println(F("Microduino AudioPro Library Example:")); Serial.println(F("COMMANDS:")); Serial.println(F(" [0-9] to play a track")); Serial.println(F(" [a] to Set Amplifier")); Serial.println(F(" [s] to stop playing")); Serial.println(F(" [p] to pause or unpause")); Serial.println(F(" [s] to stop playing")); Serial.println(F(" [+ or -] to change volume")); Serial.println(F(" [> or <] to increment or decrement play speed by 1 factor")); Serial.println(F(" [m] Toggle between Mono and Stereo Output.")); Serial.println(F(" [d] to toggle SM_DIFF between inphase and differential output")); Serial.println(F(" [r] Resets and initializes VS10xx chip.")); Serial.println(F(" [f] turns OFF the VS10xx into low power reset.")); Serial.println(F(" [n] turns ON the VS10xx out of low power reset.")); Serial.println(F(" [i] retrieve current audio information (partial list)")); Serial.println(F(" [l] Display list of music files")); Serial.println(F(" [h] this help")); } </source> [[File:AudioPro MP3.jpg|400px|thumb|center|运行结果]] 对照列表中的项目,每一行开头的字符通过串口输入(波特率115200): 0~9 播放SD卡中的歌曲,实际可以更多,此处为示例,可以通过程序修改 a 开/关LM4863功放模块 s 停止播放 P 暂停播放/继续播放 +或- 增加1点音量或降低1点音量,可在串口连续输入 >或< 加速播放或减速播放 m 切换单声道(mono)与立体声(stereo) d 切换同相/区分输出 r 重启VS1053芯片 f 将VS1053设置为低功耗模式并重启 n 将VS1053从低功耗模式唤醒 i 获取当前音频信息 l 获取播放列表 h 帮助列表 <br> |} [[MCookie-Module AudioPro | 返回AudioPro页面]]
返回至
播放MP3
。
导航菜单
个人工具
创建账户
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
Welcome
首页
创客大赛
大赛详情
3D打印
安装月球车
图形化编程
操控月球车
升级月球车
编程工具下载
软件下载
Arduino
Processing
Mixly
Scratch
模块套件
Microduino 102
mCookie 102
mCookie 202
mCookie 302
IBC
其他
应用套件
四轴飞行器
平衡车
小车CUBE
音乐播放器
刷卡音乐播放器
wifi气象站
彩虹音乐触摸灯
分贝检测仪
迎门汇报
LED点阵时钟
LED点阵屏幕
硬件
mCookie
Sensor
Microduino
MicroWrt
MicroNux
MicroRobot-Core
MicroRobot-CoreESP
ideaBoard
ideaBox
MicroMV
MicroAI
帮助
常见问题
帮助
工具
链入页面
相关更改
特殊页面
页面信息