Announcement

Collapse
No announcement yet.

Using SoundPlayer to play music.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using SoundPlayer to play music.

    Hello! I wanted to make our robot sing a specific song...and I searched for a method to do that. I found that there is a sound player built into the FTC SDK, named SoundPlayer. When I try to use it, it plays the song (song.mp3), but for a couple of seconds. What are the supported types of files (mp3, wav etc)? Can you help me find a solution? Thanks!

  • #2
    Two years ago, we did a similar thing to play "And his name is John Cena!" while a button was held down. We had a cena.mp3 file in the res/raw folder. The gist of the code was this:
    Code:
     
     import android.media.MediaPlayer; public class CenaPlayer { 	//The player handling the audio 	private static MediaPlayer mediaPlayer = null;  	//Start the wubs 	public static void start(Context context) { 		if (mediaPlayer == null) mediaPlayer = MediaPlayer.create(context, R.raw.cena); 		mediaPlayer.seekTo(0); 		mediaPlayer.start(); 	} 	//Stop the wubs 	public static void stop() { 		if (mediaPlayer != null) { 			mediaPlayer.stop(); 			try { mediaPlayer.prepare(); } 			catch (IOException e) {} 		} 	} }  public class SoundOpMode extends OpMode {   private boolean pressedLast;    public void init() {     this.pressedLast = false;   }   public void loop() {     if (this.gamepad1.a && !this.pressedLast) {       CenaPlayer.start(this.hardwareMap.appContext);       this.pressedLast = true;     }     else if (!this.gamepad1.a && this.pressedLast) {       CenaPlayer.stop();       this.pressedLast = false;     }   }   public void stop() {     CenaPlayer.stop();   } }

    Comment


    • #3

      Sorry about the weird formatting. Here it is again.
      Code:
      import android.media.MediaPlayer;
      public class CenaPlayer {
        //The player handling the audio
        private static MediaPlayer mediaPlayer = null;
        //Start the wubs
        public static void start(Context context) {
          if (mediaPlayer == null) mediaPlayer = MediaPlayer.create(context, R.raw.cena);
          mediaPlayer.seekTo(0);
          mediaPlayer.start();
        }
        //Stop the wubs
        public static void stop() {
          if (mediaPlayer != null) {
            mediaPlayer.stop();
            try { mediaPlayer.prepare(); }
            catch (IOException e) {}
          }
        }
      }
      public class SoundOpMode extends OpMode {
        private boolean pressedLast;
        public void init() {
          this.pressedLast = false;
        }
        public void loop() {
          if (this.gamepad1.a && !this.pressedLast) {
            CenaPlayer.start(this.hardwareMap.appContext);
            this.pressedLast = true;
          }
          else if (!this.gamepad1.a && this.pressedLast) {
            CenaPlayer.stop();
            this.pressedLast = false;
          }
        }
        public void stop() {
          CenaPlayer.stop();
        }
      }

      Comment


      • #4
        Thanks! I will try this.

        Comment

        Working...
        X