Java Code Examples for android.widget.VideoView#setOnPreparedListener()

The following examples show how to use android.widget.VideoView#setOnPreparedListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VideoActivity.java    From Small with Apache License 2.0 7 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    VideoView videoView = (VideoView) findViewById(R.id.video_view);
    final String uri = "android.resource://" + getPackageName() + "/" + R.raw.fix_429;
    videoView.setVideoURI(Uri.parse(uri));
    // Loop
    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setLooping(true);
        }
    });
    // Play
    videoView.start();
}
 
Example 2
Source File: VideoPlayerActivity.java    From ScrollGalleryView with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String url = getIntent().getExtras().getString(Constants.URL);
    setContentView(R.layout.video_fragment);
    final VideoView videoView = (VideoView) findViewById(R.id.videoView);
    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            View progress = findViewById(R.id.videoProgress);
            progress.setVisibility(View.GONE);

            videoView.requestFocus();
            MediaController vidControl = new MediaController(VideoPlayerActivity.this);
            vidControl.setAnchorView(videoView);
            videoView.setMediaController(vidControl);
            videoView.start();
        }
    });
    videoView.setVideoURI(Uri.parse(url));
}
 
Example 3
Source File: MainActivity.java    From example with Apache License 2.0 6 votes vote down vote up
@Override protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  videoView = (VideoView) findViewById(R.id.videoview);

  DisplayMetrics metrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(metrics);
  RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
  params.width = metrics.widthPixels;
  params.height = metrics.heightPixels;
  videoView.setLayoutParams(params);

  videoView.setVideoURI(
      Uri.parse("android.resource://" + getPackageName() + "/raw/" + R.raw.momentss));
  videoView.start();
  // looping
  videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override public void onPrepared(MediaPlayer mediaPlayer) {
      mediaPlayer.setLooping(true);
    }
  });
}
 
Example 4
Source File: Activity_VideoSurveillance.java    From FoodOrdering with Apache License 2.0 5 votes vote down vote up
private void initView() {
    pg=new ProgressDialog(Activity_VideoSurveillance.this);
    pg.setMessage("缓冲中...");
    pg.show();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("");
    TextView toolbarText = (TextView) findViewById(R.id.toolbar_text);
    toolbarText.setText("视频监控");
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    if (!Util.checkNetwork(this)) {
        return;
    }
    videoViewVideo = (VideoView) findViewById(R.id.videoViewVideo);

    try {
        //设置视频控制器
        MediaController mediacontroller = new MediaController(Activity_VideoSurveillance.this);
        mediacontroller.setAnchorView(videoViewVideo);
        videoViewVideo.setMediaController(mediacontroller);
        //视频来源
        videoViewVideo.setVideoURI(Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"));
    } catch (Exception e) {
        e.printStackTrace();
        pg.dismiss();
    }
    videoViewVideo.requestFocus();
    videoViewVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        // Close the progress bar and play the video
        public void onPrepared(MediaPlayer mp) {
            pg.dismiss();
            //开始播放视频
            videoViewVideo.start();
        }
    });
}
 
Example 5
Source File: PreviewVideoActivity.java    From Cirrus_depricated with GNU General Public License v2.0 5 votes vote down vote up
/**
 *  Called when the activity is first created.
 *
 *  Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
 *
 *  The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to
 *  try to stream the remote file - TODO get the streaming works
 *
 *  {@inheritDoc}
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log_OC.v(TAG, "onCreate");

    setContentView(R.layout.video_layout);

    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        mSavedPlaybackPosition = extras.getInt(EXTRA_START_POSITION);
        mAutoplay = extras.getBoolean(EXTRA_AUTOPLAY);

    } else {
        mSavedPlaybackPosition = savedInstanceState.getInt(EXTRA_START_POSITION);
        mAutoplay = savedInstanceState.getBoolean(EXTRA_AUTOPLAY);
    }

    mVideoPlayer = (VideoView) findViewById(R.id.videoPlayer);

    // set listeners to get more contol on the playback
    mVideoPlayer.setOnPreparedListener(this);
    mVideoPlayer.setOnCompletionListener(this);
    mVideoPlayer.setOnErrorListener(this);

    // keep the screen on while the playback is performed (prevents screen off by battery save)
    mVideoPlayer.setKeepScreenOn(true);
}
 
Example 6
Source File: AudioVideoVideoPlayActivity.java    From coursera-android with MIT License 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);

	// Get a reference to the VideoView
	
	mVideoView = (VideoView) findViewById(R.id.videoViewer);

	// Add a Media controller to allow forward/reverse/pause/resume 
	
	final MediaController mMediaController = new MediaController(
			AudioVideoVideoPlayActivity.this, true);
	
	mMediaController.setEnabled(false);

	mVideoView.setMediaController(mMediaController);
	
	mVideoView
			.setVideoURI(Uri
					.parse("android.resource://course.examples.AudioVideo.VideoPlay/raw/moon"));
	
	// Add an OnPreparedListener to enable the MediaController once the video is ready
	mVideoView.setOnPreparedListener(new OnPreparedListener() {

		@Override
		public void onPrepared(MediaPlayer mp) {
			mMediaController.setEnabled(true);
		}
	});
}
 
Example 7
Source File: VideoPlayerActivity.java    From AndroidDemoProjects with Apache License 2.0 5 votes vote down vote up
protected void initViews() {
	mSpinningProgressBar = (ProgressBar) findViewById( R.id.progress_spinner );

	mVideoView = (VideoView) findViewById( R.id.video_view );
	mVideoView.setOnCompletionListener( onCompletionListener );
	mVideoView.setOnErrorListener( onErrorListener );
	mVideoView.setOnPreparedListener( onPreparedListener );

	if( mVideoView == null ) {
		throw new IllegalArgumentException( "Layout must contain a video view with ID video_view" );
	}

	mUri = Uri.parse( getIntent().getExtras().getString( EXTRA_VIDEO_URL ) );
	mVideoView.setVideoURI( mUri );
}
 
Example 8
Source File: SingleVideoActivity.java    From BaldPhone with Apache License 2.0 4 votes vote down vote up
@Override
protected void bindView(View v, Cursor cursor, Context context) {
    final VideoView videoView = v.findViewById(R.id.vid);
    final ImageView play_stop = v.findViewById(R.id.play_stop);

    videoView.setOnPreparedListener(mp -> {
        float videoProportion = (float) mp.getVideoWidth() / (float) mp.getVideoHeight();
        ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) videoView.getLayoutParams();
        lp.dimensionRatio = String.valueOf(videoProportion);
        videoView.setLayoutParams(lp);

    });

    final Uri uri = Uri.parse(cursor.getString(
            cursor.getColumnIndex(MediaStore.Images.Media.DATA)
    ));
    videoView.setVideoURI(uri);
    videoView.requestFocus();
    videoView.seekTo(1);
    videoView.setOnCompletionListener(mp -> {
        play_stop.setImageResource(R.drawable.replay_on_background);
        Toggeler.newImageToggeler(
                play_stop,
                play_stop,
                new int[]{R.drawable.stop_on_background, R.drawable.play_on_background},
                new View.OnClickListener[]{
                        view -> videoView.start(),
                        view -> videoView.pause()
                });
    });

    Toggeler.newImageToggeler(
            play_stop,
            play_stop,
            new int[]{R.drawable.stop_on_background, R.drawable.play_on_background},
            new View.OnClickListener[]{
                    view -> videoView.start(),
                    view -> videoView.pause()
            });

    ((VideoViewWrapper) v).setOnShowedChangedListener(shown -> {
        if (shown) {
            videoView.seekTo(1);
            Toggeler.newImageToggeler(
                    play_stop,
                    play_stop,
                    new int[]{R.drawable.stop_on_background, R.drawable.play_on_background},
                    new View.OnClickListener[]{
                            view -> videoView.start(),
                            view -> videoView.pause()
                    });
            play_stop.setImageResource(R.drawable.play_on_background);
            videoView.start();
            videoView.pause();
        } else
            videoView.pause();
    });

}
 
Example 9
Source File: VideoEnabledWebChromeClient.java    From carstream-android-auto with Apache License 2.0 4 votes vote down vote up
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
    Log.d(getClass().getName(), "onShowCustomView() called with: view = [" + view + "], callback = [" + callback + "]");
    if (view instanceof FrameLayout) {

        // A video wants to be shown
        FrameLayout frameLayout = (FrameLayout) view;
        View focusedChild = frameLayout.getFocusedChild();
        if (focusedChild != null) {
            focusedChild.requestFocus();
            focusedChild.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                            webView.goBack();
                            return true;
                        }
                    }
                    return false;
                }
            });
        }

        // Save video related variables
        this.isVideoFullscreen = true;
        this.videoViewContainer = frameLayout;
        this.videoViewCallback = callback;

        // Hide the non-video view, add the video view, and show it
        activityNonVideoView.setVisibility(View.INVISIBLE);
        activityVideoView.addView(videoViewContainer, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        activityVideoView.setVisibility(View.VISIBLE);
        activityVideoView.setBackgroundColor(Color.BLACK);
        if (focusedChild != null) {
            focusedChild.setBackgroundColor(Color.BLACK);
            if (videoTouchListener != null) {
                focusedChild.setOnTouchListener(videoTouchListener);
            }
        }


        if (focusedChild instanceof VideoView) {
            // VideoView (typically API level <11)
            VideoView videoView = (VideoView) focusedChild;
            // Handle all the required events
            videoView.setOnPreparedListener(this);
            videoView.setOnCompletionListener(this);
            videoView.setOnErrorListener(this);
        } else // Usually android.webkit.HTML5VideoFullScreen$VideoSurfaceView, sometimes android.webkit.HTML5VideoFullScreen$VideoTextureView
        {
            // HTML5VideoFullScreen (typically API level 11+)
            // Handle HTML5 video ended event
            if (webView != null && webView.getSettings().getJavaScriptEnabled()) {
                // Run javascript code that detects the video end and notifies the interface
                handleVideoStretching();
            }
        }

        // Notify full-screen change
        if (toggledFullscreenCallback != null) {
            toggledFullscreenCallback.toggledFullscreen(true);
        }
    }
}
 
Example 10
Source File: HomeSplashFragment.java    From Android with MIT License 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.home_splash_screen, container, false);


    mContext = this.getActivity();
    Button login_fragment =  view.findViewById(R.id.login_fragment);
    Button signup_fragment =  view.findViewById(R.id.signup_fragment);
    VideoView videoview =  view.findViewById(R.id.videoview);
    mPager =  view.findViewById(R.id.pager);
    indicator =  view.findViewById(R.id.indicator);
    ImageView background_image_home_fragment = (ImageView) view.findViewById(R.id.background_image_home_fragment);

    login_fragment.setOnClickListener(this);
    signup_fragment.setOnClickListener(this);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
        videoview.setVisibility(View.GONE);
        background_image_home_fragment.setVisibility(View.VISIBLE);
    }else {
        Uri uri = Uri.parse("android.resource://" + mContext.getPackageName() + "/" + R.raw.story);
        videoview.setVideoURI(uri);
        videoview.start();
        final int duration = 4000;
        final int colorFrom = Color.parseColor("#10000000");
        final int colorTo = Color.parseColor("#b8000000");
        ColorDrawable[] color = {new ColorDrawable(colorFrom), new ColorDrawable(colorTo)};
        TransitionDrawable transition = new TransitionDrawable(color);
        videoview.setBackground(transition);
        transition.startTransition(duration);
        //Video Loop
        videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
                mp.setLooping(true);
                mp.setVolume(0f, 0f);
            }
        });
    }
    init();
    return view;
}
 
Example 11
Source File: PlayerActivity.java    From android with MIT License 4 votes vote down vote up
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        url = getIntent().getStringExtra(KEY_URL);
        url = "http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4";
        if (url == null && savedInstanceState != null) {
            url = savedInstanceState.getString(KEY_URL);
        }

        if (url == null) {
            Toast.makeText(getApplicationContext(), TOAST_ERROR_URL, Toast.LENGTH_LONG).show();
            finish();
            return;
        }

        setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);

        videoView = new VideoView(this);
        videoView.setVideoURI(Uri.parse(url));
        videoView.requestFocus();
        videoView.setOnPreparedListener(this);
        videoView.setOnCompletionListener(this);
        videoView.setOnErrorListener(this);

        mc = new MediaController(this);
        mc.setAnchorView(videoView);
        mc.setKeepScreenOn(true);

        videoView.setMediaController(mc);

        llMain = new LinearLayout(this);
        llMain.setGravity(Gravity.CENTER_VERTICAL);
        llMain.setOrientation(LinearLayout.VERTICAL);
        llMain.setLayoutParams(params);

        llMain.addView(videoView, params);
        setContentView(llMain);

        initDialog();
    }