【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“LPAudioPlayer.java”一文,供大家参考学习
/**
* <p>Title: lipeng</p>
* <p>Description:
* You cannot remove this copyright and notice.
* You cannot use this file without the express permission of the author.
* All Rights Reserved</p>
* <p>Copyright: lizhenpeng (c) 2004</p>
* <p>Company: LP&P</p>
* @author lizhenpeng
* @version 1.3.0
* <p>
* Revise History
* 2004.07.12 Revise exception mode and exception description V1.1.0
* 2004.07.20 Change audio loop mode to thread mode v1.2.0
* 2004.07.20 revise mode to test pass mode V1.2.1
* 2004.09.25 add a method replay media,add two data member to load resource,
* add a method to load resource,
* v1.3.0
* </p>
*/
package exframework;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;
public class LPAudioPlayer implements Runnable
{
private Player player;
private String filename;
private String format;
public LPAudioPlayer(String filename,String format,boolean isLoad)
{
this.format = format;
this.filename = filename;
if(isLoad)
{
loadResource();
}
}
public LPAudioPlayer(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 setVolume(int level)
{
if(player!=null)
{
VolumeControl control = (VolumeControl)player.getControl("VolumeControl");
control.setLevel(level);
}
}
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.prefetch();
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 run()
{
replay();
}
public void threadPlay()
{
new Thread(this).start();
}
public void close()
{
if(player!=null)
{
player.close();
player=null;
}
}
}
上一篇:
lozengeStar.java
下一篇:
美术教学中网络信息论文