Java programming method for obtaining mp3 duration and playing mp3 files, javamp3
This article describes how to obtain mp3 duration and play mp3 files by Java programming. We will share this with you for your reference. The details are as follows:
The required packages are jaudiotagger-2.2.6-SNAPSHOT.jar and jl1.0.1.jar.
Import java. io. bufferedInputStream; import java. io. file; import java. io. fileInputStream; import org. jaudiotagger. audio. audioFileIO; import org.jaudiotagger.audio.pdf. MP3AudioHeader; import org.jaudiotagger.audio.pdf. MP3File; import javazoom. jl. player. player; public class MusicUtil {public static void play (String position) {try {BufferedInputStream buffer = new BufferedInputStream (new FileInputStream (position); Player player = new Player (buffer ); player. play ();} catch (Exception e) {e. printStackTrace () ;}} public static int getDuration (String position) {int length = 0; try {MP3File mp3File = (MP3File) AudioFileIO. read (new File (position); MP3AudioHeader audioHeader = (MP3AudioHeader) mp3File. getAudioHeader (); // The unit is second length = audioHeader. getTrackLength (); return length;} catch (Exception e) {e. printStackTrace ();} return length;} public static void main (String [] args) {String position = "escape plan-the brightest star node in the night sky"; getDuration (position); play (position );}}
For more java-related content, refer to this topic: java image operation skills summary, java Date and Time Operation Skills summary, Java operation DOM node skills summary, Java file and directory operation skills summary, and Java data structure and algorithm tutorial.
I hope this article will help you with java programming.