在BlackBerry上播放音频
播放音频范例
本节介绍如何在BlackBerry上创建一个音频播放器播放mp3,并提供播放,暂停,停止等控制。播放器如图所示:
这个播放器应用运行在BlackBerry 9700模拟器上,界面显示了歌曲名称,歌曲时长,播放状态,和操作按钮暂停,播放,和停止。如下几幅图显示其播放状态的变化:
播放器状态为“正在播放”,歌曲All Rise正在被播放。
播放器状态为暂停,歌曲暂停播放,但是按下播放按钮可以继续播放。
播放器为停止状态。在停止状态,播放器实例不能够被重新利用。再按播放按钮,应用程序会销毁Player实例并创建新的Player进行播放。
播放器源代码如下:
import java.io.InputStream;
import javax.microedition.media.Player;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class MusicPlayerApp extends UiApplication{
public static void main(String[] args){
// 应用程序启动入口
getInstance().enterEventDispatcher();
}
// MusicPlayerApp的单类
private static MusicPlayerApp m_instance;
private MusicPlayerScreen musicPlayerScreen;
public MusicPlayerApp(){
// 创建播放器屏幕
musicPlayerScreen = new MusicPlayerScreen();
// 推出屏幕堆栈,显示播放器屏幕
pushScreen(musicPlayerScreen);
}
// 实现 MusicPlayerApp 单类,运行环境中只有一个MusicPlayerApp实例
public static synchronized MusicPlayerApp getInstance(){
if (m_instance == null){
m_instance = new MusicPlayerApp();
}
return m_instance;
}
// 播放器屏幕
class MusicPlayerScreen extends MainScreen implements FieldChangeListener{
public static final String SONG_NAME = "All Rise";
// 指向歌曲的URI
public static final String URI = "/sounds/all_rise.mp3";
// 歌曲的格式,audio/mpeg支持mp3
public static final String AUDIO_FORMAT = "audio/mpeg";
// 播放器的所有状态
public static final int PLAYING = 0;
public static final int READY_FOR_PLAY = 1;
public static final int PAUSE = 2;
public static final int STOP = 3;
// 播放器的当前状态
private int m_currentStatus = READY_FOR_PLAY;
// 播放器实例
private Player m_player;
// 播放器标签Layout
private VerticalFieldManager m_labelManager;
// 播放器标签
private LabelField m_songNameField;
private LabelField m_durationField;
private LabelField m_statusField;
// 播放按钮Layout
private HorizontalFieldManager m_buttonManager;
// 播放器按钮
private ButtonField m_pauseButton;
private ButtonField m_playButton;
private ButtonField m_stopButton;
public MusicPlayerScreen(){
super(Manager.NO_HORIZONTAL_SCROLL|Manager.NO_VERTICAL_SCROLL);
// 创建播放器并做好播放准备
createPlayer();
// 创建界面
initializeUI();
}
private void initializeUI(){
setTitle(new LabelField("黑莓播放器", Field.FIELD_VCENTER|Field.FIELD_HCENTER));
// 歌曲名称标签
m_songNameField = new LabelField("歌曲名称: "+SONG_NAME, Field.USE_ALL_WIDTH);
// 歌曲时长
m_durationField = new LabelField("播放时长: " +m_player.getDuration()/1000000/60+"分钟", Field.USE_ALL_WIDTH);
// 播放状态
m_statusField = new LabelField("播放状态: "+getStatus(), Field.USE_ALL_WIDTH);
m_labelManager = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL|Manager.NO_VERTICAL_SCROLL);
m_labelManager.add(m_songNameField);
m_labelManager.add(m_durationField);
m_labelManager.add(m_statusField);
add(m_labelManager);
// 暂停按钮
m_pauseButton = new ButtonField("||", Field.FIELD_VCENTER|Field.FIELD_HCENTER);
m_pauseButton.setChangeListener(this);
// 播放按钮
m_playButton = new ButtonField(">", Field.FIELD_VCENTER|Field.FIELD_HCENTER);
m_playButton.setChangeListener(this);
// 停止按钮
m_stopButton = new ButtonField("O", Field.FIELD_VCENTER|Field.FIELD_HCENTER);
m_stopButton.setChangeListener(this);
m_buttonManager = new HorizontalFieldManager(Manager.NO_HORIZONTAL_SCROLL|Manager.NO_VERTICAL_SCROLL);
m_buttonManager.add(m_pauseButton);
m_buttonManager.add(m_playButton);
m_buttonManager.add(m_stopButton);
add(m_buttonManager);
}
// 获取当前播放状态
private String getStatus(){
switch(m_currentStatus){
case PLAYING:
return "正在播放";
case READY_FOR_PLAY:
return "准备完毕";
case PAUSE:
return "暂停";
case STOP:
return "停止";
}
return null;
}
// 根据设置的播放状态更新状态标签
private void setPlayerStatus(int status){
m_currentStatus = status;
m_statusField.setText("播放状态: "+getStatus());
}
// 创建播放器
private void createPlayer(){
try{
// 关闭当前播放器
if (m_player != null){
m_player.close();
m_player = null;
}
// 创建新播放器
InputStream is = this.getClass().getResourceAsStream(URI);
m_player = javax.microedition.media.Manager.createPlayer(is, AUDIO_FORMAT);
// 预装媒体资源准备播放
m_player.prefetch();
}catch(Exception ex){
ex.printStackTrace();
}
}
// 阻止屏幕save菜单弹出
public boolean onSavePrompt(){
return true;
}
public void fieldChanged(Field field, int context) {
if (field == m_playButton){
// 当播放按钮按下时
// 根据播放器当前状态进行处理
switch (m_currentStatus){
case PLAYING:
this.setPlayerStatus(PLAYING);
return;
case READY_FOR_PLAY:
try{
m_player.start();
}catch(Exception ex){}
setPlayerStatus(PLAYING);
return;
case PAUSE:
try{
m_player.start();
}catch(Exception ex){}
setPlayerStatus(PLAYING);
return;
case STOP:
createPlayer();
try{
m_player.start();
}catch(Exception ex){}
setPlayerStatus(PLAYING);
return;
}
}else if (field == m_pauseButton){
// 当暂停按钮按下时
// 根据播放器当前状态进行处理
switch (m_currentStatus){
case PLAYING:
try{
m_player.stop();
}catch(Exception ex){}
setPlayerStatus(PAUSE);
return;
case READY_FOR_PLAY:
return;
case PAUSE:
return;
case STOP:
try{
m_player.close();
}catch(Exception ex){}
setPlayerStatus(STOP);
return;
}
}else if (field == m_stopButton){
// 当停止按按钮按下时
// 根据播放器当前状态进行处理
switch (m_currentStatus){
case PLAYING:
try{
m_player.stop();
m_player.close();
}catch(Exception ex){}
setPlayerStatus(STOP);
return;
case READY_FOR_PLAY:
try{
m_player.close();
}catch(Exception ex){}
setPlayerStatus(STOP);
return;
case PAUSE:
try{
m_player.close();
}catch(Exception ex){}
setPlayerStatus(STOP);
return;
case STOP:
return;
}
}
}
}
}