天天看點

Java 播放MP31 使用官方的解決方案2 使用第三方解決方案 (jl1.0.jar)3 使用第三方解決方案 (jmp123.jar)4 使用自定義解決方案(推薦)5 使用第三方解決方案 (mp3spi.jar)6 使用第三方解決方案 (jaudiotagger.jar)7 制作一個 Java 音樂播放器

Java 播放MP3 的六種方法

  • 1 使用官方的解決方案
  • 2 使用第三方解決方案 (jl1.0.jar)
  • 3 使用第三方解決方案 (jmp123.jar)
  • 4 使用自定義解決方案(推薦)
    • 4.1 依賴引用
    • 4.2 播放時長
    • 4.3 全部代碼
  • 5 使用第三方解決方案 (mp3spi.jar)
    • 5.1 依賴引用
    • 5.2 測試代碼
  • 6 使用第三方解決方案 (jaudiotagger.jar)
    • 6.1 添加依賴
    • 6.2 測試代碼
  • 7 制作一個 Java 音樂播放器

java sound api(JDK)原生支 .wav .au .aiff 這些格式的音頻檔案,當然 PCM(Pulse Code Modulation----脈沖編碼調制)檔案也是可以直接播放的,如果是 mp3,ogg,ape,flac 則需要第三方 jar 。

音頻格式 添加依賴
*.wav *.au *.aiff java sound api(JDK原生支援)
*.mp3 *.mp4 *.ogg *.flac *.wav *.aif *.dsf *.wma (擷取音頻标簽) jaudiotagger.jar
*.mp3 mp3spi.jar
*.mp3 jl1.0.jar
*.mp3 jmp123.jar
*.flac jflac.jar

1 使用官方的解決方案

方法1:使用官方開發的 JMF (Java Media Framework),現在JMF已經不再更新了。如果使用方法1,你必須安裝好 JMF 軟體并導入JMF安裝目錄下對應的jar包。

音頻格式 添加依賴
*.mp3 jmf 安裝目錄的 jar 包
package com.xu.musicplayer.player;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;

/**
 * Java 播放音頻
 * @ClassName: MusicPlayer   
 * @Description: TODO   
 * @author: hyacinth
 * @date: 2019年8月9日 上午12:10:53     
 * @Copyright: hyacinth
 */
public class MusicPlayer {
	
	public static void main(String[] args) throws NoPlayerException, MalformedURLException, IOException {
		File file = new  File("F:\\KuGou\\張婉清、童英然、薛之謙、黃雲龍 - 醜八怪.mp3");
		Player player = Manager.createPlayer(file.toURL());
		player.start();//開始播放
	}
	
}

           

2 使用第三方解決方案 (jl1.0.jar)

2 使用第三方jar包 jl1.0.jar。

音頻格式 添加依賴
*.mp3 jl1.0.jar
package com.xu.musicplayer.player;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

/**
 * Java 播放音頻
 * @ClassName: MusicPlayer   
 * @Description: TODO   
 * @author: hyacinth
 * @date: 2020年3月5日 上午12:10:53     
 * @Copyright: hyacinth
 */
public class MusicPlayer {
	
	
	static Player player = null;

	public static void main(String[] args) throws FileNotFoundException, JavaLayerException {
		File file = new File("C:\\Users\\hyacinth\\Desktop\\Work\\花涵 - 假行僧.mp3");
		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream stream = new BufferedInputStream(fis);
		Player player = new Player(stream);
		player.play();
	}

	/**
	 * 播放 20 秒并結束播放
	 */
	public void play() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					File file = new File("C:\\Users\\hyacinth\\Desktop\\Work\\花涵 - 假行僧.mp3");
					FileInputStream fis = new FileInputStream(file);
					BufferedInputStream stream = new BufferedInputStream(fis);
					player = new Player(stream);
					player.play();
				} catch (Exception e) {

					// TODO: handle exception
				}
			}
		}).start(); 
		try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		player.close();
	}

	
}

           

3 使用第三方解決方案 (jmp123.jar)

3 使用第三方jar包 jmp123.jar。CSDN jmp

音頻格式 添加依賴
*.mp3 jmp123.jar
package com.xu.musicplayer.player;

import java.io.File;
import java.io.IOException;

import jmp123.output.Audio;

/**
 * Java 播放音頻
 * @ClassName: MusicPlayer   
 * @Description: TODO   
 * @author: hyacinth
 * @date: 2019年8月9日 上午12:10:53     
 * @Copyright: hyacinth
 */
public class MusicPlayer {

public static void main(String[] args) {
		String path="G:\\KuGou\\夢涵 - 愛的故事上集.mp3";
		MiniPlayer player = new MiniPlayer(new Audio());
		try {
			long t0 = System.nanoTime();
			String msg = player.open(path);
			long t1 = System.nanoTime() - t0;
			File file = new File(path);
			long length = file.length();
			int frames = player.getFrameCount();
			System.out.println(msg);
			System.out.printf("      length: %d bytes, %d frames\n", length, frames);
			System.out.printf("elapsed time: %,dns (%.9fs, %.2f fps)\n", t1, t1/1e9, frames/(t1/1e9));
			player.run();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

           

4 使用自定義解決方案(推薦)

4 自定義通過擷取MP3的PCM播放MP3,需要導入Google的一個jar包,可以擷取音頻的播放時長。

音頻格式 添加依賴
*.wav 無 (JDK 原生支援)
*.pcm 無 (JDK 原生支援)
*.au 無 (JDK 原生支援)
*.aiff 無 (JDK 原生支援)
*.mp3 mp3spi.jar
*.flac jflac-codec.jar

4.1 依賴引用

<dependency>
	<groupId>com.googlecode.soundlibs</groupId>
	<artifactId>mp3spi</artifactId>
	<version>1.9.5.4</version>
</dependency>

<!-- 如果需要解碼播放flac檔案則引入這個jar包 -->
<dependency>
	<groupId>org.jflac</groupId>
	<artifactId>jflac-codec</artifactId>
	<version>1.5.2</version>
</dependency>

           

4.2 播放時長

// float 類型 秒
AudioSystem.getAudioInputStream(new File("")).getFrameLength()/AudioSystem.getAudioInputStream(new File("")).getFormat().getSampleRate()

           
/**
 * Java Music 擷取音頻檔案資訊
 * @Title: get_info
 * @Description: 擷取音頻檔案資訊
 * @param path 音頻檔案路徑
 * @return 音頻檔案資訊
 * @date 2019年10月25日 下午12:28:41
 */
public String get_info(String path) {
	File file=new File(path);
	AudioInputStream ais;
	String result="";
	try {
		ais = AudioSystem.getAudioInputStream(file);
		AudioFormat af = ais.getFormat();
		result = af.toString();
		System.out.println(result);
		System.out.println("音頻總幀數:"+ais.getFrameLength());
		System.out.println("每秒播放幀數:"+af.getSampleRate());
		float len = ais.getFrameLength()/af.getSampleRate();
		System.out.println("音頻時長(秒):"+len);
		System.out.println("音頻時長:"+(int)len/60+"分"+(int)len%60+"秒");
	} catch(UnsupportedAudioFileException e) {
		throw new RuntimeException(e.getMessage());
	} catch(IOException e) {
		throw new RuntimeException(e.getMessage());
	}
	return result;
}

           

4.3 全部代碼

package com.xu.music.player;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;

import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.id3.AbstractID3v2Frame;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC;

/**
 * Java 播放音頻
 *
 * @ClassName: MusicPlayer
 * @Description: TODO
 * @author: hyacinth
 * @date: 2019年10月25日 下午12:28:41
 * @Copyright: hyacinth
 */
public class MusicPlayer {

    public static void main(String[] args) throws Exception {
        MusicPlayer player = new MusicPlayer();
        //player.mp3_to_pcm("C:\\Users\\Administrator\\Desktop\\夢涵 - 加減乘除.mp3", "C:\\Users\\Administrator\\Desktop\\夢涵 - 加減乘除.pcm");
        //player.wav_to_pcm("C:\\Users\\Administrator\\Desktop\\夢涵 - 加減乘除.wav", "C:\\Users\\Administrator\\Desktop\\夢涵 - 加減乘除.pcm");

        //player.get_info("C:\\Users\\Administrator\\Desktop\\夢涵 - 加減乘除.pcm");
        //player.get_info("C:\\Users\\Administrator\\Desktop\\夢涵 - 加減乘除.wav");

        //player.play_pcm("C:\\Users\\Administrator\\Desktop\\夢涵 - 加減乘除.pcm");
        //player.play_wav("C:\\Users\\Administrator\\Desktop\\夢涵 - 加減乘除.wav");
        player.play_mp3("G:\\KuGou\\丸子呦 - 廣寒宮.mp3");
        //player.play_flac("C:\\Users\\Administrator\\Desktop\\夢涵 - 加減乘除.flac");
    }

    /**
     * Java Music 播放 flac
     *
     * @param path flac檔案路徑
     * @throws IOException
     * @throws UnsupportedAudioFileException
     * @Title: play_flac
     * @Description: 播放 flac
     * @date 2019年10月25日 下午12:28:41
     */
    public void play_flac(String path) throws UnsupportedAudioFileException, IOException {
        File file = new File(path);
        if (!file.exists() || !path.toLowerCase().endsWith(".flac")) {
            return;
        }
        AudioInputStream audio = AudioSystem.getAudioInputStream(file);
        AudioFormat format = audio.getFormat();
        if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), 16, format.getChannels(), format.getChannels() * 2, format.getSampleRate(), false);
            audio = AudioSystem.getAudioInputStream(format, audio);
        }
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);
        SourceDataLine data = null;
        try {
            data = (SourceDataLine) AudioSystem.getLine(info);
            data.open(format);
            data.start();
            byte[] bt = new byte[1024];
            while (audio.read(bt) != -1) {
                data.write(bt, 0, bt.length);
            }
            data.drain();
            data.stop();
            data.close();
            audio.close();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * Java Music 播放 wav
     *
     * @param path wav 檔案路徑
     * @throws IOException
     * @throws UnsupportedAudioFileException
     * @Title: play_wav
     * @Description: 播放 wav
     * @date 2019年10月25日 下午12:28:41
     */
    public void play_wav(String path) throws UnsupportedAudioFileException, IOException {
        File file = new File(path);
        if (!file.exists() || !path.toLowerCase().endsWith(".wav")) {
            throw new RuntimeException("檔案不存在");
        }
        AudioInputStream stream = AudioSystem.getAudioInputStream(file);
        AudioFormat target = stream.getFormat();
        DataLine.Info dinfo = new DataLine.Info(SourceDataLine.class, target);
        SourceDataLine line = null;
        int len = -1;
        try {
            line = (SourceDataLine) AudioSystem.getLine(dinfo);
            line.open(target);
            line.start();
            byte[] buffer = new byte[1024];
            while ((len = stream.read(buffer)) > 0) {
                line.write(buffer, 0, len);
            }
            line.drain();
            line.stop();
            line.close();
            stream.close();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * Java Music 播放 pcm
     *
     * @param path pcm檔案路徑
     * @throws IOException
     * @throws UnsupportedAudioFileException
     * @Title: play_pcm
     * @Description: 播放 pcm
     * @date 2019年10月25日 下午12:28:41
     */
    public void play_pcm(String path) throws UnsupportedAudioFileException, IOException {
        File file = new File(path);
        if (!file.exists() || !path.toLowerCase().endsWith(".pcm")) {
            throw new RuntimeException("檔案不存在");
        }
        AudioInputStream stream = AudioSystem.getAudioInputStream(file);
        AudioFormat target = stream.getFormat();
        DataLine.Info dinfo = new DataLine.Info(SourceDataLine.class, target, AudioSystem.NOT_SPECIFIED);
        SourceDataLine line = null;
        int len = -1;
        try {
            line = (SourceDataLine) AudioSystem.getLine(dinfo);
            line.open(target);
            line.start();
            byte[] buffer = new byte[1024];
            while ((len = stream.read(buffer)) > 0) {
                line.write(buffer, 0, len);
            }
            line.drain();
            line.stop();
            line.close();
            stream.close();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * Java Music 播放 mp3
     *
     * @param path mp3檔案路徑
     * @throws IOException
     * @throws UnsupportedAudioFileException
     * @Title: play_mp3
     * @Description: 播放 mp3
     * @date 2019年10月25日 下午12:28:41
     */
    public void play_mp3(String path) throws UnsupportedAudioFileException, IOException {
        File file = new File(path);
        if (!file.exists() || !path.toLowerCase().endsWith(".mp3")) {
            throw new RuntimeException("檔案不存在");
        }
        AudioInputStream stream = null;
        //使用 mp3spi 解碼 mp3 音頻檔案
        MpegAudioFileReader mp = new MpegAudioFileReader();
        stream = mp.getAudioInputStream(file);
        AudioFormat baseFormat = stream.getFormat();
        //設定輸出格式為pcm格式的音頻檔案
        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
        // 輸出到音頻
        stream = AudioSystem.getAudioInputStream(format, stream);
        AudioFormat target = stream.getFormat();
        DataLine.Info dinfo = new DataLine.Info(SourceDataLine.class, target, AudioSystem.NOT_SPECIFIED);
        SourceDataLine line = null;
        int len = -1;
        try {
            line = (SourceDataLine) AudioSystem.getLine(dinfo);
            line.open(target);
            line.start();
            byte[] buffer = new byte[1024];
            while ((len = stream.read(buffer)) > 0) {
                line.write(buffer, 0, len);
            }
            line.drain();
            line.stop();
            line.close();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            stream.close();
        }
    }

    /**
     * Java Music 讀取pcm檔案
     *
     * @param path pcm檔案路徑
     * @return AudioInputStream
     * @Title: read_pcm
     * @Description: 讀取pcm檔案
     * @date 2019年10月25日 下午12:28:41
     */
    public AudioInputStream read_pcm(String path) throws UnsupportedAudioFileException, IOException {
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        AudioInputStream stream = AudioSystem.getAudioInputStream(file);
        AudioFormat format = stream.getFormat();
        System.out.println(format.toString());
        return stream;
    }

    /**
     * Java Music 擷取 mp3 脈沖編碼調制
     *
     * @param rpath mp3檔案路徑
     * @param spath pcm檔案儲存路徑
     * @return AudioInputStream
     * @Title: get_pcm_from_mp3
     * @Description: 擷取 mp3 脈沖編碼調制
     * @date 2019年10月25日 下午12:28:41
     */
    public void get_pcm_from_mp3(String rpath, String spath) {
        File file = new File(rpath);
        if (!file.exists()) {
            return;
        }
        AudioInputStream stream = null;
        AudioFormat format = null;
        try {
            AudioInputStream in = null;
            //讀取音頻檔案的類
            MpegAudioFileReader mp = new MpegAudioFileReader();
            in = mp.getAudioInputStream(file);
            AudioFormat baseFormat = in.getFormat();
            //設定輸出格式為pcm格式的音頻檔案
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
            //輸出到音頻
            stream = AudioSystem.getAudioInputStream(format, in);
            AudioSystem.write(stream, AudioFileFormat.Type.WAVE, new File(spath));
            stream.close();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * Java Music mp3 轉 pcm
     *
     * @param rpath MP3檔案路徑
     * @param spath PCM檔案儲存路徑
     * @return AudioInputStream
     * @Title: mp3_to_pcm
     * @Description: MP3 PCM
     * @date 2019年10月25日 下午12:28:41
     */
    public void mp3_to_pcm(String rpath, String spath) {
        File file = new File(rpath);
        if (!file.exists()) {
            return;
        }
        AudioInputStream stream = null;
        AudioFormat format = null;
        try {
            AudioInputStream in = null;
            //讀取音頻檔案的類
            MpegAudioFileReader mp = new MpegAudioFileReader();
            in = mp.getAudioInputStream(file);
            AudioFormat baseFormat = in.getFormat();
            //設定輸出格式為pcm格式的音頻檔案
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
            //輸出到音頻
            stream = AudioSystem.getAudioInputStream(format, in);
            AudioSystem.write(stream, AudioFileFormat.Type.WAVE, new File(spath));
            stream.close();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * Java Music wav轉pcm
     *
     * @param wpath wav檔案路徑
     * @param ppath pcm檔案儲存路徑
     * @return AudioInputStream
     * @throws IOException
     * @throws UnsupportedAudioFileException
     * @Title: wav_to_pcm
     * @Description: wav轉pcm
     * @date 2019年10月25日 下午12:28:41
     */
    public void wav_to_pcm(String wpath, String ppath) {
        File file = new File(wpath);
        if (!file.exists()) {
            throw new RuntimeException("檔案不存在");
        }
        AudioInputStream stream1;
        try {
            stream1 = AudioSystem.getAudioInputStream(file);
            // 根據實際情況修改 AudioFormat.Encoding.PCM_SIGNED
            AudioInputStream stream2 = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, stream1);
            AudioSystem.write(stream2, AudioFileFormat.Type.WAVE, new File(ppath));
            stream2.close();
            stream1.close();
        } catch (UnsupportedAudioFileException | IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * Java Music PCM轉WAV
     *
     * @param wpath WAV檔案路徑
     * @param ppath PCM檔案儲存路徑
     * @return AudioInputStream
     * @throws IOException
     * @throws UnsupportedAudioFileException
     * @Title: pcm_to_wav
     * @Description: PCM轉WAV
     * @date 2019年10月25日 下午12:28:41
     */
    public void pcm_to_wav(String ppath, String wpath) {

    }

    /**
     * Java Music 擷取wav或者pcm檔案的編碼資訊
     *
     * @param path wav或者pcm檔案路徑
     * @return wav或者pcm檔案的編碼資訊
     * @Title: get_info
     * @Description: 擷取wav或者pcm檔案的編碼資訊
     * @date 2019年10月25日 下午12:28:41
     */
    public String get_info(String path) {
        File file = new File(path);
        AudioInputStream ais;
        String result = "";
        try {
            ais = AudioSystem.getAudioInputStream(file);
            AudioFormat af = ais.getFormat();
            result = af.toString();
            System.out.println(result);
            System.out.println("音頻總幀數:" + ais.getFrameLength());
            System.out.println("每秒播放幀數:" + af.getSampleRate());
            float len = ais.getFrameLength() / af.getSampleRate();
            System.out.println("音頻時長(秒):" + len);
            System.out.println("音頻時長:" + (int) len / 60 + "分" + len % 60 + "秒");
        } catch (UnsupportedAudioFileException e) {
            throw new RuntimeException(e.getMessage());
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    /**
     * Java Music 擷取mp3檔案的圖檔
     *
     * @param mpath mp3flac檔案路徑
     * @Title: get_image_from_mp3
     * @Description: 擷取mp3檔案的圖檔
     * @date 2019年10月25日 下午12:28:41
     */
    public void get_image_from_mp3(String mpath) throws IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
        File sourceFile = new File(mpath);
        MP3File mp3file = new MP3File(sourceFile);
        AbstractID3v2Tag tag = mp3file.getID3v2Tag();
        AbstractID3v2Frame frame = (AbstractID3v2Frame) tag.getFrame("APIC");
        FrameBodyAPIC body = (FrameBodyAPIC) frame.getBody();
        byte[] image = body.getImageData();
        Image img = Toolkit.getDefaultToolkit().createImage(image, 0, image.length);
        ImageIcon icon = new ImageIcon(img);
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\夢涵 - 加減乘除.jpg");
        fos.write(image);
        fos.close();
        System.out.println("width:" + icon.getIconWidth());
        System.out.println("height:" + icon.getIconHeight());
    }

}
           

5 使用第三方解決方案 (mp3spi.jar)

5 使用 Google 的 mp3spi 播放MP3。添加 mp3spi 的 maven 依賴。

音頻格式 添加依賴
*.mp3 mp3spi.jar

5.1 依賴引用

<dependency>
	<groupId>com.googlecode.soundlibs</groupId>
	<artifactId>mp3spi</artifactId>
	<version>1.9.5.4</version>
</dependency>
           

5.2 測試代碼

/**  
 * 
 * @Author: hyacinth
 * @Title: MusicPlayer.java   
 * @Package com.xu.test   
 * @Description: TODO: 
 * @Date: 2019年8月25日 下午10:40:47   
 * @Version V-1.0 
 * @Copyright: 2019 hyacinth
 * 
 */  
package com.xu.test;

import java.io.File;
import java.io.FileInputStream;

import javazoom.jl.player.Player;

/** 
 * @Author: hyacinth
 * @ClassName: MusicPlayer   
 * @Description: TODO    
 * @Date: 2019年8月25日 下午10:40:47   
 * @Copyright: hyacinth
 */
public class MusicPlayer {
	
	public static void main(String[] args) throws Exception {
		File file=new File("I:\\KuGou\\許麗靜 - 昨天今天下雨天.mp3");
		FileInputStream stream=new FileInputStream(file);
		Player player=new Player(stream);
		player.play();
	}
	
}

           

6 使用第三方解決方案 (jaudiotagger.jar)

jaudiotagger.jar 本身不能播放 音頻檔案,但是能解碼 音頻檔案 中的标簽資訊。

音頻格式 添加依賴
*.mp3 *.mp4 *.ogg *.flac *.wav *.aif *.dsf *.wma (擷取音頻标簽) jaudiotagger.jar

6.1 添加依賴

<dependency>
	<groupId>org</groupId>
	<artifactId>jaudiotagger</artifactId>
	<version>2.0.3</version>
</dependency>
           

6.2 測試代碼

package com.xu.music.player;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.tag.TagException;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

public class MusicPlayer {

	public static void main(String[] args) throws Exception {
		get_music_play_length("I:\\KuGou\\許麗靜 - 昨天今天下雨天.mp3");
	}

	/**
	 * Java Music 擷取歌曲播放時長
	 * @Title: get_music_play_length
	 * @Description: 擷取歌曲播放時長
	 * @param path mp3路徑
	 * @throws InvalidAudioFrameException 
	 * @throws ReadOnlyFileException 
	 * @throws TagException 
	 * @throws IOException 
	 * @throws CannotReadException 
	 * @date 2019年10月25日 下午12:28:41
	 */
	public int get_music_play_length(String path) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
		File file=new File("C:\\Users\\Administrator\\Desktop\\夢涵 - 加減乘除.mp3");
		AudioFile mp3=AudioFileIO.read(file);
		return mp3.getAudioHeader().getTrackLength();
	}

}

           

7 制作一個 Java 音樂播放器

部落客用 Java SWT 編寫的 Java MusicPlayer

支援FFT頻譜顯示,預設不開啟FFT頻譜,喜歡的幫助點顆心。

Java MusicPlayer GitHub 位址

Java 播放MP31 使用官方的解決方案2 使用第三方解決方案 (jl1.0.jar)3 使用第三方解決方案 (jmp123.jar)4 使用自定義解決方案(推薦)5 使用第三方解決方案 (mp3spi.jar)6 使用第三方解決方案 (jaudiotagger.jar)7 制作一個 Java 音樂播放器