扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
// 你看看吧。。 必须下载 jmf包 如果不知道下载就问我吧
彝良ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
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());
}
}
}
--------------不支持MP3---------------------
public AudioClip loadSound(String filename) { // 返回一个AudioClip对象
URL url = null; // 因为newAudioClip()的参数为URL型
try {
url = new URL("file:" + filename); // 指定文件,“file:"不能少
} catch (MalformedURLException e) {
}
return JApplet.newAudioClip(url); // 通过newAudioClip(
// )方法装载声音,此方法为JDK后添加的方法,太老的JDK里可能没有
}
AudioClip s1 = loadSound("声音//TableStopGetPrice.wav");
s1.play();
------------------支持mp3--------------------------
见附件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.Decoder;
import javazoom.jl.decoder.Header;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.decoder.SampleBuffer;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
/**
* The codePlayer/code class implements a simple player for playback of an
* MPEG audio stream.
*
* @author Mat McGowan
* @since 0.0.8
*/
public class Player
{
/**
* The current frame number.
*/
private int frame = 0;
/**
* The MPEG audio bitstream.
*/
// javac blank final bug.
/* final */private Bitstream bitstream;
/**
* The MPEG audio decoder.
*/
/* final */private Decoder decoder;
/**
* The AudioDevice the audio samples are written to.
*/
private AudioDevice audio;
/**
* Has the player been closed?
*/
private boolean closed = false;
/**
* Has the player played back all frames from the stream?
*/
private boolean complete = false;
private int lastPosition = 0;
/**
* Creates a new codePlayer/code instance.
*/
public Player ( InputStream stream ) throws JavaLayerException
{
this (stream, null);
}
public Player ( InputStream stream, AudioDevice device ) throws JavaLayerException
{
bitstream = new Bitstream (stream);
decoder = new Decoder ();
if (device != null)
{
audio = device;
}
else
{
FactoryRegistry r = FactoryRegistry.systemRegistry ();
audio = r.createAudioDevice ();
}
audio.open (decoder);
}
public void play () throws JavaLayerException
{
play (Integer.MAX_VALUE);
}
/**
* Plays a number of MPEG audio frames.
*
* @param frames
* The number of frames to play.
* @return true if the last frame was played, or false if there are more
* frames.
*/
public boolean play ( int frames ) throws JavaLayerException
{
boolean ret = true;
while (frames-- 0 ret)
{
ret = decodeFrame ();
}
if (!ret)
{
// last frame, ensure all data flushed to the audio device.
AudioDevice out = audio;
if (out != null)
{
out.flush ();
synchronized (this)
{
complete = ( !closed );
close ();
}
}
}
return ret;
}
/**
* Cloases this player. Any audio currently playing is stopped immediately.
*/
public synchronized void close ()
{
AudioDevice out = audio;
if (out != null)
{
closed = true;
audio = null;
// this may fail, so ensure object state is set up before
// calling this method.
out.close ();
lastPosition = out.getPosition ();
try
{
bitstream.close ();
}
catch (BitstreamException ex)
{}
}
}
/**
* Returns the completed status of this player.
*
* @return true if all available MPEG audio frames have been decoded, or
* false otherwise.
*/
public synchronized boolean isComplete ()
{
return complete;
}
/**
* Retrieves the position in milliseconds of the current audio sample being
* played. This method delegates to the code
* AudioDevice/code that is used by this player to sound the decoded audio
* samples.
*/
public int getPosition ()
{
int position = lastPosition;
AudioDevice out = audio;
if (out != null)
{
position = out.getPosition ();
}
return position;
}
/**
* Decodes a single frame.
*
* @return true if there are no more frames to decode, false otherwise.
*/
protected boolean decodeFrame () throws JavaLayerException
{
try
{
AudioDevice out = audio;
if (out == null)
return false;
Header h = bitstream.readFrame ();
if (h == null)
return false;
// sample buffer set when decoder constructed
SampleBuffer output = (SampleBuffer) decoder.decodeFrame (h, bitstream);
synchronized (this)
{
out = audio;
if (out != null)
{
out.write (output.getBuffer (), 0, output.getBufferLength ());
}
}
bitstream.closeFrame ();
}
catch (RuntimeException ex)
{
throw new JavaLayerException ("Exception decoding audio frame", ex);
}
/*
* catch (IOException ex) {
* System.out.println("exception decoding audio frame: "+ex); return
* false; } catch (BitstreamException bitex) {
* System.out.println("exception decoding audio frame: "+bitex); return
* false; } catch (DecoderException decex) {
* System.out.println("exception decoding audio frame: "+decex); return
* false; }
*/
return true;
}
public static void main ( String[] args )
{
try
{
Player player = new Player (new FileInputStream (new File ("D:\\Youdagames\\JLayer1.0.1\\abc.mp3")));
player.play ();
}
catch (FileNotFoundException e)
{
e.printStackTrace ();
}
catch (JavaLayerException e)
{
e.printStackTrace ();
}
}
}
直接下个JMF,google搜,sun官网上有~~然后安装目录是你的JDK,
再播放MP3文件就:
import javax.media.*;
import java.net.MalformedURLException;
import java.net.URL;
Player player;
File playFile=new File("");//你的MP3文件
try {
player=Manager.createRealizedPlayer(playFile.toURL());
player.prefetch();
player.setMediaTime(new Time(10.0));
player.start();
} catch (NoPlayerException e1)
{
e1.printStackTrace();
} catch (CannotRealizeException e1)
{
e1.printStackTrace();
} catch (MalformedURLException e1)
{
e1.printStackTrace();
} catch (IOException e1)
{
e1.printStackTrace();
}
这种基本代码,自己搜一下,用编译器熟悉熟悉JMF的方法就会了。。。我以前回答别人时的答案~~~
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
}
}
简单的实例,代码如下,纯粹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();
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流