導航:首頁 > 編程語言 > java播放wav

java播放wav

發布時間:2022-06-20 17:34:37

A. 如何播放wav文件

建議使用jmf(java media framwork),這樣就能播放mp3等眾多格式的音樂了;去sun官網下一個jmf,安裝好後,把
jmf.jar包引入便可使用,給出例zi代碼:使用方法:構造函數中傳入文件路徑名即可,播放、暫停、繼續、停止等功能均已實現。

/*************************************************
* Subclass: MusicPlay
*************************************************/
public class MusicPlay implements Runnable {
private Time zeroTime = new Time(0);
private Player player;
private boolean isloop = false;

/*************************************************
* Function: MusicPlay Description: constructor, load the music file and
* get ready for play Called By: MultiMedia()
*************************************************/
// 實例化各個參數 filename 為文件名,可為絕對路徑
public MusicPlay(String filename) {
File file = new File(filename);
try {
player = Manager.createRealizedPlayer(file.toURI().toURL());
player.addControllerListener(new ControllListener());
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (CannotRealizeException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/*************************************************
* Function: isRunning Description: test if this music is playing Called
* By:
*************************************************/
public boolean isRunning() {
return player.getState() == Player.Started;
}

/*************************************************
* Function: play Description: play the music for once Called By:
* resumeAll()
*************************************************/
// 只播放一次
public void play() {
if (!turnOff)
player.start();
}

/*************************************************
* Function: replay Description: replay the music Called By: musics that
* will be played many times will invoke this methed
*************************************************/
// 再播放一次
public void replay() {
if (turnOff)
return;

if (player.getState() == Controller.Prefetched)
player.setMediaTime(zeroTime);
player.start();
}

/*************************************************
* Function: stop Description: stop this music Called By: stopAll() of
* upper class,suspendAll() of upper
* class,BackroundForMenuPanel,GameOverPanel
*************************************************/
public void stop() {
player.stop();
}

/*************************************************
* Function: close Description: dispose the music Called By: closeAll()
* of super class
*************************************************/
public void close() {
player.stop();
player.close();
}

/*************************************************
* Function: loop Description: make the music played repetitiously
* Called By: music that will repetitious play
*************************************************/
// 循環播放
public void loop() {
if (turnOff)
return;

isloop = true;
player.prefetch();
replay();
}

/*************************************************
* Function: run Description: trig this music Called By: Override method
*************************************************/
@Override
public void run() {
loop();
}

/*************************************************
* Subclass: ControllListener Description: listener for playing and
* implement playing repetitiously
*************************************************/
// 通過對播放進度的監聽,實現循環播放
private class ControllListener implements ControllerListener {

public void controllerUpdate(ControllerEvent e) {
if (e instanceof EndOfMediaEvent) {
if (isloop) {
player.setMediaTime(new Time(0));
player.start();
}
}
}
}

}

B. java運行時,播放的背景wav音樂會停止,求助!!!

import java.applet.AudioClip;

import java.io.*;
import java.applet.Applet;
import java.awt.Frame;
import java.net.MalformedURLException;
import java.net.URL;

public class JavaClip extends Frame{

public JavaClip(){
super();
}
public static void main(String args[]) {
try {
URL cb;
File f = new File("d:\\love.wav");
cb = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(cb);
aau.loop();//循環播放 aau.play() 單曲 aau.stop()停止播放
JavaClip frame=new JavaClip();
//frame.setBounds(0, 0, 300, 200);
//frame.setVisible(true);

} catch (MalformedURLException e) {
e.printStackTrace();
}

}
}

C. java播放的音樂文件格式哪些

一般不裝插件情況下,jdk自帶類庫只能實現AU,AIFF,WAV,MIDI,RFM等格式的音頻,加了插件後才能實現mp3等。

D. java編程:AudioClip播放wav就是不出聲音啊

試試這個
InputStream is =null;
AudioStream as = null ;
is = getClass().getResourceAsStream("a.wav");
try {
as = new AudioStream(is);
} catch (IOException e) {}
AudioPlayer.player.start(as);

E. java播放WAV文件找不到import sun.audio.AudioPlayer;

jdk裡面沒有sun.audio.AudioPlayer這個東東,是三方包的話,如果沒設定好環境變數,沒將這東西放到環境變數裡面,當然找不到了。

WAV用jdk包javax.sound.sampled裡面的東西就可以了。參考例子:
http://hi..com/breezedancer/blog/item/7eebb499680d8f086e068cb9.html

F. 求一個簡單java播放wav音頻程序

/**
* 播放音樂
*
* @param mp3Path
* @param repeat
*/
public static void playSound(final String mp3Path, final int repeat) {

new Thread(new Runnable() {

@Override
public void run() {

if (mp3Path.endsWith("mp3")) {
// TODO Auto-generated method stub
MP3Player mp3 = new MP3Player(mp3Path);
int ccc = 0;
while (ccc < repeat) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ccc++;
mp3.play();
}
} else {
try {
// .wav 文件放在java project 下面
//System.getProperty("user.dir") + File.separator+ "ring.wav"
System.out.println(mp3Path);
FileInputStream fileau = new FileInputStream(
mp3Path);
AudioStream as = new AudioStream(fileau);
int ccc = 0;
while (ccc < repeat) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ccc++;
AudioPlayer.player.start(as);
}

} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();

}

sun audio jar下載 需要自己下載

G. 如何用Java寫一個原生wav播放器

MusicPlayer.java
[java] view plain
package musicplayer;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

/**
*
* @author Chandler
*/
public class MusicPlayer extends JFrame{

private Container container;
private JButton playbutton;
private PlayBackThread playbackthread;
//默認構造函數
public MusicPlayer(String title){
super(title);
container = this.getContentPane();
playbutton = new JButton("播放");
playbutton.setSize(50,20);
playbutton.addActionListener(new PlayActionListener());
this.setLayout(new BorderLayout());
this.setSize(500,400);
container.add(playbutton,BorderLayout.CENTER);
Toolkit toolkit = Toolkit.getDefaultToolkit();
int x = (int)(toolkit.getScreenSize().getWidth()-this.getWidth())/2;
int y = (int)(toolkit.getScreenSize().getHeight()-this.getHeight())/2;
this.setLocation(x,y);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MusicPlayer musicplayer = new MusicPlayer("MusicPlayer");
}
class PlayActionListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
playbackthread = new PlayBackThread();
playbackthread.start();
}
}
}
PlayBackThread.java

[java] view plain
package musicplayer;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.LineUnavailableException;

import java.io.RandomAccessFile;

import java.util.Scanner;

/**
*
* @author Chandler
*/
public class PlayBackThread extends Thread{
private SourceDataLine dataline;
private final int dataOffset = 0x2e;
public PlayBackThread(){
super("playbackthread");
}

@Override
public void run(){
try{
RandomAccessFile raf = new RandomAccessFile("C:\\Users\\Chandler\\Documents\\NetBeansProjects\\MusicPlayer\\src\\musicplayer\\John Denver - Country Roads.wav","r");
AudioFormat af;
af = new AudioFormat(44100,16,2,true,false);
dataline = (SourceDataLine)AudioSystem.getSourceDataLine(af);
dataline.open(af);
raf.seek(dataOffset);
int hasRead=0;
dataline.start();
byte[] buff=new byte[4096];
Scanner input = new Scanner(System.in);
while((hasRead=raf.read(buff))>0){
// print(raf.getFilePointer(),buff);
dataline.write(buff, 0, hasRead);
}
dataline.stop();
} catch(Exception e){
e.printStackTrace();
}
}
public static void print(long pointer, byte[] buff){
System.out.format("%x: " ,pointer);
System.out.format("%x ", buff[0]);
System.out.format("%x ", buff[1]);
System.out.format("%x ", buff[2]);
System.out.format("%x ", buff[3]);

System.out.println();
}
}

H. java 如何播放C:/sound.wav 給出簡單代碼

public class Music
{
private static AudioStream audioStream = null;
private static ContinuousAudioDataStream audioDataStream = null;
private static final String w = "setting/3.wav";
public static void play(String musicFile)
{
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(musicFile);
audioStream = new AudioStream(fileInputStream);
AudioPlayer.player.start(audioStream);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void stopPlay()
{
AudioPlayer.player.stop(audioStream);
}
public static void stopAllPlay() {
if (audioDataStream != null) {
AudioPlayer.player.stop(audioDataStream);
try {
audioDataStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public static void allPlay(String musicFile) {
stopAllPlay();
FileInputStream fileInputStream = null;
AudioStream audioStream = null;
try {
fileInputStream = new FileInputStream(musicFile);
audioStream = new AudioStream(fileInputStream);
audioDataStream = new ContinuousAudioDataStream(audioStream.getData());
AudioPlayer.player.start(audioDataStream);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static String getW()
{
return "setting/s.wav";
}
public static void main(String[] args) {
new Music(); new Music(); play("setting/s.wav");
}
}

I. java播放音頻文件的

我幫你把程序改了一下,你看看吧。
另外,你的music應該建在當前盤的根目錄下,Keepup.wav放在其中,因為你的路徑是/music/Keepup.wav。如果想放在當前項目中,需要把路徑改為music/Keepup.wav(去掉開頭的斜杠),這樣就可以在當前項目下建music目錄放Keepup.wav文件了。你把Keepup.wav放在了src目錄下面,新建了個文件夾叫music,它的路徑應該是src/music/Keepup.wav。
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JApplet;

public class RR {
RR(){
//URL mus=this.getClass().getResource("/music/Keepup.wav");
URL mus=null;
try {
mus = new File("src/music/Keepup.wav").toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
AudioClip snd =JApplet.newAudioClip(mus);
snd.loop();
}
public static void main(String[] args) {
new RR();
}
}

J. java 播放wav,以及其他音頻文件,哪種方法比較好,看到網上方法很多啊 求高手建議

留名同求~ 我只用過
public class Sound {
public static final String DIR = "sounds/";
public static final AudioClip dead1 = Applet.newAudioClip(Sound.class.getResource(DIR+"dead1.wav"));
}

閱讀全文

與java播放wav相關的資料

熱點內容
不會數學英語如何編程 瀏覽:88
如何能知道網站伺服器地址 瀏覽:648
程序員月薪5萬難嗎 瀏覽:138
如何評價程序員 瀏覽:802
雲虛機和伺服器的區別 瀏覽:403
廣西柳州壓縮機廠 瀏覽:639
arm開發編譯器 瀏覽:833
51單片機的核心 瀏覽:746
看電視直播是哪個app 瀏覽:958
將c源程序編譯成目標文件 瀏覽:787
再要你命3000pdf 瀏覽:558
ai軟體解壓軟體怎麼解壓 瀏覽:520
文件夾怎樣設置序列號 瀏覽:963
javascriptgzip壓縮 瀏覽:248
易語言怎麼取出文件夾 瀏覽:819
蘋果xs手機加密app哪裡設置 瀏覽:605
超聲霧化器與壓縮霧化器 瀏覽:643
模擬實現進程調度演算法 瀏覽:388
現在的壓縮包都是加密 瀏覽:331
施工員找工作去哪個app 瀏覽:632