Using Java to Realize MP3 Music Playing

Using Java to Realize MP3 Music Playing

The API that comes with Java SE lacks support for MP3 format audio files, and a third-party library is required to use Java code to play MP3. JLayer-MP3 library is an open source MP3-supporting SPI (Service Provider Interface, Service Provider Interface) support library written in Java language, commonly known as javazoom.jl package (JLayer package).

insert image description here

JLayer-MP3 library contains multiple MP3 players that can play MP3 directly:

  1. Standard player class: The javazoom.jl.player.Player class is a standard player class provided by JLayer. The use of this class is very simple. When creating a player object, the file input stream of the MP3 file is used as a parameter, and then the player is executed. The play() method to play.

  2. Minimalist player: The javazoom.jl.player.jlp class is a minimalist player that only provides a play() method for playing MP3 audio.

  3. Simple player: javazoom.jl.player.advanced.jlap class is a simplified version of the advanced player, but its function is more powerful than the Player player, adding some control functions.

  4. Advanced Player: The javazoom.jl.player.advanced.AdvancedPlayer class is the most flexible and full-featured player class. This class provides many methods for various controls during playback.

    However, when writing an MP3 player program, you need to add the JLayer-MP3 library support library in the build path, which contains three jar packages: jl1.0.jar, mp3spi1.9.4.jar, and tritonus_share.jar, which we give
    insert image description here
    below The routine, a simple demonstration of playing MP3 audio with standard players and advanced players, for your reference:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
import javazoom.jl.player.advanced.AdvancedPlayer;
public class Mp3PlayTest {
	/**JLayer提供的AdvancedPlayer类是最灵活、功能最全的播放器类。
	 * 该类提供了很多方法**/
	public static void Mp3AdvancedPlayer(String path) {
		try {
			FileInputStream fIn = new FileInputStream((path));
			BufferedInputStream bis = new BufferedInputStream(fIn);
			AdvancedPlayer player = new AdvancedPlayer(bis);
			player.play(); //播放音频文件
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**  javazoom.jl.player.Player类是JLayer提供的标准播放器类,
	 * 该类的使用方法非常简单,在创建播放器对象时将MP3文件的文件输入流当做参数,
	 * 然后执行播放器的play()方法即可播放。
	 * **/
	public static void Mp3Player(String path) {
		try {
			Player player = new Player(new FileInputStream(path));
			player.play();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public static void main(String[] args) {
		String filePath = "D:\\test\\music\\三步舞曲.mp3"; 
		System.out.println("\n高级播放器,开始播放:" + filePath);
		Mp3AdvancedPlayer(filePath);
		System.out.println("\n标准播放器类,开始播放:" + filePath);
		String path = "D:\\test\\花心.mp3";
		Mp3Player(path);
	}
}

For a more detailed introduction to the JLayer-MP3 library, please refer to:
Java Plays MP3 - JLayer's blog link

Guess you like

Origin blog.csdn.net/weixin_42369079/article/details/125091292