【问题标题】:Using surfaceview and fragments to display video. Video doesn't show but I have sound,使用surfaceview和fragments来显示视频。视频不显示,但我有声音,
【发布时间】:2013-06-22 04:37:20
【问题描述】:

我已经看到了几个相同问题的帖子,但我仍然无法解决我的问题。我正在使用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

        }
    });       

【问题讨论】:

  • 尝试使用 videoview - 您用来显示视频的设备可能不支持 mediaplayer 对象中的格式,我用 videoview 解决了这个问题
  • 谢谢,我会试试的。
  • @LenaBru VideoView 工作!
  • 看起来您注释掉了将表面连接到媒体播放器的行://mp.setDisplay(mSurfaceView.getHolder());。而且,我认为应该在 surfaceChanged() 回调中调用附加显示。
  • @robd 感谢您的反馈。我使用了 VideoView,效果很好。

标签: android android-fragments surfaceview mediacontroller


【解决方案1】:

尝试使用 videoview - 您用来显示视频的设备可能不支持 mediaplayer 对象中的格式,我用 videoview 解决了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多