In this article we will see, how can we play an audio file in pure java, here pure means, we are not going to use any external library. You can create your own music player by the help of this article. Java inbuilt libraries support only AIFC, AIFF, AU, SND and WAVE formats.
There are 2 different interfaces which can be used for this purpose Clip and SourceDataLine. In this article, we will discuss playing audio file using Clip only and see the various methods of clip. We will cover following operations:
- Jump to a specific position of playback.
Play Audio using Clip
Clip is a java interface available in javax.sound.sampled package and introduced in Java7.
Following steps are to be followed to play a clip object.
- Create an object of AudioInputStream by using AudioSystem.getAudioInputStream(File file). AudioInputStream converts an audio file into stream.
- Get a clip reference object from AudioSystem.
- Stream an audio input stream from which audio data will be read into the clip by using open() method of Clip interface.
- Set the required properties to the clip like frame position, loop, microsecond position.
- Start the clip
In above program we have used AudioInputStream which is a class in Java to read audio file as a stream. Like every stream of java if it is to be used again it has to be reset.
- To pause the playback we have to stop the player and store the current frame in an object. So that while resuming we can use it. When resuming we just have to play again the player from the last position we left.
clip.getMicrosecondPosition() method returns the current position of audio and clip.setMicrosecondPosition(long position) sets the current position of audio.
- To stop the playback, you must have to close the clip otherwise it will remain open. I have also used clip.loop(Clip.LOOP_CONTINOUSLY) for testing. Because wav files are generally small so I have played mine in a loop.
- Always close your opened stream and resources before exiting the program.
- You have to stop the clip before playing it again. So keep proper checks.
- If AudioInputStream is to be used again, it has to be reset.
This article is contributed by Vishal Garg. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- OpenCV C++ Program to play a video
- Google Play Protect : How it Detects and Removes Malicious Apps?
- Top 8 Tips to Get Your Android App Featured on Google Play Store
- Top 7 Google Play Store Tips That You Should Try
- Tips to Prepare Your Android App For Launch in Google Play Store
- How to Publish Your Android App on Google Play Store?
- How to Build Progressive Web Application and Submit it to the Play Store?
- Operations on Audio/Video files using ffmpeg, avconv, and youtube-dl
- Audio Format
- Copying file using FileStreams in Java
- File handling in Java using FileWriter and FileReader
- Delete a file using Java
- Moving a file from one directory to another using Java
- Counting number of lines, words, characters and paragraphs in a text file using Java
- Size of file on the Internet using Java
- Creating a Cell at specific position in Excel file using Java
- Reading a CSV file in Java using OpenCSV
- Writing a CSV file in Java using OpenCSV
- Creating Sheets in Excel File in Java using Apache POI