//可以在你的j2me项目中,用这个类文件来播放声音
//使用方法:
// source = new source("1.mid");
// source.start();
//我的手机不支持mp3,^_^,没关系,我自己写个程序来放歌听,只是效果差点.
import javax.microedition.media.*;
import javax.microedition.media.player;
import javax.microedition.media.control.volumecontrol;
import java.io.*;
/**
* @author 钟伟海
* todo to change the template for this generated type comment go to
* window - preferences - java - code style - code templates
*/
public class source implements playerlistener{
player p;
volumecontrol volc;
/**
* @param filename 声音文件名称
*/
public source(string filename) {
super();
// todo auto-generated constructor stub
//定义用来读取声音文件的inputstream
inputstream is = getclass().getresourceasstream(filename);
if(is == null){
system.out.println("error happens: inputstream not found " + filename);
}
try {
p = manager.createplayer(is, "audio/midi");
if(p == null){
system.out.println("error handler happens: inputstream " + filename);
}else{
p.addplayerlistener(this);
//"具现"播放器
p.realize();
//预取播放器
p.prefetch();
//将播放器设置为无限次播放
p.setloopcount(-1);
system.out.println("realized player: " + filename);
}
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
system.out.println(e);
} catch (mediaexception e) {
// todo auto-generated catch block
e.printstacktrace();
system.out.println(e);
}
//从播放器中获取音量控件
volc = (volumecontrol)p.getcontrol("volumecontrol");
}
/**关闭播放器
*/
public void destroy(){
if(p != null){
p.close();
}
}
/**
* 从"媒体当前播放到的时刻"开始播放
*/
public void start(){
if(p != null){
try {
p.start();
} catch (mediaexception e) {
// todo auto-generated catch block
e.printstacktrace();
system.out.println(e);
}
}
}
/**
* 停止当前播放器,注意当调用stop时
* 播放器不会重绕到开头
*/
public void stop(){
if(p != null && p.getstate() == player.started){
try {
p.stop();
system.out.println("the player stoped here:" + p.getmediatime());
} catch (mediaexception e) {
// todo auto-generated catch block
e.printstacktrace();
system.out.println(e);
}
}
}
/**
* 将媒体当前播放到的时刻设置为负值
* 使媒体重绕到开头位置开始播放
*/
public void rewind(){
if(p != null & p.getstate() == player.prefetched){
try {
p.setmediatime(-1);
p.start();
} catch (mediaexception e) {
// todo auto-generated catch block
e.printstacktrace();
system.out.println(e);
}
}
}
/**更新播放器的音量
* @param level
* @return
*/
public int setlevel(int level){
return volc.setlevel(level);
}
/** 响应playerlistener接口的预定义标准事件
* @see javax.microedition.media.playerlistener#playerupdate(javax.microedition.media.player, java.lang.string, java.lang.object)
*/
public void playerupdate(player p, string event, object eventdata){
// todo auto-generated method stub
if(event == started){
system.out.println("mysound player started");
}else if(event == closed){
system.out.println("mysound player closed");
}else if(event == end_of_media){
system.out.println("mysound player end_of_media");
}else if(event == error){
system.out.println("mysound player error");
}else if(event == volume_changed){
system.out.println("mysound player volume_changed");
}else if(event == duration_updated){
system.out.println("mysound player duration_updated");
}
}
}
播放声音文件的完整源代码
最新推荐文章于 2018-09-03 17:38:19 发布