package com.bird.jmf;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.Manager;
import javax.media.Player;
@SuppressWarnings("restriction")
public class SimpleAudioPlayer {
private Player audioPlayer = null;//建立一个播放接口
public SimpleAudioPlayer(URL url) throws Exception{//创建一个准备Player,准备好播放
audioPlayer = Manager.createRealizedPlayer(url);
}
@SuppressWarnings("deprecation")
public SimpleAudioPlayer(File file) throws MalformedURLException, Exception{//将本地文件改为URL
this(file.toURL());
}
public void play(){//直接调用播放方法就可以
audioPlayer.start();
}
public void stop(){//停止的时候一定要释放资源
audioPlayer.stop();
audioPlayer.close();
}
public static void main(String [] args) throws MalformedURLException, Exception{
File file = new File("d://1.mp3");
SimpleAudioPlayer Player = new SimpleAudioPlayer(file);
Player.play();
}
}
这就是最简单的音乐播放,没有图形界面,但是播放音乐还是没问题的
博客给出了使用Java和JMF播放背景音乐的代码示例。定义了SimpleAudioPlayer类,包含创建播放接口、播放和停止等方法,通过将本地文件转换为URL实现音乐播放,虽无图形界面,但能正常播放音乐。

被折叠的 条评论
为什么被折叠?



