투케이2K

379. (android/java) VideoView 비디오 뷰 기본 동작 코드 지정 실시 본문

Android

379. (android/java) VideoView 비디오 뷰 기본 동작 코드 지정 실시

투케이2K 2022. 11. 2. 17:26
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[소스 코드]

    VideoView screenVideoView;





    screenVideoView = (VideoView)findViewById(R.id.screenVideoView);





    public void playMovie(String url){
        Log.d("---","---");
        Log.w("//===========//","================================================");
        Log.d("","\n"+"[A_Pip > playMovie() 메소드 : 영상 재생 실시]");
        Log.d("","\n"+"["+"주소 : "+String.valueOf(url)+"]");
        Log.w("//===========//","================================================");
        Log.d("---","---");
        try {

            //TODO 비디오 [뷰에 컨트롤 바 추가]
            //screenVideoView.setMediaController(new MediaController(this));


            //TODO [비디오 뷰에 재생할 URL 지정]
            screenVideoView.setVideoURI(Uri.parse(url));


            //TODO [비디오 재생 준비 완료 시]
            screenVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    try {

                        //TODO [비디오 재생 시작]
                        screenVideoView.start();
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });

            //TODO [비디오 재생 완료 시]
            screenVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {

                    //TODO [Alert 팝업창 알림 실시]
                    getAlertDialog("[알림]",
                            "비디오 영상 재생이 완료되었습니다.",
                            "확인", "취소", "");
                }
            });

            //TODO [비디오 재생에 문제가 발생 시]
            screenVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {

                    //TODO [Alert 팝업창 알림 실시]
                    getAlertDialog("[알림]",
                            "비디오 영상 재생에 문제가 발생하였습니다.",
                            "확인", "취소", "");
                    return false;
                }
            });
        }
        catch (Exception e){
            progressStop();
            e.printStackTrace();
        }
    }




    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d("---","---");
        Log.e("//===========//","================================================");
        Log.d("","\n"+"[A_Pip > onDestroy() 메소드 : 액티비티 종료 실시]");
        Log.e("//===========//","================================================");
        Log.d("---","---");
        try {
            //TODO [비디오 정지 실시]
            if(screenVideoView != null){
                screenVideoView.stopPlayback();
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }





    @Override
    public void onPause() {
        super.onPause();
        Log.d("---","---");
        Log.e("//===========//","================================================");
        Log.d("","\n"+"[A_Pip > onPause() 메소드 : 액티비티 실행 정지 실시]");
        Log.e("//===========//","================================================");
        Log.d("---","---");
        try {
            //TODO [비디오가 재생중인 경우는 일시 정지 실시]
            if(screenVideoView!=null && screenVideoView.isPlaying()) {
                //screenVideoView.pause(); //TODO [PIP 모드가 되어도 영상을 계속 재생하기 위해 주석]
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

 

반응형
Comments