为什么MediaPlayer使用SurfaceView,即使我没有将其指定为显示?

时间:2015-10-28 10:00:34

标签: android android-mediaplayer surfaceview

为什么MediaPlayer在SurfaceView上输出视频,即使我没有将其指定为显示?我错过了什么吗?

public class TestActivity extends Activity {

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    setContentView(R.layout.test);

    TestVideoView2 v = new TestVideoView2(this);
    ((ViewGroup)findViewById(android.R.id.content)).addView(v);
}

private static class TestVideoView2 extends  LinearLayout {

    private SurfaceView surfaceView;
    private MediaPlayer mediaPlayer;

    public TestVideoView2(Context context) {
        super(context);

        setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        try {
            surfaceView = new SurfaceView(getContext());
            surfaceView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            addView(surfaceView);
            mediaPlayer = new MediaPlayer();
            //even though I specifically pointed to not use any display, I still get the video on the surface shown
            mediaPlayer.setDisplay(null);
            //or any other source
            mediaPlayer.setDataSource(getContext(), Uri.parse("http://pmdvod.nationalgeographic.com/NG_Video/683/787/151026-news-microplastics-trash-trawl-vin-ds1502001-222__334712.mp4"));
            mediaPlayer.prepare();
            mediaPlayer.start();
        }
        catch (Exception e) {

        }
    }
}

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
</LinearLayout>

0 个答案:

没有答案