我的电脑有3个声卡。我可以选择然后使用此代码播放.wav文件
import javax.sound.sampled.*
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Mixer.Info[] arrMixerInfo = AudioSystem.getMixerInfo();
// Get a sound clip resource.
Clip clip = AudioSystem.getClip(arrMixerInfo[1]);
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
clip.drain();
clip.close();
如何播放.mp3文件?
最佳答案
这个答案来自:How to play an mp3 file in java。
我已将本答复的全部信用额提供给原始海报:Juned Ahsan
为此,您需要在PC中安装Java Media Framework (JMF)。您已经安装了一个,然后尝试以下代码:
import javax.media.*;
import java.net.*;
import java.io.*;
import java.util.*;
class AudioPlay
{
public static void main(String args[]) throws Exception
{
// Take the path of the audio file from command line
File f=new File("song.mp3");
// Create a Player object that realizes the audio
final Player p=Manager.createRealizedPlayer(f.toURI().toURL());
// Start the music
p.start();
// Create a Scanner object for taking input from cmd
Scanner s=new Scanner(System.in);
// Read a line and store it in st
String st=s.nextLine();
// If user types 's', stop the audio
if(st.equals("s"))
{
p.stop();
}
}
}
您可能会遇到无法处理formaterror的错误,这是因为Java默认情况下已取消了对MP3的支持(盗版版权问题),因此您需要安装“JMF MP3插件”才能播放MP3文件。
转到Java的JMF网站下载
http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html
为确保您使用的是受支持的格式文件,请在此处检查:
http://www.oracle.com/technetwork/java/javase/formats-138492.html
如果您使用的是Windows7,则可能还需要阅读以下内容:
https://forums.oracle.com/forums/thread.jspa?threadID=2132405&tstart=45
关于java - Java选择音频设备并播放.mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22438353/