本文目錄一覽:
- 1、java編寫的音樂播放器如何讓進度條和音樂關聯起來 點擊進度條的指定位置 音樂就從指定位置開始播放
- 2、如何用java做一個音樂播放器?
- 3、想用Java編寫一個音樂播放器,將歷史記錄存放在MySql資料庫中,請大神給出思路
- 4、求一個JAVA音樂播放器的源代碼
- 5、java音樂播放器沒有聲音,MP3 flac 都試過了
java編寫的音樂播放器如何讓進度條和音樂關聯起來 點擊進度條的指定位置 音樂就從指定位置開始播放
//變數:
private SeekBar seekbar;
private MediaPlayer player = null;
player = new MediaPlayer();//初始化
seekbar.setMax(player.getDuration());//設進度條顯示
//音量:
audioManager=(AudioManager)getSystemService(AUDIO_SERVICE);
int MaxSound=audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
maxVolumeTextView.setText(String.valueOf(MaxSound));
SoundseekBar.setMax(MaxSound);
int currentSount=audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SoundseekBar.setProgress(currentSount);
SoundseekBar.setOnSeekBarChangeListener(new SeekBarListener());
ProceseekBar2.setOnSeekBarChangeListener(new ProcessBarListener());
如何用java做一個音樂播放器?
首先下載播放mp3的包,比如mp3spi1.9.4.jar。在工程中添加這個包。
播放器演示代碼如下
package com.test.audio;
import java.io.File;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class MusicPlayer extends Frame {
/**
*
*/
private static final long serialVersionUID = -2605658046194599045L;
boolean isStop = true;// 控制播放線程
boolean hasStop = true;// 播放線程狀態
String filepath;// 播放文件目錄
String filename;// 播放文件名稱
AudioInputStream audioInputStream;// 文件流
AudioFormat audioFormat;// 文件格式
SourceDataLine sourceDataLine;// 輸出設備
List list;// 文件列表
Label labelfilepath;//播放目錄顯示標籤
Label labelfilename;//播放文件顯示標籤
public MusicPlayer() {
// 設置窗體屬性
setLayout(new BorderLayout());
setTitle(“MP3 Music Player”);
setSize(350, 370);
// 建立菜單欄
MenuBar menubar = new MenuBar();
Menu menufile = new Menu(“File”);
MenuItem menuopen = new MenuItem(“Open”, new MenuShortcut(KeyEvent.VK_O));
menufile.add(menuopen);
menufile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open();
}
});
menubar.add(menufile);
setMenuBar(menubar);
// 文件列表
list = new List(10);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 雙擊時處理
if (e.getClickCount() == 2) {
// 播放選中的文件
filename = list.getSelectedItem();
play();
}
}
});
add(list, “Center”);
// 信息顯示
Panel panel = new Panel(new GridLayout(2, 1));
labelfilepath = new Label(“Dir:”);
labelfilename = new Label(“File:”);
panel.add(labelfilepath);
panel.add(labelfilename);
add(panel, “North”);
// 註冊窗體關閉事件
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
// 打開
private void open() {
FileDialog dialog = new FileDialog(this, “Open”, 0);
dialog.setVisible(true);
filepath = dialog.getDirectory();
if (filepath != null) {
labelfilepath.setText(“Dir:” + filepath);
// 顯示文件列表
list.removeAll();
File filedir = new File(filepath);
File[] filelist = filedir.listFiles();
for (File file : filelist) {
String filename = file.getName().toLowerCase();
if (filename.endsWith(“.mp3”) || filename.endsWith(“.wav”)) {
list.add(filename);
}
}
}
}
// 播放
private void play() {
try {
isStop = true;// 停止播放線程
// 等待播放線程停止
System.out.print(“Start:” + filename);
while (!hasStop) {
System.out.print(“.”);
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
System.out.println(“”);
File file = new File(filepath + filename);
labelfilename.setText(“Playing:” + filename);
// 取得文件輸入流
audioInputStream = AudioSystem.getAudioInputStream(file);
audioFormat = audioInputStream.getFormat();
// 轉換mp3文件編碼
if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
audioFormat.getSampleRate(), 16, audioFormat
.getChannels(), audioFormat.getChannels() * 2,
audioFormat.getSampleRate(), false);
audioInputStream = AudioSystem.getAudioInputStream(audioFormat,
audioInputStream);
}
// 打開輸出設備
DataLine.Info dataLineInfo = new DataLine.Info(
SourceDataLine.class, audioFormat,
AudioSystem.NOT_SPECIFIED);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
// 創建獨立線程進行播放
isStop = false;
Thread playThread = new Thread(new PlayThread());
playThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
class PlayThread extends Thread {
byte tempBuffer[] = new byte[320];
public void run() {
try {
int cnt;
hasStop = false;
// 讀取數據到緩存數據
while ((cnt = audioInputStream.read(tempBuffer, 0,
tempBuffer.length)) != -1) {
if (isStop)
break;
if (cnt 0) {
// 寫入緩存數據
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
// Block等待臨時數據被輸出為空
sourceDataLine.drain();
sourceDataLine.close();
hasStop = true;
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
public static void main(String args[]) {
new MusicPlayer();
}
}
想用Java編寫一個音樂播放器,將歷史記錄存放在MySql資料庫中,請大神給出思路
給你一個基礎的實現方法與一個完全開發的實現方法:
基礎實現方法:
1)從網上搜索一個可以播放音樂的 java 類庫,該類庫帶有音樂播放的API供外部程序調用。
2)你直接使用該音樂類庫進行音樂播放操作。
3)你設計一個 Swing 界面在播放的時候,把播放的音樂信息存在資料庫。
完全開發方法:
1)你自己從最原始做起包括音樂文件的播放解碼等全部用 java 寫出一個音樂類庫供自己的程序調用。
2)你直接使用該音樂類庫進行音樂播放操作。
3)你設計一個 Swing 界面在播放的時候,把播放的音樂信息存在資料庫。
求一個JAVA音樂播放器的源代碼
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MediaPlayer extends JFrame implements ActionListener,
ItemListener, ControllerListener {
String title;
Player player;
boolean first = true, loop = false;
Component vc, cc;
String currentDirectory=null;
// 構造函數,其中包括了設置響應窗口事件的監聽器。
MediaPlayer(String title) {
super(title);
/* 關閉按鈕的實現。。 */
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
public void windowClosed(WindowEvent e) {
if (player != null)
player.close();
System.exit(0);
}
});
// 調用程序菜單欄的方法成員完成菜單的布置
setupMenu();
setSize(400, 400);
setVisible(true);
}
// 本方法用以設置程序菜單欄
public void setupMenu() {
// 設置一個菜單
Menu f = new Menu(“文件”);
// 往設置的菜單添加菜單項
MenuItem mi = new MenuItem(“打開”);
f.add(mi);
mi.addActionListener(this);
f.addSeparator();
CheckboxMenuItem cbmi = new CheckboxMenuItem(“循環”, false);
cbmi.addActionListener(this);
f.add(cbmi);
f.addSeparator();
MenuItem ee = new MenuItem(“退出”);
ee.addActionListener(this);
f.add(ee);
f.addSeparator();
Menu l = new Menu(“播放列表”);
Menu c = new Menu(“播放控制”);
MenuItem move = new MenuItem(“播放”);
move.addActionListener(this);
c.add(move);
c.addSeparator();
MenuItem pause = new MenuItem(“暫停”);
pause.addActionListener(this);
c.add(pause);
c.addSeparator();
MenuItem stop = new MenuItem(“停止”);
stop.addActionListener(this);
c.add(stop);
c.addSeparator();
// 設置一個菜單欄
MenuBar mb = new MenuBar();
mb.add(f);
mb.add?;
mb.add(l);
// 將構造完成的菜單欄交給當前程序的窗口;
setMenuBar(mb);
}
// 動作時間響應成員;捕捉髮送到本對象的各種事件;
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String cufile, selectfile, currentDirectory;
if (e.getActionCommand().equals(“退出”)) {
// 調用dispose以便執行windowClosed
dispose();
return;
}
// 此事表明擁護選擇了「播放」命令;
// 如果當前有一個文件可以播放則執行播放命令;
if (e.getActionCommand().equals(“播放”)) {
if (player != null) {
player.start();
}
return;
}
// 如果當前正在播放某一文件,則執行暫停;
if (e.getActionCommand().equals(“暫停”)) {
if (player != null) {
player.stop();
}
return;
}
// 停止命令的響應;
if (e.getActionCommand().equals(“停止”)) {
if (player != null) {
player.stop();
player.setMediaTime(new Time(0));
}
return;
}
// 用戶選擇要播放的媒體文件
if (e.getActionCommand().equals(“打開”)) {
FileDialog fd = new FileDialog(this, “打開媒體文件”, FileDialog.LOAD);
// fd.setDirectory(currentDirectory);
2008-2-6 02:46 回復
肆方茉莉
62位粉絲
6樓
fd.setVisible(true);
// 如果用戶放棄選擇文件,則返回
if (fd.getFile() == null) {
return;
}
// 保存了所選文件的名稱及其路徑名稱已被稍後使用
// 同時設置當前文件夾路徑
selectfile = fd.getFile();
currentDirectory = fd.getDirectory();
cufile = currentDirectory + selectfile;
// 將用戶選擇的文件作為一個菜單項加入播放列表,該菜單項名為該文件名;
// 被點擊後給出的命令串是該文件的全路徑名
MenuItem mi = new MenuItem(selectfile);
mi.setActionCommand(cufile);
MenuBar mb = getMenuBar();
Menu m = mb.getMenu(2);
mi.addActionListener(this);
m.add(mi);
} else {
// 程序邏輯運行到次表示用戶選擇了一個「播放列表」中的媒體文件
// 此時可以通過如下動作獲得該文件的全路徑名
cufile = e.getActionCommand();
selectfile = cufile;
}
// 如果存在一個播放器,則先將其關閉,稍後再重新創建
// 創建播放器時需要捕捉一些異常
if (player != null) {
player.close();
}
try {
player = Manager.createPlayer(new MediaLocator(“file:” + cufile));
} catch (Exception e2) {
System.out.println(e2);
return;
}/*
* catch(NoPlayerException e2){ System.out.println(“不能找到播放器”);
* return ; }
*/
if (player == null) {
System.out.println(“無法創建播放器”);
return;
}
first = false;
setTitle(selectfile);
// 設置處理播放控制器實際的對象;
/**/
player.addControllerListener(this);
player.prefetch();
}
// 菜單狀態改變事件的響應函數;
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MediaPlayer(“播放器”);
}
// 調用繪圖函數進行界面的繪製 // public void update() {
// }
// 繪圖函數成員 //public void paint(Graphics g) {
// }
public void controllerUpdate(ControllerEvent e) {
// TODO Auto-generated method stub
Container tainer = getContentPane();
// 調用player.close()時ControllerClosedEvent事件出現
// 如果存在視覺部件,則該部件應該拆除(為了一致起見,我們對控制面版部件也執行同樣的操作,下一次需要時再構造)
if (e instanceof ControllerClosedEvent) {
if (vc != null) {
remove(vc);
vc = null;
}
if (cc != null) {
remove(cc);
cc = null;
}
}
// 播放結束時,將播放指針置於文件之首,如果設定了循環播放,則再次啟動播放器;
if (e instanceof EndOfMediaEvent) {
player.setMediaTime(new Time(0));
if (loop) {
player.start();
}
return;
}
// PrefetchCompletEvent事件發生後調用start,正式啟動播放
if (e instanceof PrefetchCompleteEvent) {
player.start();
return;
}
// 本事件表示由於播放的資源已經確定;此時要將媒體的圖形conmopnent
// 如果有顯示出來,同時將播放器player的控制顯示到窗口裡;
if (e instanceof RealizeCompleteEvent) {
// 如果媒體中有圖像,將對應圖像component載入窗體;
vc = player.getVisualComponent();
if (vc != null)
tainer.add(vc, BorderLayout.CENTER);
// 將對應控制器component載入窗體;
cc = player.getControlPanelComponent();
cc.setBackground(Color.blue);
if (cc != null)
tainer.add(cc, BorderLayout.SOUTH);
// 有一些特殊媒體在播放時提供另外的控制手段,將控制器一併加入窗口;
/*
* gc=player.getGainControl(); gcc=gc.getControlComponent();
* if(gcc!=null) tainer.add(gcc,BorderLayout.NORTH);
*/
// 根據媒體文件中是否有圖像,設定相應的窗口大小
if (vc != null) {
pack();
return;
} else {
setSize(300, 75);
setVisible(true);
return;
}
}
} }
java音樂播放器沒有聲音,MP3 flac 都試過了
Java Applet支持的音樂格式只有wav,mid,au三種格式,
你如果要播放Mp3格式的音樂文件,需要JMF(Java Media Framework) API的有關jar包的支持才行.flac格式的音樂文件就更不行了.
要不然你用音樂格式的轉換軟體把Mp3文件轉成wav,就可以播放了.
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/158936.html