我已经看到了几个相同问题的帖子,但我仍然无法解决我的问题。我正在使用surfaceview、fragments 和mediaplayer 来播放mp4 视频。我有音频,但没有视频。该视频在应用程序之外播放时没有任何问题。我尝试了不同的格式,但没有运气。我没有使用模拟器。我正在使用三星 Galaxy Tab2 进行测试。我缺少什么来显示视频?
这是我的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/video_surfaceView"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</SurfaceView>
<Button
android:id="@+id/playvideoplayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PLAY "/>
<Button
android:id="@+id/pausevideoplayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PAUSE"/>
<Button
android:id="@+id/stopvideoplayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- STOP"/>
</LinearLayout>
这是我的代码:
public class ChapterVideoFragment extends Fragment {
private static final String TAG = "ChapterVideoFragment";
private static final boolean VERBOSE = true;
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private MediaPlayer mp = null;
private Button mPlayVideo,
mPauseVideo,
mStopVideo;
private boolean mPausing = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
if (VERBOSE) Log.v(TAG, "+++ onCreateView +++");
View v = inflater.inflate(R.layout.fragment_video, parent, false);
mSurfaceView = (SurfaceView)v.findViewById(R.id.video_surfaceView);
mPlayVideo = (Button)v.findViewById(R.id.playvideoplayer);
mPauseVideo = (Button)v.findViewById(R.id.pausevideoplayer);
mStopVideo = (Button)v.findViewById(R.id.stopvideoplayer);
mp = MediaPlayer.create(getActivity(), R.raw.improvisation);
mPlayVideo.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
//Get the dimensions of the video
int videoWidth = mp.getVideoWidth();
int videoHeight = mp.getVideoHeight();
//Get the width of the screen
int screenWidth =
getActivity().getWindowManager().getDefaultDisplay().getWidth();
//Get the SurfaceView layout parameters
android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();
//Set the width of the SurfaceView to the width of the screen
lp.width = screenWidth;
//Set the height of the SurfaceView to match the aspect ratio of the video
//be sure to cast these as floats otherwise the calculation will likely be
0
lp.height = (int) (((float)videoHeight / (float)videoWidth) *
(float)screenWidth);
//Commit the layout parameters
mSurfaceView.setLayoutParams(lp);
//mp.setDisplay(mSurfaceView.getHolder());
mp.start();
}
});
这是 SurfaceView 的代码:
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
if (VERBOSE) Log.v(TAG, "+++ surfaceCreated +++");
// TODO Auto-generated method stub
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
});