使用全屏按钮在下一个活动上播放视频

时间:2017-09-01 13:50:44

标签: java android android-videoview

没有Main2Activity,一切正常,但我想要做的是在点击全屏按钮时在Main2Activity上播放视频。一切都在MainActivity上运行良好但是当我点击全屏按钮时它会崩溃。不知道为什么。我是Android开发的新手,任何帮助都会受到赞赏。

MainActivity:

public class MainActivity extends AppCompatActivity {


    Button buttonPlay;
    Button buttonFullScreen;
    static final int REQUEST_VIDEO_CAPTURE = 1;
    VideoView resultvideo;
    MediaController mediacontroller;

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

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.setContentView(R.layout.activity_main);

        setContentView(R.layout.activity_main);

        resultvideo = (VideoView)findViewById(R.id.videoView);
        mediacontroller = new MediaController(MainActivity.this);

        mediacontroller.setAnchorView(resultvideo);

        resultvideo.setMediaController(mediacontroller);

        Button click = (Button)findViewById(R.id.buttonRecord);
        resultvideo = (VideoView)findViewById(R.id.videoView);
    }

    public void dispatchTakeVideoIntent(View v) {
        Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
            Uri videoUri = data.getData();
            Log.i("test","111111111111111" + videoUri.toString());
            resultvideo.setVideoURI(videoUri);
            mediacontroller.setAnchorView(resultvideo);
            resultvideo.pause();

        }
        buttonPlay = (Button) findViewById(R.id.buttonPlay);
        {
            buttonPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mediacontroller.show();
                    mediacontroller.setAnchorView(resultvideo);
                    resultvideo.start();
                    Log.i("test","111111111111111");
                }
            });
        }

        buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
        {
            buttonFullScreen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(MainActivity.this, Main2Activity.class));
                }
            });
        }
    }
}

Main2Activity:

public class Main2Activity extends MainActivity {

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

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main2);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
            Uri videoUri = data.getData();
            Log.i("test", "111111111111111" + videoUri.toString());
            resultvideo.setVideoURI(videoUri);
            mediacontroller.setAnchorView(resultvideo);
            resultvideo.pause();

        }
        buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
        {
            buttonFullScreen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mediacontroller.show();
                    mediacontroller.setAnchorView(resultvideo);
                    resultvideo.start();
                    Log.i("test","111111111111111");
                }
            });
        }
    }
}

2 个答案:

答案 0 :(得分:0)

resultvideo在MainActivity中声明,在Main2Activity中不可用

答案 1 :(得分:0)

[tox] indexserver = default = http://pypi.my-alternative-index.org mediacontroller在MainActivity中声明,它们位于resultvideo?您必须在Main2Activity的布局中使用它们,因为您在Main2Activity布局中拥有它们,并且MainActivity中的ID也需要找到它们。