导航:首页 > 编程语言 > java播放器的实现

java播放器的实现

发布时间:2023-06-07 14:40:51

❶ 如何用java做一个音乐播放器

首先下载播放mp3的包,比如mp3spi1.9.4.jar。在工程中添加这个包。

播放器演示代码如下

packagecom.test.audio;
importjava.io.File;
importjava.awt.BorderLayout;
importjava.awt.FileDialog;
importjava.awt.Frame;
importjava.awt.GridLayout;
importjava.awt.Label;
importjava.awt.List;
importjava.awt.Menu;
importjava.awt.MenuBar;
importjava.awt.MenuItem;
importjava.awt.MenuShortcut;
importjava.awt.Panel;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyEvent;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;

importjavax.sound.sampled.AudioFormat;
importjavax.sound.sampled.AudioInputStream;
importjavax.sound.sampled.AudioSystem;
importjavax.sound.sampled.DataLine;
importjavax.sound.sampled.SourceDataLine;

{
/**
*
*/
=-2605658046194599045L;
booleanisStop=true;//控制播放线程
booleanhasStop=true;//播放线程状态

液悔Stringfilepath;//播放文件目录
Stringfilename;//播放文件名称
;//文件流
AudioFormataudioFormat;//文件格式
SourceDataLinesourceDataLine;//输出设备

Listlist;//文件列表
Labellabelfilepath;//播放目录显示标签
Labellabelfilename;//播放文件显示标签

publicMusicPlayer(){
闹磨正//设置窗体属性
setLayout(newBorderLayout());
setTitle("MP3MusicPlayer");
setSize(350,370);

//建立菜单栏
MenuBarmenubar=newMenuBar();
Menumenufile=newMenu("File");
MenuItemmenuopen=newMenuItem("Open",newMenuShortcut(KeyEvent.VK_O));
menufile.add(menuopen);
游宴menufile.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
open();
}
});
menubar.add(menufile);
setMenuBar(menubar);

//文件列表
list=newList(10);
list.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
//双击时处理
if(e.getClickCount()==2){
//播放选中的文件
filename=list.getSelectedItem();
play();
}
}
});
add(list,"Center");

//信息显示
Panelpanel=newPanel(newGridLayout(2,1));
labelfilepath=newLabel("Dir:");
labelfilename=newLabel("File:");
panel.add(labelfilepath);
panel.add(labelfilename);
add(panel,"North");

//注册窗体关闭事件
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
setVisible(true);
}

//打开
privatevoidopen(){
FileDialogdialog=newFileDialog(this,"Open",0);
dialog.setVisible(true);
filepath=dialog.getDirectory();
if(filepath!=null){
labelfilepath.setText("Dir:"+filepath);

//显示文件列表
list.removeAll();
Filefiledir=newFile(filepath);
File[]filelist=filedir.listFiles();
for(Filefile:filelist){
Stringfilename=file.getName().toLowerCase();
if(filename.endsWith(".mp3")||filename.endsWith(".wav")){
list.add(filename);
}
}
}
}

//播放
privatevoidplay(){
try{
isStop=true;//停止播放线程
//等待播放线程停止
System.out.print("Start:"+filename);
while(!hasStop){
System.out.print(".");
try{
Thread.sleep(10);
}catch(Exceptione){
}
}
System.out.println("");
Filefile=newFile(filepath+filename);
labelfilename.setText("Playing:"+filename);

//取得文件输入流
audioInputStream=AudioSystem.getAudioInputStream(file);
audioFormat=audioInputStream.getFormat();
//转换mp3文件编码
if(audioFormat.getEncoding()!=AudioFormat.Encoding.PCM_SIGNED){
audioFormat=newAudioFormat(AudioFormat.Encoding.PCM_SIGNED,
audioFormat.getSampleRate(),16,audioFormat
.getChannels(),audioFormat.getChannels()*2,
audioFormat.getSampleRate(),false);
audioInputStream=AudioSystem.getAudioInputStream(audioFormat,
audioInputStream);
}

//打开输出设备
DataLine.InfodataLineInfo=newDataLine.Info(
SourceDataLine.class,audioFormat,
AudioSystem.NOT_SPECIFIED);
sourceDataLine=(SourceDataLine)AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();

//创建独立线程进行播放
isStop=false;
ThreadplayThread=newThread(newPlayThread());
playThread.start();
}catch(Exceptione){
e.printStackTrace();
}
}

classPlayThreadextendsThread{
bytetempBuffer[]=newbyte[320];

publicvoidrun(){
try{
intcnt;
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(Exceptione){
e.printStackTrace();
System.exit(0);
}
}
}

publicstaticvoidmain(Stringargs[]){
newMusicPlayer();
}
}

❷ java如何实现播放mp3

简单的实例,代码如下,纯粹JMF加载MP3并播放:
import javax.media.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PlayerMusic implements ControllerListener {// ControllerListener
// 控制事件
private Player player;

private boolean first, loop;

private String path;
private List mp3List;
private int mp3NO = 0;

PlayerMusic(List mp3List) {
this.mp3List = mp3List;
}

public void start() {
try {
player = Manager.createPlayer(new MediaLocator("file://" + mp3List.get(mp3NO)));
} catch (NoPlayerException ex) {
ex.printStackTrace();
System.out.println("不能播放文件");
return;
} catch (IOException ex) {
ex.printStackTrace();
return;
}
if (player == null) {
System.out.println("播放器为空");
return;
}

first = false;
player.addControllerListener(this);
// 提取媒体内容
player.prefetch();

}

public void controllerUpdate(ControllerEvent e) {
// 当媒体播放结束时,循环播放
if (e instanceof EndOfMediaEvent) {
mp3NO++;
if(mp3NO<this.mp3List.size()){
this.start();
}
return;
}

// 当预提取媒体的内容结束
if (e instanceof PrefetchCompleteEvent) {
player.start();
return;
}
// 当实例化后
if (e instanceof RealizeCompleteEvent) {
// pack(); //执行pack()操作
return;
}

}
public static void main(String[] args) {
List mp3List = new ArrayList();
mp3List.add("d://a.mp3");
mp3List.add("d://b.mp3");
mp3List.add("d://c.mp3");
PlayerMusic pm = new PlayerMusic(mp3List);
pm.start();
}
}

❸ java课题设计——视频播放器怎么制作

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;import javax.swing.*;
import javax.media.*;public class VideoPlayer extends JFrame implements ActionListener,
ControllerListener {
FileDialog fd;
Player player = null;
String name;
String path;
Component comp, vc;
Image image=new ImageIcon("open.GIF").getImage();
MenuBar mb = new MenuBar();
Menu m1 = new Menu("文件");
Menu m2 = new Menu("帮助");
MenuItem mi1 = new MenuItem("打开", new MenuShortcut(KeyEvent.VK_0));
MenuItem mi2 = new MenuItem("关闭");
MenuItem mi3 = new MenuItem("帮助");
JButton open=new JButton(new ImageIcon("open.GIF")); public VideoPlayer() {
super("视屏播放器");
this.setMenuBar(mb);
mb.add(m1);
mb.add(m2);
m1.add(mi1);
mi1.addActionListener(this);
m1.add(mi2);
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
m2.add(mi3);
mi3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"Java视屏播放器\n2010年6月9日第一版\n制作人: 李攀");
}
});
this.setLayout(new BorderLayout());
this.setSize(800, 650);
this.setVisible(true);
this.setBackground(Color.BLACK);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public static void main(String[] args) {
new VideoPlayer();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == mi1) {
open();
}
} public void open() {
fd = new FileDialog(this, "打开媒体文件", FileDialog.LOAD);
fd.setVisible(true);
path = fd.getDirectory();
name = fd.getFile();
if (!path.equals("") && path != null) {
try {
if (player != null)
player.close();
player = Manager.createPlayer(new MediaLocator("file:" + path
+ name));
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "获取播放器失败!");
}
player.addControllerListener(this);
player.prefetch();
player.start();
}
} public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 650);
g.setColor(Color.GREEN);
g.setFont(new Font("宋体",Font.BOLD,30));
g.drawString("欢迎使用",340,150);
g.drawImage(image,330,285,image.getWidth(this),image.getHeight(this),this);
}
public void update(Graphics g){
paint(g);
} public synchronized void controllerUpdate(ControllerEvent e) {
if (e instanceof RealizeCompleteEvent) {
if ((comp = player.getControlPanelComponent()) != null) {
vc = player.getVisualComponent();
if (vc != null)
add(vc);
add("South", comp);
comp.setBackground(Color.green);
}
validate();
}
if (e instanceof ControllerClosedEvent) {
if (vc != null) {
remove(vc);
vc = null;
}
if (comp != null) {
remove(comp);
comp = null;
}
return;
}
if (e instanceof EndOfMediaEvent) {
player.setMediaTime(new Time(0));
player.start();
return;
}
}
}
java的视屏播放要用到JMF(java media framework),你自己到网上去下一个嘛,而且那玩意支持的格式少,mpg,mpeg,sun的官方网站说支持AVI但是我试过了好像不行,至于rmvb那些好像不行,要装解码器。JMF在安装的时候要安装对哦,路径要选择对,不然不行,具体下载地址和安装方法可以网络一下,到处都是

❹ 怎么用java实现一个简单的播放器

用java实现播放器 主要用到java里的媒体框架,即JMF, JMF实际上是Java的一个类包。JMF 2.1.1技术提供了先进的媒体处理能力,从而扩展了Java平台的功能。这些功能包括:媒体捕获、压缩、流转、回放,以及对各种主要媒体形式和编码的支 持,如M-JPEG、H.263、MP3、RTP/RTSP (实时传送协议和实时流转协议)、Macromedias Flash、IBM的HotMedia和Beatniks的Rich Media Format (RMF)等。JMF 2.1.1还支持广受欢迎的媒体类型,如Quicktime、Microsofts AVI和MPEG-1等。此外,JMF 2.1.1软件中包括了一个开放的媒体架构,可使开发人员灵活采用各种媒体回放、捕获组件,或采用他们自己的定制的内插组件。 我当初也做过类似的东西(本科实习时),给你个具体教程链接吧: http://hi..com/samxx8/blog/item/90532ba4d13fcdf69052ee5c.html

❺ 如何用Java来编写一个音乐播放器

首先要在环境电脑中安装下JMF环境,才能引入javax.sound.sampled.*这个包,一迹和下是用过的代码
package TheMusic;
import java.io.*;

import javax.sound.sampled.*;

public class Music {

public static void main(String[] args) {

// TODO Auto-generated method stub

//修改你的音乐文仿州扰件路径就OK了

AePlayWave apw=new AePlayWave("突然备旦好想你.wav");

apw.start();

}

}

在程序中实例化这个类,启动线程,实例化的时候参照Test修改路径就OK播放声音的类
Java代码

public class AePlayWave extends Thread {

private String filename;

public AePlayWave(String wavfile) {

filename = wavfile;

}

public void run() {

File soundFile = new File(filename);

AudioInputStream audioInputStream = null;

try {

audioInputStream = AudioSystem.getAudioInputStream(soundFile);

} catch (Exception e1) {

e1.printStackTrace();

return;

}

AudioFormat format = audioInputStream.getFormat();

SourceDataLine auline = null;

DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

try {

auline = (SourceDataLine) AudioSystem.getLine(info);

auline.open(format);

} catch (Exception e) {

e.printStackTrace();

return;

}

auline.start();

int nBytesRead = 0;

byte[] abData = new byte[512];

try {

while (nBytesRead != -1) {

nBytesRead = audioInputStream.read(abData, 0, abData.length);

if (nBytesRead >= 0)

auline.write(abData, 0, nBytesRead);

}

} catch (IOException e) {

e.printStackTrace();

return;

} finally {

auline.drain();

auline.close();

}

}

}

阅读全文

与java播放器的实现相关的资料

热点内容
80年代台湾老电影红楼梦 浏览:277
大疆带屏控如何安装app 浏览:945
国产大胸电影 浏览:706
模拟器中的pco的命令提示窗口 浏览:874
萝卜双端源码 浏览:873
魔域gm易语言工具源码 浏览:452
机械设计手册pdf电子版 浏览:98
为什么网吧服务器会掉盘 浏览:526
文电通pdf套装版4 浏览:327
如何使用百度地图服务器地址 浏览:921
吉林租服务器托管云服务器 浏览:781
中越反击战电影全集 浏览:116
溯源码验证码无效 浏览:354
风月片有酷网站 浏览:687
大尺度电影韩剧 浏览:680
安卓手机怎么联接a 浏览:716
好色小姨 小说 浏览:677
网站的源码怎么使用 浏览:61
我的世界服务器b2怎么玩 浏览:582
付费电影免费看。 浏览:844