Introduction
In this article, we aim to develop a basic music player using Java Spring. The player will load, play, and stop audio files, specifically in WAV format. We will utilize Java’s built-in sound capabilities and the Spring framework to structure the application effectively.
Why We Used WAV Files Instead of MP3 Files
We opted for WAV files in this project mainly due to their uncompressed nature, which ensures high audio quality. WAV files store audio data in a straightforward format without any compression, making them ideal for applications that demand clear and precise sound reproduction. In contrast, MP3 files utilize lossy compression, which reduces file size by discarding certain audio information. While this compression makes MP3 files more convenient for storage and playback, it often results in a noticeable decline in audio quality, especially in professional audio applications. Moreover, working with WAV files simplifies the coding process since Java’s Sound API natively supports this format. This support allows us to avoid potential complications that can arise when decoding compressed formats like MP3.
Prerequisites
Before starting, ensure you have the following:
- Java Development Kit (JDK) installed on your machine.
- An IDE (like VS Code) for coding.
- A basic understanding of Java programming and the Spring framework.
Structure
/music-player
├── songs
│ ├── song1.wav
│ └── song2.wav
└── src
├── ConsoleApp.java
└── SongService.java
This is the basic structure of our project. In this formate we create folders and files on our console.
Now Let us see steps involved in Bulding this project.
Bulding The Project With Java Code
Step 1: Setting Up the Project :
- Create the Project Folder: Start by creating a folder named music-played.
- Create the Songs Folder: Inside the music-player folder, createa songs folder and place your WAV files there.
- Create the Source Folder: Inside the music-player folder, create a src folder for your Java files.
Step 2: Writing the Code :
In this we create total two files as showen in the structure i.e ConsoleApp.java and SongService.java. Now Let us see the code.
1. SongService.java
This class manages the loading and playback of audio files.
import javax.sound.sampled.*; import java.io.File; import java.io.IOException; public class SongService { private Clip audioClip; private AudioInputStream audioStream; private String currentSong; public void loadSong(String filePath) { try { if (audioClip != null && audioClip.isRunning()) { stop(); } currentSong = filePath; audioStream = AudioSystem.getAudioInputStream(new File(filePath)); audioClip = AudioSystem.getClip(); audioClip.open(audioStream); System.out.println("Song loaded: " + filePath); } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) { System.out.println("Failed to load song. Ensure the file is in .wav format."); } } public void play() { if (audioClip != null && !audioClip.isRunning()) { audioClip.start(); System.out.println("Playing song: " + currentSong); } else { System.out.println("No song loaded or already playing."); } } public void stop() { if (audioClip != null) { audioClip.stop(); audioClip.close(); System.out.println("Stopped song."); } } }
2. ConsoleApp.java
This main application interacts with users through the console.
import java.util.Scanner; public class ConsoleApp { public static void main(String[] args) { SongService songService = new SongService(); Scanner sc = new Scanner(System.in); System.out.println("Welcome to Console Music Player!"); while (true) { System.out.println("Commands: load <path>, play, stop, exit"); String input = sc.nextLine(); String[] command = input.split(" ", 2); switch (command[0]) { case "load": if (command.length > 1) { songService.loadSong(command[1]); } else { System.out.println("Please provide the file path."); } break; case "play": songService.play(); break; case "stop": songService.stop(); break; case "exit": songService.stop(); System.exit(0); break; default: System.out.println("Invalid command."); break; } } } }
Step 3: Compile and Run the code :
In this Step first we need to open ConsoleApp .java file in vs code after opening music-player folder.
Then we need to run the code. We get the output as follows :
Welcome to Console Music Player! Commands: load <path>, play, stop, exit
Now we will see the interaction with this output page in steps
Interacting With the Output Window
- We need to load the song from our console [* NOTE * : you need to download .wav music file before hand on your console. You can download music from FreemusicArchive and can convert frm open source websites like CloudConvert from mp3 to .wav file.]
- Load a Song -> load songs/song1.wav
- After to play the song simple type ‘play’ -> play
- Then the song will be played.
- To stop it just type s’top’ -> stop
- And finally to exit output window just type ‘exit’ -> exit
Welcome to Console Music Player! Commands: load <path>, play, stop, exit load C:\Users\Sai Sidhartha\Desktop\music-player\songs\Here Comes A Big Black Cloud!! - Graverobbin.wav Song loaded: C:\Users\Sai Sidhartha\Desktop\music-player\songs\Here Comes A Big Black Cloud!! - Graverobbin.wav Commands: load <path>, play, stop, exit play Playing song: C:\Users\Sai Sidhartha\Desktop\music-player\songs\Here Comes A Big Black Cloud!! - Graverobbin.wav Commands: load <path>, play, stop, exit stop Stopped song. Commands: load <path>, play, stop, exit exit Stopped song.
Credits
I have downloaded songs from FreemusicArchives Website FreemusicArchive and convertd them to .wav file using CloudConvert Website. Below I am giving details about three songs i used :
- Track -> Graverobbinby Here Comes A Big Black Cloud!! Album – Pompeii Genres – Rock,Loud-Rock,Psych-Rock,Indie-Rock
- Track -> Come Back (and stop those rainy days)by Rod Album – Noises in my head Genres – Rock,Noise-Rock,Psych-Rock,Indie-Rock
- Track -> The River Of The Dreamby Rod Album – Noises in my head Genres – Rock,Noise-Rock,Psych-Rock,Indie-Rock
Conclusion
This project showcases a straightforward console-based music player that utilizes Java Spring along with the built-in Java Sound API. It enables users to easily load and play WAV files, highlighting essential audio handling capabilities in Java. Looking ahead, you can expand this setup to incorporate additional features, such as a graphical user interface or support for various audio formats. By integrating third-party libraries, you can enhance the music player’s functionality and user experience.
Feel free to share your thoughts on bit stuffing and how it impacts data communication protocols in the comments below!
Thank You