我在片段中创建了一个媒体播放器。我把它初始化为:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Main.mp = new MediaPlayer();
}
首先,当按下这首歌还在播放时我遇到了问题。为什么添加以下代码: -
public void onPause() {
super.onPause();
if (Main.prepared && !error) {
if (Main.mp.isPlaying()) {
Main.mp.stop();
}
}
}
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
}
// super.onBackPressed();
}
现在当我按下它时会停止媒体播放器。但是,当我按下那首歌并按下播放按钮时,它会再次崩溃。我得到这个错误:
08-14 12:07:16.650: E/AndroidRuntime(9913): FATAL EXCEPTION: main
08-14 12:07:16.650: E/AndroidRuntime(9913): java.lang.IllegalStateException
08-14 12:07:16.650: E/AndroidRuntime(9913): at android.media.MediaPlayer.isPlaying(Native Method)
08-14 12:07:16.650: E/AndroidRuntime(9913): at com.fragment.StreamFragment$7.run(StreamFragment.java:290)
08-14 12:07:16.650: E/AndroidRuntime(9913): at android.os.Handler.handleCallback(Handler.java:615)
08-14 12:07:16.650: E/AndroidRuntime(9913): at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 12:07:16.650: E/AndroidRuntime(9913): at android.os.Looper.loop(Looper.java:213)
08-14 12:07:16.650: E/AndroidRuntime(9913): at android.app.ActivityThread.main(ActivityThread.java:4787)
08-14 12:07:16.650: E/AndroidRuntime(9913): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 12:07:16.650: E/AndroidRuntime(9913): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 12:07:16.650: E/AndroidRuntime(9913): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-14 12:07:16.650: E/AndroidRuntime(9913): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-14 12:07:16.650: E/AndroidRuntime(9913): at dalvik.system.NativeStart.main(Native Method)
编写的代码(StreamFragment.java:290)是:
// set seeker to follow media player
handler = new Handler();
r = new Runnable() {
@Override
public void run() {
if (Main.prepared) {
if (Main.mp.isPlaying() ) {
seeker.setProgress(Main.mp
.getCurrentPosition());
}
}
seeker.postDelayed(r, 200);
}
};
handler.postDelayed(r, 1000);
答案 0 :(得分:0)
你必须在片段中按下键,检查键码。 onBackPress()方法可以在Activity中使用 - 它是片段的逻辑父级。
试试这个:
frag.getView().setFocusableInTouchMode(true);
frag.getView().setOnKeyListener( new OnKeyListener(){
@Override
public boolean onKey( View v, int keyCode, KeyEvent event ){
if( keyCode == KeyEvent.KEYCODE_BACK ){
// your media player code
if (getFragmentManager().getBackStackEntryCount() > 0)
{
getFragmentManager().popBackStack();
}
return true;
}
return false;
}
} );
答案 1 :(得分:0)
试试这个
MediaPlayer mediaPlayer;
Button buttonPlayPause, buttonQuit;
TextView textState;
private int stateMediaPlayer;
private final int stateMP_NotStarter = 0;
private final int stateMP_Playing = 1;
private final int stateMP_Pausing = 2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonPlayPause = (Button)findViewById(R.id.playpause);
buttonQuit = (Button)findViewById(R.id.quit);
textState = (TextView)findViewById(R.id.state);
buttonPlayPause.setOnClickListener(buttonPlayPauseOnClickListener);
buttonQuit.setOnClickListener(buttonQuitOnClickListener);
initMediaPlayer();
}
private void initMediaPlayer()
{
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(AndroidMediaPlayer.this, R.raw.music);
stateMediaPlayer = stateMP_NotStarter;
textState.setText("- IDLE -");
}
Button.OnClickListener buttonPlayPauseOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(stateMediaPlayer){
case stateMP_NotStarter:
mediaPlayer.start();
buttonPlayPause.setText("Pause");
textState.setText("- PLAYING -");
stateMediaPlayer = stateMP_Playing;
break;
case stateMP_Playing:
mediaPlayer.pause();
buttonPlayPause.setText("Play");
textState.setText("- PAUSING -");
stateMediaPlayer = stateMP_Pausing;
break;
case stateMP_Pausing:
mediaPlayer.start();
buttonPlayPause.setText("Pause");
textState.setText("- PLAYING -");
stateMediaPlayer = stateMP_Playing;
break;
}
}
};
Button.OnClickListener buttonQuitOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mediaPlayer.stop();
mediaPlayer.release();
finish();
}
};
}