我使用此代码(我在 stackoverflow 上找到)实现了一个简单的 MediaController。我的第一个问题是我希望MediaController
始终可见。我可以覆盖该hide()
方法,但此时我无法使用 BACK 按钮退出我的应用程序。我读到覆盖dispatchKeyEvent()
可能会解决问题,但它对我没有影响。
实际上我的第二个问题是使用它时如何制作活动按钮?媒体播放器中没有新按钮,但我希望它们采用另一种布局(我将有大约 15 个不同的按钮)。如果我LinearLayout
在我的 xml 中创建一个新的,那么在媒体控制器出现在屏幕上之前,带有按钮的布局是不活动的。touchEvent 使MediaController
消失,然后我可以按下按钮。MedaiController
是否可以同时在屏幕上拥有活动按钮和永不消失的按钮?
public class PlayAudioActivity extends Activity implements MediaPlayerControl {
private static final String PLAY_AUDIO = "PLAY_AUDIO";
private MediaController mMediaController;
private MediaPlayer mMediaPlayer;
private Handler mHandler = new Handler();
static Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=this;
mMediaPlayer = new MediaPlayer();
mMediaController = new MediaController(this)
/* {
@Override
public void hide()
{
mMediaController.show(0);
}
}*/;
mMediaController.setMediaPlayer(PlayAudioActivity.this);
mMediaController.setAnchorView(findViewById(R.id.audioView));
// String audioFile = "" ;
try {
mMediaPlayer.setDataSource(this,Uri.parse("android.resource://com.example.audioplayer/raw/song"));
mMediaPlayer.prepare();
} catch (IOException e) {
Log.e(PLAY_AUDIO, "Could not open file " + " " + " for playback.", e);
}
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mHandler.post(new Runnable() {
public void run() {
mMediaController.show(0);
mMediaPlayer.start();
}
});
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mMediaPlayer.stop();
mMediaPlayer.release();
}
@Override
public boolean canPause() {
return true;
}
@Override
public boolean canSeekBackward() {
return false;
}
@Override
public boolean canSeekForward() {
return false;
}
@Override
public int getBufferPercentage() {
int percentage = (mMediaPlayer.getCurrentPosition() * 100) / mMediaPlayer.getDuration();
return percentage;
}
@Override
public int getCurrentPosition() {
return mMediaPlayer.getCurrentPosition();
}
@Override
public int getDuration() {
return mMediaPlayer.getDuration();
}
@Override
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
@Override
public void pause() {
if(mMediaPlayer.isPlaying())
mMediaPlayer.pause();
}
@Override
public void seekTo(int pos) {
mMediaPlayer.seekTo(pos);
}
@Override
public void start() {
mMediaPlayer.start();
}
/*public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
finish();
return super.dispatchKeyEvent(event);
}*/
@Override
public boolean onTouchEvent(MotionEvent event) {
mMediaController.show(0);
return false;
}
}
我的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/audioView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:id="@+id/audioView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>