我想创建一个简单的黑莓应用程序,它在我为手机充电时播放音频文件,并在我拔下它时关闭应用程序。
伪代码
插入电池电缆后启动应用程序,
充电时应用程序连续播放声音无法让它在没有中间间隙的情况下循环播放,而是播放声音
如果用户拔掉电缆,停止流,播放声音,停止流
可选:如果电池电量下降到临界/完成充电,播放声音
浏览文档,我认为没有听众可以告诉您电池电量是否为 100%。
编辑:通过batteryStatusChange找到了一种方法,感谢Nate帮助我
有空异常错误。
编辑:使用 InputStream 并且没有更多的空异常错误。将 wav 文件添加到 res 文件夹。下面的新代码以 100 的频率播放声音和两种不同的声音,一种用于 USB 连接,另一种用于 USB 断开连接。
public class HelloBlackBerryScreen extends MainScreen implements SystemListener2 {
private BasicEditField basicEditField;
private Player HEV;
private String wav;
private InputStream stream;
private int volume; //going to set volume from GUI using a drop down list, working on it currently
public HelloBlackBerryScreen()
{
super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
setTitle( "HelloBlackBerry" );
add(new RichTextField("Battery", RichTextField.TEXT_ALIGN_HCENTER));
Application.getApplication().addSystemListener(this);
wav = "voice_on.wav";
stream = (InputStream)this.getClass().getResourceAsStream("/" + wav);
try {
HEV = Manager.createPlayer(stream, "audio/wav");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void batteryGood() {
// TODO Auto-generated method stub
}
public void batteryLow() {
// TODO Auto-generated method stub
}
public void batteryStatusChange(int status)
{
// TODO Auto-generated method stub
if ((status & DeviceInfo.BSTAT_LEVEL_CHANGED) != 0)
{
if(DeviceInfo.getBatteryLevel() == 100)
{
try
{
setWav("power_level_is_100_percent.wav");
HEV.start();
stream.close();
}
catch (MediaException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void powerOff() {
// TODO Auto-generated method stub
}
public void powerUp() {
// TODO Auto-generated method stub
}
public void backlightStateChange(boolean on) {
// TODO Auto-generated method stub
}
public void cradleMismatch(boolean mismatch) {
// TODO Auto-generated method stub
}
public void fastReset() {
// TODO Auto-generated method stub
}
public void powerOffRequested(int reason) {
// TODO Auto-generated method stub
}
public void usbConnectionStateChange(int state)
{
// TODO Auto-generated method stub
if (state == USB_STATE_CABLE_CONNECTED)
{
try
{
setWav("suitchargeok1.wav");
HEV.start();
stream.close();
}
catch (MediaException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (state == USB_STATE_CABLE_DISCONNECTED)
{
try
{
stream.close();
setWav("battery_pickup.wav");
HEV.start();
stream.close();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String getWav() {
return wav;
}
public void setWav(String wav) {
this.wav = wav;
stream = (InputStream)this.getClass().getResourceAsStream("/" + this.wav);
try {
HEV = Manager.createPlayer(stream, "audio/wav");
HEV.realize();
VolumeControl volume = (VolumeControl) HEV.getControl("VolumeControl");
volume.setLevel(25);
HEV.prefetch();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean onClose()
{
UiApplication.getUiApplication().requestBackground();
return true;
}
}