资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

mp3java代码的简单介绍

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编写MP3播放器程序

import java.applet.AudioClip; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; // 声音播放程序 public class AudioPlayDemo extends JFrame implements ActionListener, ItemListener { boolean looping = false; //是否循环播放 String[] choics = { "chimes.wav", "start.wav" }; //声音文件名数组 URL file1 = getClass().getResource(choics[0]); //声音文件1 URL file2 = getClass().getResource(choics[1]); //声音文件2 AudioClip sound1 = java.applet.Applet.newAudioClip(file1); //声音剪辑对象1 AudioClip sound2 = java.applet.Applet.newAudioClip(file2); //声音剪辑对象2 AudioClip chosenClip = sound1; //选择的声音剪辑对象 JComboBox jcbFiles = new JComboBox(choics); //文件选择组合框 JButton playButton = new JButton("播放"); //播放按钮 JButton loopButton = new JButton("循环播放"); //循环播放按钮 JButton stopButton = new JButton("停止"); //停止播放按钮 JLabel status = new JLabel("选择播放文件"); //状态栏标签 JPanel controlPanel = new JPanel(); //控制面板用于包容按钮 Container container = getContentPane(); //获得窗口内容窗格 public AudioPlayDemo() { //构造器 super("声音播放程序"); //调用父类构造器设置窗口标题栏 jcbFiles.setSelectedIndex(0); //设置组合框选择项 jcbFiles.addItemListener(this); //为播放按钮添加项目监听器 //为播放按钮、循环播放按钮、停止播放按钮添加动作监听器 playButton.addActionListener(this); loopButton.addActionListener(this); stopButton.addActionListener(this); stopButton.setEnabled(false); //设置停止播放按钮不可用 //把播放按钮、循环播放按钮、停止播放按钮加入控制面板 controlPanel.add(playButton); controlPanel.add(loopButton); controlPanel.add(stopButton); //把文件选择组合框、控制面板、状态栏标签加入到窗口内容窗格 container.add(jcbFiles, BorderLayout.NORTH); container.add(controlPanel, BorderLayout.CENTER); container.add(status, BorderLayout.SOUTH); setSize(300, 130); //设置窗口大小 setVisible(true); //设置窗口可视 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序 } //文件选择组合框事件处理 public void itemStateChanged(ItemEvent e) { if (jcbFiles.getSelectedIndex() == 0) { chosenClip = sound1; } else { chosenClip = sound2; } } //按钮事件处理 public void actionPerformed(ActionEvent event) { if (chosenClip == null) { status.setText("声音未载入"); return; //如果AudioClip对象为空,则直接返回 } 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("正在循环播放"); //设置状态栏信息 } //停止播放按钮事件处理 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(); //创建AudioPlayDemo对象 } }//图片和音乐放在与java.class通目录下

java播MP3的代码

简单写个小例子

import javax.media.ControllerEvent;

import javax.media.ControllerListener;

import javax.media.NoPlayerException;

import javax.media.Player;

import javax.media.Manager;

import javax.media.MediaLocator;

import javax.media.EndOfMediaEvent;

import javax.media.PrefetchCompleteEvent;

import javax.media.RealizeCompleteEvent;

import java.io.*;

import java.util.*;

public class PlayerMusic implements ControllerListener {

//播放对象

private Player player;

//是否循环播放

private boolean first,loop;

//文件路径

private String path;

//存放MP3文件

private ListString mp3List;

//当前MP3文件数量

private int mp3NO=0;

PlayerMusic(ListString mp3List)

{

this.mp3List=mp3List;

}

//播放方法

public void start()

{

try {

player = Manager.createPlayer(new File(mp3List.get(mp3NO)).toURI().toURL());

} catch (NoPlayerException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

System.out.println("不能播放此文件!");

return;

} catch (IOException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

return;

}

if(player==null)

{

System.out.println("播放文件为空!");

return;

}

player.addControllerListener(this);

//提取媒体内容

player.prefetch();

}

public void controllerUpdate(ControllerEvent e) {

//当媒体播放结束时,循环播放

if(e instanceof EndOfMediaEvent)

{

mp3NO++;

System.out.println(mp3NO);

if(mp3NOmp3List.size())

{

this.start();

}

return;

}

//当提取媒体的内容结束

if (e instanceof PrefetchCompleteEvent) {

System.out.println("内容结束");

player.start();

return;

}

//当实例化后

if (e instanceof RealizeCompleteEvent) {

System.out.println("实例化");

//pack(); //执行pack()操作

return;

}

}

public static void main(String[] args)

{

ListString path=new ArrayListString();

path.add("F:\\歌曲\\天下.mp3");

path.add("F:\\歌曲\\画心.mp3");

path.add("F:\\歌曲\\只对你有感觉.mp3");

PlayerMusic play=new PlayerMusic(path);

play.start();

}

}

怎样用JAVA流来分割一个mp3文件代码

package xuan;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.nio.Buffer;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

public class mp3 {

public static void cutMusic() throws IOException{

File file=new File("E:\\薛之谦 - 那是你离开了北京的生活.flac");

File file2=new File("E:\\music");

FileInputStream fis =new FileInputStream(file);

FileOutputStream fos=null;

//if(file2.exists()!=true) {

// file2.mkdirs();

//}

int len=0;

int x=0;

int y=1020*1024;

byte [] one=new byte[y];

if(file.length()%y!=0) {

x=(int)(file.length()/y+1);

}else if(file.length()%y==0) {

x=(int)(file.length()/y);

}

for(int i=1;i=x;i++) {

len=fis.read(one);

fos=new FileOutputStream (new File(file2,i+".flac"));

fos.write(one,0,len);

}

fis.close();

fos.close();

}

public static void mergeMusic()throws IOException{

File file=new File("E:\\merge.flac");

File file2=new File("E:\\music");

// if(file.exists()!=true) {

// file.createNewFile();

// }

File[]f=file2.listFiles();

FileInputStream fis=null;

FileOutputStream fos=new FileOutputStream(file);

BufferedOutputStream bos =new BufferedOutputStream(fos,1024*1024);

int len=0;

for(int i=0;if.length;i++) {

fis =new FileInputStream(f[i]);

BufferedInputStream bis =new BufferedInputStream(fis,1024*1024);

while((len=bis.read())!=-1) {

bos.write(len);

}

fos.flush();

fis.close();

}

bos.close();

fos.close();

}

public static void main(String[] args) throws IOException{

cutMusic();

mergeMusic();

// TODO Auto-generated method stub

}

}


文章标题:mp3java代码的简单介绍
当前路径:http://cdkjz.cn/article/dodddge.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220