🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    How to stop MP3 file from playing (pause / rewind button)?

    Scheduled Pinned Locked Moved PROJECTS
    1 Posts 1 Posters 4.3k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C Offline
      creativetabi
      last edited by

      Hi,

      Can I ask a question?
      I want to make an m5stack program that would play an mp3 file if I send a trigger via UDP, and would stop if I send a different trigger via UDP.
      I followed some code from:
      http://forum.m5stack.com/topic/143/lesson-6-1-speaker-mp3-player/9

      But when I sent the trigger, the mp3 only played the first second (1s) and kept repeating the same time position. It wouldn't play the entire mp3 song.

      This is the code that I used:


      #include <M5Stack.h>
      #include <WiFi.h>
      #include <WiFiUdp.h>
      #include "AudioFileSourceSD.h"
      #include "AudioFileSourceID3.h"
      #include "AudioGeneratorMP3.h"
      #include "AudioOutputI2S.h"
      #define N 1024

      bool playing = true;
      AudioGeneratorMP3 *mp3;
      AudioFileSourceSD *file;
      AudioOutputI2S *out;
      AudioFileSourceID3 *id3;

      const char* ssid = "wifiname";
      const char* password = "wifipassword";
      const int port = 5555;

      // The udp library class
      WiFiUDP udp;

      void print_wifi_state(){
      M5.Lcd.clear(BLACK); // clear LCD
      M5.Lcd.setTextColor(YELLOW);
      M5.Lcd.setCursor(3, 3);
      M5.Lcd.println("");
      M5.Lcd.println("WiFi connected.");
      M5.Lcd.print("IP address: ");
      M5.Lcd.println(WiFi.localIP());
      M5.Lcd.print("Port: ");
      M5.Lcd.println(port);
      }

      void setup_wifi(){
      M5.Lcd.setTextColor(RED);
      M5.Lcd.setTextSize(2);
      M5.Lcd.setCursor(3, 10);
      M5.Lcd.print("Connecting to ");
      M5.Lcd.println(ssid);

      // setup wifi
      WiFi.mode(WIFI_STA); // WIFI_AP, WIFI_STA, WIFI_AP_STA or WIFI_OFF
      WiFi.begin(ssid, password);
      // WiFi.begin();

      // Connecting ..
      while (WiFi.status() != WL_CONNECTED) {
      delay(100);
      M5.Lcd.print(".");
      }

      // print state
      print_wifi_state();

      udp.begin(port);
      

      }

      void setup() {
      M5.begin();
      M5.Speaker.setVolume(5);
      play('m');
      // setup wifi
      setup_wifi();

      }

      bool play(char dir){
      switch(dir)
      {
      case 'm':
      delete file;
      delete out;
      delete mp3;
      mp3 = NULL;
      file = NULL;
      out = NULL;
      file = new AudioFileSourceSD("/");
      id3 = new AudioFileSourceID3(file);
      out = new AudioOutputI2S(0, 1);
      out->SetOutputModeMono(true);
      mp3 = new AudioGeneratorMP3();
      mp3->begin(id3, out);
      playing = false;
      return true;
      default:
      if(playing){
      play('m');
      return true;
      }
      break;
      }
      mp3->stop();
      delete file;
      delete out;
      delete mp3;
      mp3 = NULL;
      file = NULL;
      out = NULL;
      file = new AudioFileSourceSD("/RainDrizzle.mp3");
      id3 = new AudioFileSourceID3(file);
      id3->seek(trackList->timePos, 1);
      out = new AudioOutputI2S(0, 1);
      out->SetOutputModeMono(true);
      mp3 = new AudioGeneratorMP3();
      mp3->begin(id3, out);
      playing = true;

      return true;
      }

      void loop() {
      char packetBuffer[N];
      int packetSize = udp.parsePacket();

      // get packet
      if (packetSize){

      int len = udp.read(packetBuffer, packetSize);
      
      if (len > 0){
        packetBuffer[len] = '\0'; // end
      }
      

      }

      if(strcmp(packetBuffer,"start")==0){
      // print param
      M5.Lcd.clear(BLACK);
      M5.Lcd.setCursor(3, 3);
      M5.Lcd.setTextColor(GREEN);
      M5.Lcd.println(packetBuffer);
      
      play('t');
      
      }
      
      if(strcmp(packetBuffer,"stop")==0){
      M5.Lcd.clear(BLACK);
      M5.Lcd.setCursor(3, 3);
      M5.Lcd.setTextColor(GREEN);
      M5.Lcd.println(packetBuffer);
      play('m');
      }
      
      if(playing){
        if(mp3->isRunning()){
          if(!mp3->loop()){
            mp3->stop();
            playing = false;
          }
        }
        else{
          delay(1000);
        }
      

      }
      M5.update();
      }


      Please advise if anything's wrong in the code.

      Thank you in advance.

      1 Reply Last reply Reply Quote 0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • First post
        Last post