我使用片段获得了带有导航抽屉的应用。从列出所有曲目的片段之一,我尝试运行媒体播放器并显示它的相关歌词。但是我无法处理这个片段的后退按钮。
我在这里所做的只是导航回到之前的片段。但是我回来之后无法点击ListView项目,而且我也无法点击媒体播放器中的导航抽屉或者从媒体播放器片段返回。有想法的人吗?
这是我的代码:
package com.myapp.slidingmenu;
import java.io.IOException;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.AssetFileDescriptor;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.Toast;
import com.myapp.myplayer.R;
public class PlayTrackFragment extends Fragment implements MediaPlayerControl {
protected static final Fragment PlayTrackFragment = null;
public PlayTrackFragment(){}
private MediaController mMediaController;
private MediaPlayer mMediaPlayer;
private Handler mHandler = new Handler();
AssetFileDescriptor fd = null;
int iDispatchCounter = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_playtrack, container, false);
Bundle args = getArguments();
if (args != null && args.containsKey("id_User"))
{
String userId = args.getString("id_User");
Toast.makeText(rootView.getContext(),userId,Toast.LENGTH_SHORT).show();
}
WebView wv;
wv = (WebView) rootView.findViewById(R.id.webViewPlayTrack);
wv.loadUrl("file:///android_asset/traditions.html");
wv.setBackgroundColor(0x00000000);
wv.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
//AssetFileDescriptor fd = expansionFile.getAssetFileDescriptor(mytracks[trackTitle].toString()+".mp3");
AssetFileDescriptor fd = null;
try {
fd = getActivity().getAssets().openFd("aa01.mp3");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (fd == null)
{
Toast.makeText(rootView.getContext(), "Expansion File Not Loaded", Toast.LENGTH_SHORT).show();
//return;
}
mMediaPlayer = new MediaPlayer();
mMediaController = new MediaController(rootView.getContext()){
@Override
public void hide() {
this.show(0);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && iDispatchCounter == 1)
{
iDispatchCounter++;
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
if(mMediaPlayer.isPlaying()){ mMediaPlayer.pause();}
//mMediaPlayer = null;
//mMediaController.hide();
mMediaController.setVisibility(View.GONE);
mMediaController = null;
HomeFragment homefragment = new HomeFragment();
PlayTrackFragment playtrackfragment = new PlayTrackFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentManager fragmentManager2 = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, homefragment).commit();
fragmentTransaction2.remove(playtrackfragment).commit();
//fragmentTransaction.commit();
//mMediaPlayer.stop();
//mMediaPlayer = null;
//mMediaController = null;
//getActivity().finish();
//getActivity().onBackPressed();
//Todo: make finish work
//return true;
//Toast.makeText(rootView.getContext(),"Inside onDispatchKey",Toast.LENGTH_SHORT).show();
Log.i((String) getTag(), "onDispatch keyCode: " + event);
//getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
getFragmentManager().popBackStackImmediate();
return false;
}
return false;
}
};
//Original Lines
//mMediaController.setMediaPlayer(PlayTrack.this);
mMediaController.setBackgroundColor(Color.rgb(40, 40, 80));
mMediaController.setMediaPlayer(PlayTrackFragment.this);
mMediaController.setAnchorView(rootView.findViewById(R.id.layoutPlayTrack));
mMediaPlayer.setVolume(1f, 1f);
try {
mMediaPlayer.setDataSource( fd.getFileDescriptor(), fd.getStartOffset(),fd.getLength());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mHandler.post(new Runnable() {
public void run() {
getActivity().setVolumeControlStream(AudioManager.STREAM_SYSTEM);
mMediaPlayer.start();
mMediaController.show(0);
}
});
}
});
/*rootView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_MOVE){
//mMediaController.show();
//do something
}
return true;
}
});*/
/*rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Toast.makeText(rootView.getContext(),"Inside onKey",Toast.LENGTH_SHORT).show();
Log.i(getTag(), "onKey keyCode: " + keyCode);
if( keyCode == KeyEvent.KEYCODE_BACK ) {
Log.i(getTag(), "onKey Back listener is working!!!");
getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
return true;
} else {
return false;
}
}
});*/
return rootView;
}
@Override
public boolean canPause() {
return true;
}
@Override
public boolean canSeekBackward() {
return true;
}
@Override
public boolean canSeekForward() {
return true;
}
@Override
public int getBufferPercentage() {
int percentage = (mMediaPlayer.getCurrentPosition() * 100) / mMediaPlayer.getDuration();
return percentage;
}
@Override
public int getCurrentPosition() {
if (mMediaPlayer != null){return mMediaPlayer.getCurrentPosition();}
return 0;
}
@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();
}
@Override
public int getAudioSessionId() {
// TODO Auto-generated method stub
return 0;
}
}
答案 0 :(得分:1)
我找到了解决方案。我可以在片段的onDetach中隐藏MediaController。
@Override
public void onDetach() {
// TODO Auto-generated method stub
mMediaController.hide();
super.onDetach();
}
但仍有一个问题。虽然我可以拉出我的滑动抽屉菜单,但我无法点击它。 MediaController似乎是最重要的。我该如何解决这个问题呢?