在本文中,我们将看到,如何在纯java中播放音频文件,这里的纯意思是,我们不使用任何外部库。通过本文的帮助,您可以创建自己的音乐播放器。Java内置库只支持AIFC、AIFF、AU、SND和WAVE格式。 有两种不同的接口可用于此目的Clip和SourceDataLine。在本文中,我们将讨论仅使用Clip播放音频文件,并了解Clip的各种方法。我们将涵盖以下操作:
null
- 开始
- 暂停
- 简历
- 重新启动。
- 停止
- 跳转到播放的特定位置。
使用剪辑播放音频
Clip是javax中可用的java接口。声音样本包,并在Java7中引入。 要播放剪辑对象,请遵循以下步骤。
- 使用AudioSystem创建AudioInputStream的对象。getAudioInputStream(文件)。AudioInputStream将音频文件转换为流。
- 从AudioSystem获取剪辑参考对象。
- Stream音频输入流,使用clip interface的open()方法将音频数据从中读取到剪辑中。
- 将所需属性设置为剪辑样帧位置、循环、微秒位置。
- 开始剪辑
// Java program to play an Audio // file using Clip Object import java.io.File; import java.io.IOException; import java.util.Scanner; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; public class SimpleAudioPlayer { // to store current position Long currentFrame; Clip clip; // current status of clip String status; AudioInputStream audioInputStream; static String filePath; // constructor to initialize streams and clip public SimpleAudioPlayer() throws UnsupportedAudioFileException, IOException, LineUnavailableException { // create AudioInputStream object audioInputStream = AudioSystem.getAudioInputStream( new File(filePath).getAbsoluteFile()); // create clip reference clip = AudioSystem.getClip(); // open audioInputStream to the clip clip.open(audioInputStream); clip.loop(Clip.LOOP_CONTINUOUSLY); } public static void main(String[] args) { try { filePath = "Your path for the file" ; SimpleAudioPlayer audioPlayer = new SimpleAudioPlayer(); audioPlayer.play(); Scanner sc = new Scanner(System.in); while ( true ) { System.out.println( "1. pause" ); System.out.println( "2. resume" ); System.out.println( "3. restart" ); System.out.println( "4. stop" ); System.out.println( "5. Jump to specific time" ); int c = sc.nextInt(); audioPlayer.gotoChoice(c); if (c == 4 ) break ; } sc.close(); } catch (Exception ex) { System.out.println( "Error with playing sound." ); ex.printStackTrace(); } } // Work as the user enters his choice private void gotoChoice( int c) throws IOException, LineUnavailableException, UnsupportedAudioFileException { switch (c) { case 1 : pause(); break ; case 2 : resumeAudio(); break ; case 3 : restart(); break ; case 4 : stop(); break ; case 5 : System.out.println( "Enter time (" + 0 + ", " + clip.getMicrosecondLength() + ")" ); Scanner sc = new Scanner(System.in); long c1 = sc.nextLong(); jump(c1); break ; } } // Method to play the audio public void play() { //start the clip clip.start(); status = "play" ; } // Method to pause the audio public void pause() { if (status.equals( "paused" )) { System.out.println( "audio is already paused" ); return ; } this .currentFrame = this .clip.getMicrosecondPosition(); clip.stop(); status = "paused" ; } // Method to resume the audio public void resumeAudio() throws UnsupportedAudioFileException, IOException, LineUnavailableException { if (status.equals( "play" )) { System.out.println( "Audio is already " + "being played" ); return ; } clip.close(); resetAudioStream(); clip.setMicrosecondPosition(currentFrame); this .play(); } // Method to restart the audio public void restart() throws IOException, LineUnavailableException, UnsupportedAudioFileException { clip.stop(); clip.close(); resetAudioStream(); currentFrame = 0L; clip.setMicrosecondPosition( 0 ); this .play(); } // Method to stop the audio public void stop() throws UnsupportedAudioFileException, IOException, LineUnavailableException { currentFrame = 0L; clip.stop(); clip.close(); } // Method to jump over a specific part public void jump( long c) throws UnsupportedAudioFileException, IOException, LineUnavailableException { if (c > 0 && c < clip.getMicrosecondLength()) { clip.stop(); clip.close(); resetAudioStream(); currentFrame = c; clip.setMicrosecondPosition(c); this .play(); } } // Method to reset audio stream public void resetAudioStream() throws UnsupportedAudioFileException, IOException, LineUnavailableException { audioInputStream = AudioSystem.getAudioInputStream( new File(filePath).getAbsoluteFile()); clip.open(audioInputStream); clip.loop(Clip.LOOP_CONTINUOUSLY); } } |
在上面的程序中,我们使用了 音频输入流 这是Java中的一个类,用于将音频文件作为流读取。就像每一个java流一样,如果要再次使用它,它必须被重置。
- 要暂停播放,我们必须停止播放器并将当前帧存储在对象中。这样在恢复的同时我们可以使用它。当恢复比赛时,我们只需要从我们离开的最后一个位置再次与球员比赛。 夹子方法返回音频和剪辑的当前位置。设置微秒位置(长位置)设置音频的当前位置。
- 要停止播放,必须关闭剪辑,否则它将保持打开状态。我也用过clip。循环(Clip.loop_continuously)进行测试。因为wav文件通常很小,所以我循环播放了我的wav文件。
要点:
- 在退出程序之前,请始终关闭打开的流和资源。
- 在再次播放之前,您必须停止该剪辑。所以要保持适当的检查。
- 如果要再次使用AudioInputStream,必须将其重置。
本文由 维沙尔·加格 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END