“Java 播放本地音频”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第25行: | 第25行: | ||
} | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === 示例2:同步播放 === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | import javax.sound.sampled.*; | ||
+ | import java.io.File; | ||
+ | import java.io.IOException; | ||
+ | |||
+ | public class AudioPlayer implements LineListener { | ||
+ | /** | ||
+ | * this flag indicates whether the playback completes or not. | ||
+ | */ | ||
+ | boolean playCompleted; | ||
+ | |||
+ | /** | ||
+ | * Play a given audio file. | ||
+ | * | ||
+ | * @param audioFilePath Path of the audio file. | ||
+ | */ | ||
+ | void play(String audioFilePath) { | ||
+ | |||
+ | File audioFile = new File(audioFilePath); | ||
+ | |||
+ | try { | ||
+ | AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); | ||
+ | |||
+ | AudioFormat format = audioStream.getFormat(); | ||
+ | |||
+ | DataLine.Info info = new DataLine.Info(Clip.class, format); | ||
+ | |||
+ | Clip audioClip = (Clip) AudioSystem.getLine(info); | ||
+ | |||
+ | audioClip.addLineListener(this); | ||
+ | |||
+ | audioClip.open(audioStream); | ||
+ | |||
+ | audioClip.start(); | ||
+ | |||
+ | while (!playCompleted) { | ||
+ | // wait for the playback completes | ||
+ | try { | ||
+ | Thread.sleep(10); | ||
+ | } catch (InterruptedException ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | audioClip.close(); | ||
+ | |||
+ | } catch (UnsupportedAudioFileException ex) { | ||
+ | System.out.println("The specified audio file is not supported."); | ||
+ | ex.printStackTrace(); | ||
+ | } catch (LineUnavailableException ex) { | ||
+ | System.out.println("Audio line for playing back is unavailable."); | ||
+ | ex.printStackTrace(); | ||
+ | } catch (IOException ex) { | ||
+ | System.out.println("Error playing the audio file."); | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Listens to the START and STOP events of the audio line. | ||
+ | */ | ||
+ | @Override | ||
+ | public void update(LineEvent event) { | ||
+ | LineEvent.Type type = event.getType(); | ||
+ | |||
+ | if (type == LineEvent.Type.START) { | ||
+ | System.out.println("Playback started."); | ||
+ | |||
+ | } else if (type == LineEvent.Type.STOP) { | ||
+ | playCompleted = true; | ||
+ | System.out.println("Playback completed."); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | public class AudioPlayerTest { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | String pathname = "E:\\record\\2022\\11\\21\\%s.wav"; | ||
+ | for (int i = 1; i <= 10; i++) { | ||
+ | new AudioPlayer().play(String.format(pathname, i + "")); | ||
+ | } | ||
+ | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
2022年11月24日 (四) 16:19的版本
注意:只支持 wav 音频文件
import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.concurrent.TimeUnit;
public class MusicPlayer {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
String pathname = "I:\\Program Files (x86)\\MediaEditor\\AudioFiles\\音效\\动物世界\\猫.wav";
File f = new File(pathname);
URI uri = f.toURI();
AudioClip audioClip = Applet.newAudioClip(uri.toURL());
audioClip.play();
TimeUnit.SECONDS.sleep(5);
}
}
示例2:同步播放
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
public class AudioPlayer implements LineListener {
/**
* this flag indicates whether the playback completes or not.
*/
boolean playCompleted;
/**
* Play a given audio file.
*
* @param audioFilePath Path of the audio file.
*/
void play(String audioFilePath) {
File audioFile = new File(audioFilePath);
try {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
AudioFormat format = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this);
audioClip.open(audioStream);
audioClip.start();
while (!playCompleted) {
// wait for the playback completes
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
audioClip.close();
} catch (UnsupportedAudioFileException ex) {
System.out.println("The specified audio file is not supported.");
ex.printStackTrace();
} catch (LineUnavailableException ex) {
System.out.println("Audio line for playing back is unavailable.");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Error playing the audio file.");
ex.printStackTrace();
}
}
/**
* Listens to the START and STOP events of the audio line.
*/
@Override
public void update(LineEvent event) {
LineEvent.Type type = event.getType();
if (type == LineEvent.Type.START) {
System.out.println("Playback started.");
} else if (type == LineEvent.Type.STOP) {
playCompleted = true;
System.out.println("Playback completed.");
}
}
}
public class AudioPlayerTest {
public static void main(String[] args) {
String pathname = "E:\\record\\2022\\11\\21\\%s.wav";
for (int i = 1; i <= 10; i++) {
new AudioPlayer().play(String.format(pathname, i + ""));
}
}
}