Java播放MP3播放音频

Java播放MP3播放音频

下面我演示用jdk自带包、框架等分别展示播放mp3、等music

一、使用javafx包

AudioClip

注意jdk11以上剥离了javafx

    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\Administrator\\Desktop\\塞壬唱片-MSR,BaoUner - 生命流.mp3");
        URI uri = file.toURI();
        System.out.println(uri);
        // file:/C:/Users/Administrator/Desktop/塞壬唱片-MSR,BaoUner%20-%20生命流.mp3
        AudioClip audioClip = new AudioClip(uri.toString());
        audioClip.setCycleCount(99);// 播放次数
        audioClip.play();
        // 延迟,不关闭main用于播放音乐
        Thread.sleep(10000);
    }
}

Media

使用javafx加入场景模式

import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;

import java.io.File;
import java.net.URI;

/**
 * @author lingkang
 * @date 2021/12/11
 */
public class MyMusic01 extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        File file = new File("C:\\Users\\Administrator\\Desktop\\塞壬唱片-MSR,BaoUner - 生命流.mp3");
        URI uri = file.toURI();
        System.out.println(uri);

        Media media = new Media(uri.toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setAutoPlay(true);
        MediaView mediaView = new MediaView(mediaPlayer);
        
        // 可以使用按钮控制
        //mediaPlayer.play();
        primaryStage.setScene(new Scene(new AnchorPane(mediaView)));
        primaryStage.show();
    }

    public static void main(String[] args) throws Exception {
        launch(args);
    }
}

二、java自带包

AudioClip

使用java自带AudioClip 只能播放wav格式文件,其他格式无法播放

import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.URI;

/**
 * @author lingkang
 * @date 2021/12/11
 */
public class MyMusic02 {
    public static void main(String[] args) throws Exception {
	    // 只支持 wav
        File file = new File("C:\\Users\\Administrator\\Desktop\\aoUner_-_231148159229145189230181129.wav");
        URI uri = file.toURI();
        System.out.println(uri);
        // AudioClip 只能播放 wav 格式的music
        AudioClip audioClip = Applet.newAudioClip(uri.toURL());
        audioClip.play();// 独立线程播放
        //循环播放
        audioClip.loop();
    }
}

Clip

使用Clip,也是只支持wav

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.File;
import java.net.URI;

/**
 * @author lingkang
 * @date 2021/12/11
 */
public class MyMusic03 {
    public static void main(String[] args) throws Exception {
	    //只能播放 wav 格式的music
        File file = new File("C:\\Users\\Administrator\\Desktop\\aoUner_-_231148159229145189230181129.wav");
        URI uri = file.toURI();
        System.out.println(uri);
        AudioInputStream audioIn = AudioSystem.getAudioInputStream(file);
        Clip clip = AudioSystem.getClip();
        clip.open(audioIn);
        clip.start();
        Thread.sleep(15000);
    }
}

三、使用框架

jaco-mp3-player

2012年的框架
下载地址 http://jacomp3player.sourceforge.net/
只支持map3

import jaco.mp3.player.MP3Player;

import java.io.File;
import java.net.URI;

/**
 * @author lingkang
 * @date 2021/12/11
 */
public class MyMusic04 {
    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\Administrator\\Desktop\\塞壬唱片-MSR,BaoUner - 生命流.mp3");
        //File file = new File("C:\\Users\\Administrator\\Desktop\\aoUner_-_231148159229145189230181129.wav");
        URI uri = file.toURI();
        System.out.println(uri);
        // 只支持map3
        new MP3Player(file).play();
        Thread.sleep(15000);
    }
}

jlayer

最后更新2012年

<!-- https://mvnrepository.com/artifact/javazoom/jlayer -->
<dependency>
    <groupId>javazoom</groupId>
    <artifactId>jlayer</artifactId>
    <version>1.0.1</version>
</dependency>
import javazoom.jl.player.Player;

import java.io.File;
import java.io.FileInputStream;
import java.net.URI;

/**
 * @author lingkang
 * @date 2021/12/11
 */
public class MyMusic05 {
    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\Administrator\\Desktop\\塞壬唱片-MSR,BaoUner - 生命流.mp3");
        //File file = new File("C:\\Users\\Administrator\\Desktop\\aoUner_-_231148159229145189230181129.wav");
        URI uri = file.toURI();
        System.out.println(uri);
        // 只支持mp3格式
        Player player = new Player(new FileInputStream(file));
        player.play();// 线程阻塞
        System.out.println("Song is playing");
    }
}

总结

推荐使用javafx包下的AudioClip

posted @ 2022-09-16 00:08  凌康  阅读(592)  评论(0编辑  收藏  举报