使用Audio播放歌曲,但运行代码时串口监视器说失败?-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2736|回复: 2

[未解决] 使用Audio播放歌曲,但运行代码时串口监视器说失败?

[复制链接]
发表于 2021-5-30 19:37 | 显示全部楼层 |阅读模式
使用Audio与MicroSD模块播放歌曲,但运行代码时串口监视器中提示  initialization failed!
看代码发现是
if (!SD.begin(SD_PIN_SEL)) {
    Serial.println(F("initialization failed!"));
    return;
  }
  Serial.println(F("initialization done."));

所以说括号里的红色条件怎么样才能满足?
按照步骤来应该是initialization done.

下面是全部代码,目的是播放SD卡中的歌曲

#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(" to stop playing"));
  Serial.println(F(" [p] to pause or unpause"));
  Serial.println(F(" 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(" retrieve current audio information (partial list)"));
  Serial.println(F(" [l] Display list of music files"));
  Serial.println(F(" [h] this help"));
}

没看懂的代码

没看懂的代码
发表于 2021-5-31 12:11 | 显示全部楼层
标黄部分,初始化失败打印第一串,成功打印第二串
发表于 2021-5-31 20:07 | 显示全部楼层
一般你这种情况就是 sd卡有兼容性问题,只能更换一个了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-29 05:45 , Processed in 0.076595 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表