音频播放器java代码,java编写视频播放器

Java播放器源代码。

import javax.media.*;

平山网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。成都创新互联成立于2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联

import java.awt.*;

import java.awt.event.*;

class MediaPlayer extends Frame implements ActionListener,

ControllerListener, ItemListener

{

Player player;

Component vc, cc;

boolean first = true, loop = false;

String currentDirectory;

MediaPlayer (String title)

{

super (title);

addWindowListener

(new WindowAdapter ()

{

public void windowClosing (WindowEvent e) {

// 用户点击窗口系统菜单的关闭按钮

// 调用dispose以执行windowClosed

dispose ();

} public void windowClosed (WindowEvent e) {

if (player != null) player.close ();

System.exit (0);

}

});

Menu m = new Menu ("文件");

MenuItem mi = new MenuItem ("打开");

mi.addActionListener (this);

m.add (mi);

m.addSeparator ();

CheckboxMenuItem cbmi = new CheckboxMenuItem ("循环", false);

cbmi.addItemListener (this);

m.add (cbmi);

m.addSeparator ();

mi = new MenuItem ("退出");

mi.addActionListener (this);

m.add (mi);

MenuBar mb = new MenuBar ();

mb.add (m);

setMenuBar (mb);

setSize (200, 200);

setVisible (true);

}

public void actionPerformed (ActionEvent e)

{

if (e.getActionCommand ().equals ("退出"))

{

// 调用dispose以便执行windowClosed

dispose ();

return;

}

FileDialog fd = new FileDialog (this, "打开媒体文件",

FileDialog.LOAD);

fd.setDirectory (currentDirectory);

fd.show ();

// 如果用户放弃选择文件,则返回

if (fd.getFile () == null) return;

currentDirectory = fd.getDirectory ();

if (player != null)

player.close ();

try

{

player = Manager.createPlayer (new MediaLocator ("file:" + fd.getDirectory () + fd.getFile ()));

}

catch (java.io.IOException e2)

{

System.out.println (e2);

return;

}

catch (NoPlayerException e2)

{

System.out.println ("不能找到播放器.");

return;

}

if (player == null)

{

System.out.println ("无法创建播放器.");

return;

}

first = false;

setTitle (fd.getFile ());

player.addControllerListener (this);

player.prefetch ();

}

public void controllerUpdate (ControllerEvent e)

{

// 调用player.close()时ControllerClosedEvent事件出现。

// 如果存在视觉部件,则该部件应该拆除(为一致起见,

// 我们对控制面板部件也执行同样的操作)

if (e instanceof ControllerClosedEvent)

{

if (vc != null)

{

remove (vc);

vc = null;

}

if (cc != null)

{

remove (cc);

cc = null;

}

return;

}

if (e instanceof EndOfMediaEvent)

{

if (loop)

{

player.setMediaTime (new Time (0));

player.start ();

}

return;

}

if (e instanceof PrefetchCompleteEvent)

{

player.start ();

return;

}

if (e instanceof RealizeCompleteEvent)

{

vc = player.getVisualComponent ();

if (vc != null)

add (vc);

cc = player.getControlPanelComponent ();

if (cc != null)

add (cc, BorderLayout.SOUTH);

pack ();

}

}

public void itemStateChanged (ItemEvent e)

{

loop = !loop;

}

public void paint (Graphics g)

{

if (first)

{

int w = getSize ().width;

int h = getSize ().height;

g.setColor (Color.blue);

g.fillRect (0, 0, w, h);

Font f = new Font ("DialogInput", Font.BOLD, 16);

g.setFont (f);

FontMetrics fm = g.getFontMetrics ();

int swidth = fm.stringWidth ("*** 欢迎 ***");

g.setColor (Color.white);

g.drawString ("*** 欢迎 ***",

(w - swidth) / 2,

(h + getInsets ().top) / 2);

}

// 调用超类Frame的paint()方法,该paint()方法将调用Frame包含的各个容器

// 和部件(包括控制面板部件)的paint()方法。

super.paint (g);

}

// 不执行背景清除操作,以免控制面板部件闪烁

public void update (Graphics g)

{

paint (g);

}

public static void main (String [] args) {

new MediaPlayer ("媒体播放器1.0");

} }

java编写 mp3播放器 代码

// 你看看吧。。 必须下载 jmf包 如果不知道下载就问我吧

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import javax.media.bean.playerbean.MediaPlayer; //必须下载 jmf 媒体播放包

public class player extends Applet implements ActionListener {

Button b1, b2;

MediaPlayer player;

public void init() {

player = new MediaPlayer();

setLayout(new FlowLayout());

try{

player.setMediaLocation("file:/F:\\音乐\\mp3\\黑白配.mp3");// file:/不能删除 音频文件路径

} catch (Exception e) {

System.out.println("文件不存在");

}

b1 = new Button("播放");

b2 = new Button("停止");

add(b1);

add(b2);

b1.addActionListener(this);

b2.addActionListener(this);

setSize(200, 200);

setVisible(true);

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == b1) {

player.start();

} else if (e.getSource() == b2) {

player.stop();

System.out.println(player.getMediaTime().getSeconds());

}

}

}

求JAVA视频播放器代码

import java.awt.BorderLayout;

import java.awt.Component;

import java.awt.FileDialog;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.media.ControllerClosedEvent;

import javax.media.ControllerEvent;

import javax.media.ControllerListener;

import javax.media.EndOfMediaEvent;

import javax.media.Manager;

import javax.media.MediaLocator;

import javax.media.NoPlayerException;

import javax.media.Player;

import javax.media.PrefetchCompleteEvent;

import javax.media.RealizeCompleteEvent;

import javax.media.Time;

import javax.swing.JCheckBoxMenuItem;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

public class JMFMediaPlayer extends JFrame implements ActionListener,

ControllerListener, ItemListener {

// JMF的播放器

Player player;

// 播放器的视频组件和控制组件

Component vedioComponent;

Component controlComponent;

// 标示是否是第一次打开播放器

boolean first = true;

// 标示是否需要循环

boolean loop = false;

// 文件当前目录

String currentDirectory;

// 构造方法

public JMFMediaPlayer(String title) {

super(title);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e){

// 用户点击窗口系统菜单的关闭按钮

// 调用dispose以执行windowClosed

dispose();

}

public void windowClosed(WindowEvent e){

if (player != null){

// 关闭JMF播放器对象

player.close();

}

System.exit(0);

}

});

// 创建播放器的菜单

JMenu fileMenu = new JMenu("文件");

JMenuItem openMemuItem = new JMenuItem("打开");

openMemuItem.addActionListener(this);

fileMenu.add(openMemuItem);

// 添加一个分割条

fileMenu.addSeparator();

// 创建一个复选框菜单项

JCheckBoxMenuItem loopMenuItem = new JCheckBoxMenuItem("循环", false);

loopMenuItem.addItemListener(this);

fileMenu.add(loopMenuItem);

fileMenu.addSeparator();

JMenuItem exitMemuItem = new JMenuItem("退出");

exitMemuItem.addActionListener(this);

fileMenu.add(exitMemuItem);

JMenuBar menuBar = new JMenuBar();

menuBar.add(fileMenu);

this.setJMenuBar(menuBar);

this.setSize(200, 200);

try {

// 设置界面的外观,为系统外观

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

SwingUtilities.updateComponentTreeUI(this);

} catch (Exception e) {

e.printStackTrace();

}

this.setVisible(true);

}

/**

* 实现了ActionListener接口,处理组件的活动事件

*/

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals("退出")) {

// 调用dispose以便执行windowClosed

dispose();

return;

}

FileDialog fileDialog = new FileDialog(this, "打开媒体文件", FileDialog.LOAD);

fileDialog.setDirectory(currentDirectory);

fileDialog.setVisible(true);

// 如果用户放弃选择文件,则返回

if (fileDialog.getFile() == null){

return;

}

currentDirectory = fileDialog.getDirectory();

if (player != null){

// 关闭已经存在JMF播放器对象

player.close();

}

try {

// 创建一个打开选择文件的播放器

player = Manager.createPlayer(new MediaLocator("file:"

+ fileDialog.getDirectory() + fileDialog.getFile()));

} catch (java.io.IOException e2) {

System.out.println(e2);

return;

} catch (NoPlayerException e2) {

System.out.println("不能找到播放器.");

return;

}

if (player == null) {

System.out.println("无法创建播放器.");

return;

}

first = false;

this.setTitle(fileDialog.getFile());

// 播放器的控制事件处理

player.addControllerListener(this);

// 预读文件内容

player.prefetch();

}

/**

* 实现ControllerListener接口的方法,处理播放器的控制事件

*/

public void controllerUpdate(ControllerEvent e) {

// 调用player.close()时ControllerClosedEvent事件出现。

// 如果存在视觉部件,则该部件应该拆除(为一致起见,

// 我们对控制面板部件也执行同样的操作)

if (e instanceof ControllerClosedEvent) {

if (vedioComponent != null) {

this.getContentPane().remove(vedioComponent);

this.vedioComponent = null;

}

if (controlComponent != null) {

this.getContentPane().remove(controlComponent);

this.controlComponent = null;

}

return;

}

// 如果是媒体文件到达尾部事件

if (e instanceof EndOfMediaEvent) {

if (loop) {

// 如果允许循环,则重新开始播放

player.setMediaTime(new Time(0));

player.start();

}

return;

}

// 如果是播放器预读事件

if (e instanceof PrefetchCompleteEvent) {

// 启动播放器

player.start();

return;

}

// 如果是文件打开完全事件,则显示视频组件和控制器组件

if (e instanceof RealizeCompleteEvent) {

vedioComponent = player.getVisualComponent();

if (vedioComponent != null){

this.getContentPane().add(vedioComponent);

}

controlComponent = player.getControlPanelComponent();

if (controlComponent != null){

this.getContentPane().add(controlComponent, BorderLayout.SOUTH);

}

this.pack();

}

}

// 处理“循环”复选框菜单项的点击事件

public void itemStateChanged(ItemEvent e) {

loop = !loop;

}

public static void main(String[] args){

new JMFMediaPlayer("JMF媒体播放器");

}

}

试试吧,我这里运行正常

如何用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如何实现播放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(mp3NOthis.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代码,java编写视频播放器
文章位置:http://ybzwz.com/article/hdocio.html