play audio

This commit is contained in:
BA7LZD 2021-08-25 01:14:02 +08:00
parent 7801ac2afd
commit a2b273d8ff
5 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="jl1.0">
<CLASSES>
<root url="jar://$PROJECT_DIR$/src/bin/jl1.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -7,5 +7,6 @@
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jl1.0" level="project" />
</component> </component>
</module> </module>

BIN
src/bin/jl1.0.jar Normal file

Binary file not shown.

View File

@ -0,0 +1,60 @@
package com.yuxihan;
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("D:\\Dev\\IdeaProjects\\FunningChicken\\src\\resources\\16298209674952.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("D:\\Dev\\IdeaProjects\\FunningChicken\\src\\resources\\16298209674952.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();
}
}

Binary file not shown.