在 applet 中播放聲音文件非常簡(jiǎn)單,一般需要以下步驟:創(chuàng)建一個(gè) AudioClip 對(duì)象

德令哈網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,德令哈網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為德令哈超過千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的德令哈做網(wǎng)站的公司定做!
裝入 .au 聲音文件到 AudioClip 對(duì)象
一次播放或者不停循環(huán)播放聲音
停止播放
下面是相應(yīng)的代碼:import java.applet.*;AudioClip ac = getAudioClip(getCodeBase(), soundFile);
ac.play(); //play once
ac.stop(); //stop playing
解決這個(gè)問題的竅門是利用由 Sun 及 其JDK 提供的某些 undocumented 的特征。先看看 Sun JDK 中的文件 classes.zip (使用任何解壓工具即可),發(fā)現(xiàn)其中不僅包含標(biāo)準(zhǔn)的 Java 包如 java.applet 而且還存在包 sun.audio. (在 sun/audio 的目錄下.)
包 sun.audio 中包含了用于播放聲音文件所需的所有東西!下面是示例代碼:import sun.audio.*; //import the sun.audio package
import java.io.*;//** add this into your application code as appropriate// Open an input stream to the audio file.
InputStream in = new FileInputStream(Filename);// Create an AudioStream object from the input stream.
AudioStream as = new AudioStream(in);// Use the static class member "player" from class AudioPlayer to play
// clip.
AudioPlayer.player.start(as);// Similarly, to stop the audio.
AudioPlayer.player.stop(as);如果要用一個(gè) URL 做為聲音流的源(source),則用下面的代碼所示替換輸入流來(lái)創(chuàng)建聲音流:AudioStream as = new AudioStream (url.openStream());如果需要持續(xù)播放聲音文件,則要稍稍復(fù)雜一點(diǎn):// Create audio stream as discussed previously.
// Create AudioData source.
AudioData data = as.getData();// Create ContinuousAudioDataStream.
ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);// Play audio.
可以通過Service來(lái)播放背景音樂,以下是實(shí)現(xiàn)代碼:
1.在AndroidManifest.xml文件中的application標(biāo)簽內(nèi)加入下邊語(yǔ)句
service android:name=".MusicServer"
intent-filter
action android:name="com.angel.Android.MUSIC"/
category android:name="android.intent.category.default" /
/intent-filter
/service
2.新建MusicServer.java類,內(nèi)容為
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicServer extends Service {
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent,int startId){
super.onStart(intent, startId);
if(mediaPlayer==null){
// R.raw.mmp是資源文件,MP3格式的
mediaPlayer = MediaPlayer.create(this, R.raw.abc);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mediaPlayer.stop();
}
}
3.將歌曲放入raw文件夾下,名稱為abc。
4.在Activity中加入代碼
private Intent intent = new Intent("com.angel.Android.MUSIC");
onCreate方法中加入startService(intent);
就可以播放了。
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.AWTException;
import java.awt.Frame;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class bofan_2 extends JFrame implements ActionListener
{
boolean looping=false;
File file1=null;
AudioClip sound1;
AudioClip chosenClip;
private JComboBox box1=null; //歌曲列表
private JButton butbofan=null; //播放
private JButton butboxhuan=null; //循環(huán)播放
private JButton buttinzi=null; //停止
private JButton butshan=null; //上一首
private JButton butzhantin=null; //暫停
private JButton butxia=null; //下一首
private TrayIcon trayIcon;//托盤圖標(biāo)
private SystemTray systemTray;//系統(tǒng)托盤
public bofan_2()
{
this.setSize(420,400);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(null);
box1=new JComboBox();
box1.addItem("傷心太平洋");
box1.addItem("勁爆的士高");
box1.addItem("老夫少妻");
box1.addItem("愛不再來(lái)");
box1.addItem("抽身");
box1.addItem("傷心城市");
box1.addItem("二零一二");
box1.addItem("精忠報(bào)國(guó)");
box1.addItem("秋沙");
box1.addItem("吻別");
box1.addItem("音樂瘋起來(lái)");
box1.setBounds(10,20,150,20);
butbofan=new JButton("播放");
butbofan.addActionListener(this);
butbofan.setBounds(165,50,60,20);
butboxhuan=new JButton("循環(huán)播放");
butboxhuan.addActionListener(this);
butboxhuan.setBounds(230,50,90,20);
buttinzi=new JButton("停止");
buttinzi.setEnabled(false);
buttinzi.addActionListener(this);
buttinzi.setBounds(335,50,60,20);
butshan=new JButton("上一首");
butshan.addActionListener(this);
butshan.setBounds(165,90,80,20);
butzhantin=new JButton("暫停");
butzhantin.setEnabled(false);
butzhantin.addActionListener(this);
butzhantin.setBounds(250,90,60,20);
butxia=new JButton("下一首");
butxia.addActionListener(this);
butxia.setBounds(320,90,80,20);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(box1);
this.getContentPane().add(butbofan);
this.getContentPane().add(butboxhuan);
this.getContentPane().add(buttinzi);
this.getContentPane().add(butshan);
this.getContentPane().add(butzhantin);
this.getContentPane().add(butxia);
try {
UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel");
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (InstantiationException e)
{
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e)
{
e.printStackTrace();
}
setSize(450,450);
systemTray = SystemTray.getSystemTray();//獲得系統(tǒng)托盤的實(shí)例
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
trayIcon = new TrayIcon(ImageIO.read(new File("004.jpg")));
systemTray.add(trayIcon);//設(shè)置托盤的圖標(biāo),0.gif與該類文件同一目錄
}
catch (IOException e1)
{
e1.printStackTrace();
}
catch (AWTException e2)
{
e2.printStackTrace();
}
this.addWindowListener(
new WindowAdapter(){
public void windowIconified(WindowEvent e)
{
dispose();//窗口最小化時(shí)dispose該窗口
}
});
trayIcon.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 2)//雙擊托盤窗口再現(xiàn)
setExtendedState(Frame.NORMAL);
setVisible(true);
}
});
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source== butbofan)
{
System.out.println((String) box1.getSelectedItem());
file1=new File((String) box1.getSelectedItem()+".wav");
butboxhuan.setEnabled(true);
buttinzi.setEnabled(true);
butzhantin.setEnabled(true);
butzhantin.setText("暫停");
try {
sound1 = Applet.newAudioClip(file1.toURL());
chosenClip = sound1;
} catch(OutOfMemoryError er){
System.out.println("內(nèi)存溢出");
er.printStackTrace();
} catch(Exception ex){
ex.printStackTrace();
}
chosenClip.play();
this.setTitle("正在播放"+(String) box1.getSelectedItem());
}
if (source== butboxhuan)
{
file1=new File((String) box1.getSelectedItem()+".wav");
try {
sound1 = Applet.newAudioClip(file1.toURL());
chosenClip = sound1;
} catch(OutOfMemoryError er){
System.out.println("內(nèi)存溢出");
er.printStackTrace();
} catch(Exception ex){
ex.printStackTrace();
}
looping = true;
chosenClip.loop();
butboxhuan.setEnabled(false);
buttinzi.setEnabled(true);
butzhantin.setText("暫停");
this.setTitle("正在循環(huán)播放"+(String) box1.getSelectedItem());
}
if (source== buttinzi)
{
if (looping)
{
looping = false;
chosenClip.stop();
butboxhuan.setEnabled(true);
butzhantin.setText("暫停");
} else {
chosenClip.stop();
}
buttinzi.setEnabled(false);
this.setTitle("停止播放");
}
if(source==butshan)
{
butzhantin.setText("暫停");
}
if(source==butzhantin)
{
buttinzi.setEnabled(false);
butzhantin.setText("繼續(xù)");
if(source==butzhantin)
{
butzhantin.setText("暫停");
}
}
if(source==butxia)
{
butzhantin.setText("暫停");
}
}
public static void main(String[] args)
{
bofan_2 xx=new bofan_2();
}
}
/*
可以用加載聲音文件的方法:
第一幀:mysound= new Sound();
mysound.attachSound(聲音id名字);
ptime = 0;
播放按鈕as:
on(release){
mysound.start(ptime);
}
暫停按鈕as:
on(release){
ptime = mysound.position/1000;
mysound.stop();
}
*/
不知道你是在java里哪添加?Swing界面中嗎?
下面這個(gè)是我之前做Swing界面程序時(shí)添加音樂的代碼,希望對(duì)你有幫助
AudioClip[] musics;//定義音樂集合
musics = new AudioClip[2];//初始化
URL url1 = this.getClass().getResource("/ReadyGo.WAV"); //定義音樂文件地址
URL url2 = this.getClass().getResource("/back1.mid"); //定義音樂文件地址
musics[0] = JApplet.newAudioClip(url1);
musics[1] = JApplet.newAudioClip(url2);
musics[0].play();//音樂開始執(zhí)行
musics[1].stop();//停止播放
java中打開音樂播放器的方式是使用audioclip類來(lái)播放音樂,實(shí)例如下:
import?java.applet.*;
import?java.awt.*;
import?java.awt.event.*;
import?java.net.*;
import?javax.swing.*;
import?java.io.File;
class?AudioPlayDemo?extends?JFrame?implements?ActionListener?{
boolean?looping?=?false;?
File?file1?=?new?File("music\\明天會(huì)更好.wav");
AudioClip?sound1;
AudioClip?chosenClip;
JButton?playButton?=?new?JButton("播放");?
JButton?loopButton?=?new?JButton("循環(huán)播放");?
JButton?stopButton?=?new?JButton("停止");?
JLabel?status?=?new?JLabel("選擇播放文件");?
JPanel?controlPanel?=?new?JPanel();?
Container?container?=?getContentPane();?
public?AudioPlayDemo()?{?
try?{
sound1?=?Applet.newAudioClip(file1.toURL());
chosenClip?=?sound1;
}?catch(OutOfMemoryError?e){
System.out.println("內(nèi)存溢出");
e.printStackTrace();
}?catch(Exception?e){
e.printStackTrace();
}
playButton.addActionListener(this);
loopButton.addActionListener(this);
stopButton.addActionListener(this);
stopButton.setEnabled(false);?
controlPanel.add(playButton);
controlPanel.add(loopButton);
controlPanel.add(stopButton);
container.add(controlPanel,?BorderLayout.CENTER);
container.add(status,?BorderLayout.SOUTH);
setSize(300,?130);?
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?//關(guān)閉窗口時(shí)退出程序
}
public?void?actionPerformed(ActionEvent?event)?{
if?(chosenClip?==?null)?{
status.setText("聲音未載入");
return;?
}
Object?source?=?event.getSource();?//獲取用戶洗滌激活的按鈕
if?(source?==?playButton)?{
stopButton.setEnabled(true);?
loopButton.setEnabled(true);?
chosenClip.play();?
status.setText("正在播放");
}
if?(source?==?loopButton)?{
looping?=?true;
chosenClip.loop();?
loopButton.setEnabled(false);?
stopButton.setEnabled(true);?
status.setText("正在循環(huán)播放");?
}
if?(source?==?stopButton)?{
if?(looping)?{
looping?=?false;
chosenClip.stop();?
loopButton.setEnabled(true);
}?else?{
chosenClip.stop();
}
stopButton.setEnabled(false);?
status.setText("停止播放");
}
}
public?static?void?main(String?s[])?{
new?AudioPlayDemo();?
}
}
只能播放wav格式的歌曲
分享名稱:java調(diào)用音樂代碼 java播放音樂代碼
網(wǎng)頁(yè)地址:http://chinadenli.net/article46/hghjhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、小程序開發(fā)、手機(jī)網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站建設(shè)、App開發(fā)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)