本文目录一览:
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如何利用url下载MP3保存到本地?
Java如何利用url下载MP3保存的方法:
1 /** ;
2 * TODO 下载文件到本地 ;
3 * @author nadim ;
4 * @date Sep 11, 2015 11:45:31 AM ;
5 * @param fileUrl 远程地址 ;
6 * @param fileLocal 本地路径 ;
7 * @throws Exception ;
8 */ ;
9 public void downloadFile(String fileUrl,String fileLocal) throws Exception {;
10 URL url = new URL(fileUrl);
11 HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
12 urlCon.setConnectTimeout(6000);
13 urlCon.setReadTimeout(6000);
14 int code = urlCon.getResponseCode();
15 if (code != HttpURLConnection.HTTP_OK) {
16 throw new Exception(“文件读取失败”);
17 }
18 //读文件流;
19 DataInputStream in = new DataInputStream(urlCon.getInputStream());
20 DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal));
21 byte[] buffer = new byte[2048];
22 int count = 0;
23 while ((count = in.read(buffer)) 0) {;
24 out.write(buffer, 0, count);
25 }
26 out.close();
27 in.close();
28 }。
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。
Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。
java写mp3播放器
————–不支持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 ();
}
}
}
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/198675.html