博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
音乐播放类
阅读量:4045 次
发布时间:2019-05-24

本文共 1925 字,大约阅读时间需要 6 分钟。

/*

 * localaudioplayer.java
 *
 * created on 2006年10月9日, 下午10:52
 *
 * to change this template, choose tools | options and locate the template under
 * the source creation and management node. right-click the template and choose
 * open. you can then make changes to the template in the source editor.
 */

 

package netbeans.david.mah_jong;

import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;
/**
 *作者:戴俊华 软件大三的游戏方向学生
 *这个类实现了本地音乐资源的播放,以及状态的转换
 *同时播放器的参数也可以设置
 *希望有用
 *转载请带上本地url以及作者
 */
public class localaudioplayer{
    public player player;
    public  string filename;
    public string format;
    public boolean succ=true;
    public localaudioplayer(string filename,string format, boolean loadfile)
   {
      this.format= format;
      this.filename = filename;
      if(loadfile)
        loadfromeresource();//加载资源
   }
   public localaudioplayer(string filename, string format)
   {
      this.format = format;
      this.filename =filename;
   }
  
    public localaudioplayer()
   {
      this.format = null;
      this.filename =null;
   }

 

   public void loadfromeresource() {

    try {
      inputstream is = getclass().getresourceasstream(filename);///把资源加载进输入流
      player = manager.createplayer(is, format);
    }
    catch(ioexception ex){
        succ=false;
    }
    catch(mediaexception ex){
        succ=false;
    }
  }
  public void setloop()//设置一直循环播放
  {
    if(player!=null)
        player.setloopcount(-1);
  }
  public void setvolume(int level)//音量控制 level范围是0--100
  {
    if(player!=null){
      volumecontrol control = (volumecontrol)player.getcontrol("volumecontrol");
      control.setlevel(level);
     }
  }
  public void stop()
  {
    if(player!=null){
      try{
        player.stop();
       }catch(mediaexception ex){

 

       }

    }
  }
  public void play()
  {
    if(player!=null){
      try{
        player.deallocate();//实现播放器的状态装换
        player.realize();
        player.prefetch();
        player.start();
      }catch(mediaexception ex){
        succ=false;
      }
    }
  }
  public void replay()//重新播放一次,先释放资源再加载进入
  {
    close();
    system.gc();//释放
    loadfromeresource();//加载
    play();
  }
  public void close()
  {
    if(player!=null){
     player.close();//关闭播放器
     player=null;
    }
  }
}

你可能感兴趣的文章
枚举用法详解
查看>>
枚举用法详解
查看>>
设计模式常见面试题
查看>>
curl
查看>>
hibernateTemplate常用方法
查看>>
windows下查看端口监听情况
查看>>
windows下mongodb安装
查看>>
win7下安装mongodb
查看>>
mongodb使用笔记
查看>>
ZeroMQ研究与应用分析
查看>>
mongodb--nodejs
查看>>
linux vi 操作笔记
查看>>
使用TortoiseGit对Git版本进行分支操作
查看>>
Linux tcpdump命令详解
查看>>
mysql日期格式化
查看>>
MySQL数据库中的Date,DateTime,TimeStamp和Time类型
查看>>
MySQL索引
查看>>
mongodb 的重启
查看>>
Mongoose - 让NodeJS更容易操作Mongodb数据库
查看>>
module.exports与exports的区别
查看>>