Search code examples
androidtimermedia-player

How can I stop a video after 30 seconds


I want to have a video in my app that has a free 30 seconds preview but instead of having two separate videos one full and one a 30 sec section how can I simply stop the video after 30 seconds of playing

Here is the block of code I use to play audios or videos

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /** REMOVE TITLE **/
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.moveplay);
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);

    final VideoView audioView = (VideoView) findViewById(R.id.VideoView);
    final MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(audioView);

    Bundle extras = getIntent().getExtras();
    String pathfile = extras.getString("key");
    Log.v("key = ",pathfile);

    Uri audio = Uri.parse(pathfile);
    audioView.setMediaController(mediaController);
    audioView.setVideoURI(audio);
    audioView.start();

I have another section of my app that determines whether to play from the SD card or stream it and that all works fine.

I would simply like to set a flag so either the first 30 secs of the vid plays or the entire vid plays.

I know the controller appears when I tap the screen and I see the counter i just need to stop it when the current position reaches 30 that way I save on resources with just a single vid for both

Thanks Jeff


Solution

  • Step #1: Use postDelayed() or something to get control every second or so.

    Step #2: Check getCurrentPosition() and see if it is near your 30s marker point. If so then stop the video view.
    The code for determining the progressof the video played is given in this link.