java音视频播放器代码 java实现视频播放器

用java做音视频播放器我做过一个MP3的..给你看看.
package music;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.media.bean.playerbean.*; //这个包要用到JMF
public class MP3 extends JFrame implements Runnable {
public JLabel shijian;
private JSlider sldDiameter;
public MediaPlayer soumd1;
public JButton playSound, loopSound, stopsound;
public JButton tjian, shanc, baocun, duqu;
public JPanel jp1, jp2, jp3, jp4;
public JList jl;
int zongmiao=0;
public Vector vector, mingcheng;
【java音视频播放器代码 java实现视频播放器】 boolean fo = false, geshi = false;
JLabel jl1, jl2, sj1, sj2;
JTextField jt1, jt2;
JButton queding, xiugai;
int zong = 0;
int a = 0, b = 0, you = 1,mm=0;
int fenzhong, miaozhong;
public MP3() {
super(“java简单音乐播放器“);
soumd1 = new MediaPlayer();
Container c = getContentPane();
c.setLayout(new FlowLayout());
mingcheng = new Vector();
jp1 = new JPanel();
shijian = new JLabel();
jp1.add(shijian);
c.add(jp1);
playSound = new JButton(“开始播放“);
loopSound = new JButton(“循环播放“);
stopsound = new JButton(“停止播放“);
jp2 = new JPanel();
jp2.add(playSound);
jp2.add(loopSound);
jp2.add(stopsound);
c.add(jp2);
jp4 = new JPanel();
sj1 = new JLabel();
sj2 = new JLabel();
sldDiameter = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0);
sldDiameter.setMajorTickSpacing(1);
sldDiameter.setPaintTicks(true);
jp4.add(sj1);
jp4.add(sldDiameter);
jp4.add(sj2);
c.add(jp4);
vector = new Vector();
jl = new JList(mingcheng);
jl.setVisibleRowCount(5);
jl.setFixedCellHeight(40);
jl.setFixedCellWidth(265);
jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
c.add(new JScrollPane(jl));
tjian = new JButton(“添加“);
shanc = new JButton(“删除“);
duqu = new JButton(“读取“);
baocun = new JButto
摘选自网上,希望对你有帮助 。
求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 {
// JMFjava音视频播放器代码的播放器
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 {
// 设置界面的外观java音视频播放器代码 , 为系统外观
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来编写一个音乐播放器首先要在环境电脑中安装下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 = https://www.04ip.com/post/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 音乐播放的代码啊 , 完美运行的就行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; //循环播放
private JButton buttinzi=null;//停止
private JButton butshan=null;//上一首
private JButton butzhantin=null; //暂停
private JButton butxia=null;//下一首
private TrayIcon trayIcon;//托盘图标
private SystemTray systemTray;//系统托盘
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("爱不再来");
box1.addItem("抽身");
box1.addItem("伤心城市");
box1.addItem("二零一二");
box1.addItem("精忠报国");
box1.addItem("秋沙");
box1.addItem("吻别");
box1.addItem("音乐疯起来");
box1.setBounds(10,20,150,20);
butbofan=new JButton("播放");
butbofan.addActionListener(this);
butbofan.setBounds(165,50,60,20);
butboxhuan=new JButton("循环播放");
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();//获得系统托盘的实例
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
trayIcon = new TrayIcon(ImageIO.read(new File("004.jpg")));
systemTray.add(trayIcon);//设置托盘的图标,0.gif与该类文件同一目录
}
catch (IOException e1)
{
e1.printStackTrace();
}
catch (AWTException e2)
{
e2.printStackTrace();
}
this.addWindowListener(
new WindowAdapter(){
public void windowIconified(WindowEvent e)
{
dispose();//窗口最小化时dispose该窗口
}
});
trayIcon.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 2)//双击托盘窗口再现
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("内存溢出");
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("内存溢出");
er.printStackTrace();
} catch(Exception ex){
ex.printStackTrace();
}
looping = true;
chosenClip.loop();
butboxhuan.setEnabled(false);
buttinzi.setEnabled(true);
butzhantin.setText("暂停");
this.setTitle("正在循环播放" (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("继续");
if(source==butzhantin)
{
butzhantin.setText("暂停");
}
}
if(source==butxia)
{
butzhantin.setText("暂停");
}
}
public static void main(String[] args)
{
bofan_2 xx=new bofan_2();
}
}
/*
可以用加载声音文件的方法:
第一?。簃ysound= new Sound();
mysound.attachSound(声音id名字);
ptime = 0;
播放按钮as:
on(release){
mysound.start(ptime);
}
暂停按钮as:
on(release){
ptime = mysound.position/1000;
mysound.stop();
}
*/
如何在java应用程序中播放音频文件在 applet 中播放声音文件非常简单,一般需要以下步骤:创建一个 AudioClip 对象
装入 .au 声音文件到 AudioClip 对象
一次播放或者不停循环播放声音
停止播放
下面是相应的代码:import java.applet.*;AudioClip ac = getAudioClip(getCodeBase(), soundFile);
ac.play(); //play once
ac.stop(); //stop playing
解决这个问题的窍门是利用由 Sun 及 其JDK 提供的某些 undocumented 的特征 。先看看 Sun JDK 中的文件 classes.zip (使用任何解压工具即可),发现其中不仅包含标准的 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);如果要用一个 URL 做为声音流的源(source),则用下面的代码所示替换输入流来创建声音流:AudioStream as = new AudioStream (url.openStream());如果需要持续播放声音文件 , 则要稍稍复杂一点:// Create audio stream as discussed previously.
// Create AudioData source.
AudioData data = https://www.04ip.com/post/as.getData();// Create ContinuousAudioDataStream.
ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);// Play audio.
java音视频播放器代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现视频播放器、java音视频播放器代码的信息别忘了在本站进行查找喔 。

    推荐阅读