⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audioplayer.java

📁 经典FC游戏《超惑星战记》的J2ME版本!!功能基本上都实现了
💻 JAVA
字号:
/**
 * <p>Title: Transpanzer</p>
 * <p>Description:
 * You cannot remove this copyright and notice.
 * You cannot use this file any part without the express permission of the author.
 * All Rights Reserved</p>
 * @author Jjyo
 * @email jjyo@163.com
 * @version 1.0.0
 */

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;


public class AudioPlayer {
	  private Player player;	//媒体接口
	  private String filename;	//播放的媒体文件名
	  private String format;	//播放的媒体的格式

	  public AudioPlayer(String filename,String format,boolean isLoad)
	  {
	    this.format = format;
	    this.filename = filename;
	    if(isLoad)
	    {
	      loadResource();
	    }
	  }
	  public AudioPlayer(String filename,String format)
	  {
	    this.format = format;
	    this.filename = filename;
	  }


	  public void loadResource()
	  {
	    try
	    {
	      InputStream is=getClass().getResourceAsStream("/"+filename);
	      player=Manager.createPlayer(is,format);
	    }
	    catch(IOException ex)
	    {
	      System.out.println("can't load "+filename);
	      System.out.println(ex.toString());
	    }
	    catch(MediaException ex)
	    {
	      System.out.println("can't create audio");
	      System.out.println(ex.toString());
	    }
	  }
	  
	  //设置循环播放
	  public void setLoop()	
	  {
	    if(player!=null)
	    {
	      player.setLoopCount(-1);	//设置无限循环
	    }
	  }

	  public void stop()
	  {
	    if(player!=null)
	    {
	      try
	      {
	        player.stop();
	      }
	      catch(MediaException ex)
	      {
	        System.out.println("can't stop audio");
	        System.out.println(ex.toString());
	      }
	    }
	  }

	  public void play()	//播放声音
	  {
	    if(player!=null)
	    {
	      try
	      {
	        player.realize();
	        player.start();
	      }
	      catch(MediaException ex)
	      {
	        System.out.println("can't play audio");
	        System.out.println(ex.toString());
	      }
	    }
	  }
	  public void replay()	//重新播放声音
	  {
	    close();
	    System.gc();
	    loadResource();
	    play();
	  }

	  public void close()	//关闭声音接口
	  {
	    if(player!=null)
	    {
	      player.close();
	      player=null;
	    }
	  }
	  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -