java程序如何播放声音

java程序如何播放声音,第1张

package login;
import javaioFile;
import javaioIOException;
import javaxsoundsampledAudioFormat;
import javaxsoundsampledAudioInputStream;
import javaxsoundsampledAudioSystem;
import javaxsoundsampledDataLine;
import javaxsoundsampledFloatControl;
import javaxsoundsampledLineUnavailableException;
import javaxsoundsampledSourceDataLine;
import javaxsoundsampledUnsupportedAudioFileException;

public class testviode extends Thread {

private String filename;

private Position curPosition;

private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb

enum Position {
LEFT, RIGHT, NORMAL
};

public testviode(String wavfile) {
filename = wavfile;
curPosition = PositionNORMAL;
}

public testviode(String wavfile, Position p) {
filename = wavfile;
curPosition = p;
}

public void run() {

File soundFile = new File(filename);
if (!soundFileexists()) {
Systemerrprintln("Wave file not found: " + filename);
return;
}

AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystemgetAudioInputStream(soundFile);
} catch (UnsupportedAudioFileException e1) {
e1printStackTrace();
return;
} catch (IOException e1) {
e1printStackTrace();
return;
}

AudioFormat format = audioInputStreamgetFormat();
SourceDataLine auline = null;
DataLineInfo info = new DataLineInfo(SourceDataLineclass, format);

try {
auline = (SourceDataLine) AudioSystemgetLine(info);
aulineopen(format);
} catch (LineUnavailableException e) {
eprintStackTrace();
return;
} catch (Exception e) {
eprintStackTrace();
return;
}

if (aulineisControlSupported(FloatControlTypePAN)) {
FloatControl pan = (FloatControl) auline
getControl(FloatControlTypePAN);
if (curPosition == PositionRIGHT)
pansetValue(10f);
else if (curPosition == PositionLEFT)
pansetValue(-10f);
}

aulinestart();
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

try {
while (nBytesRead != -1) {
nBytesRead = audioInputStreamread(abData, 0, abDatalength);
if (nBytesRead >= 0)
aulinewrite(abData, 0, nBytesRead);
}
} catch (IOException e) {
eprintStackTrace();
return;
} finally {
aulinedrain();
aulineclose();
}
}}

去下载一个你喜欢的游戏的jar格式压缩文件,不要解压哦!用鼠标双击进入压缩文件,如果电脑已经安装了千千静听或其它影音软件,这个你会看到很多文件,如果是声音文件或铃声,这时文件图标会是一个铃声图标,与其他文件不同,如果你想换掉这个声音,那么你先准备一下一比较喜欢的声音,格式要与压缩文件里得音频文件一样哦,这时你只要把你准备音频文件重命名成与压缩文件内你想换掉的音频文件名一样,然后用鼠标将文件拖进压缩包,电脑经过压缩替代之后,你把它放到手机并安装就可以了。

public class Test {
public static void main(String[] args) {
File file = new File("c:/我的文档/音乐");
File[] files = filelistFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
int i = namelastIndexOf("");
name = namesubstring(i);
if (nameequalsIgnoreCase("flv"))//根据格式自己判断
return true;
else
return false;
}
});
List<File> list =new ArrayList<File>();
for (int i = 0; i < fileslength; i++) {
File f = new File(filegetAbsolutePath() + "/" + files[i]getName());
listadd(f);
Systemoutprintln(fgetName());
}
}
}//播放器 自己找找看 有了file 就好办了吧

直接下个JMF,google搜,sun官网上有~~然后安装目录是你的JDK,
再播放MP3文件就:
import javaxmedia;
import javanetMalformedURLException;
import javanetURL;
Player player;
File playFile=new File("");//你的MP3文件
try {
player=ManagercreateRealizedPlayer(playFiletoURL());
playerprefetch();
playersetMediaTime(new Time(100));
playerstart();
} catch (NoPlayerException e1)
{
e1printStackTrace();
} catch (CannotRealizeException e1)
{
e1printStackTrace();
} catch (MalformedURLException e1)
{
e1printStackTrace();
} catch (IOException e1)
{
e1printStackTrace();
}
这种基本代码,自己搜一下,用编译器熟悉熟悉JMF的方法就会了。。。我以前回答别人时的答案~~~


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13378178.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-24
下一篇 2023-07-24

发表评论

登录后才能评论

评论列表(0条)

保存