Danny
Danny

Reputation: 43

Adding looping within VideoView

I'm having a bit of trouble with a small application I've put together in Android Studio. Essentially the application is suppose to launch - autoplay a video and keep looping until touched...

I've got everything bar the looping working - I've tried a few suggestions from here but none have worked in my case (or not had the coding skills to get them to...)

Main code below

package com.pixel.danny.screensaverhfx;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    VideoView videoView = findViewById(R.id.videoView);
    Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.hab);
    videoView.setVideoURI(uri);
    videoView.requestFocus();

    videoView.start();
    videoView.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            finish();
        }
    });    }
}

Upvotes: 1

Views: 368

Answers (2)

Gowthaman M
Gowthaman M

Reputation: 8282

what below code is doing when you are touch the Videoview you are finish the activity so app will get close....

videoView.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            finish();   //remove this line 
            videoView.stopPlayback() // you can use this for stopPlay
        }
    });   

So remove finish(); you problem got reslove

and if you want to looping

 videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
              Toast.makeText(getApplicationContext(), "Video completed", Toast.LENGTH_LONG).show();
              videoView.start();   //it will start again
        }
    });

Upvotes: 1

user1838169
user1838169

Reputation: 534

Instead of calling finish(); rather call videoView.stopPlayback()

Upvotes: 0

Related Questions