Adding looping within VideoView


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();
}
}); }
}





as you mention autoplay a video and keep looping until touched which means you want to pause videos or what???
– Gowthaman M
2 days ago





Sorry, no the app exits when touched - It's basically a screensaver.
– Danny
2 days ago





remove this line finish(); so that your app not exits.it will keep play...
– Gowthaman M
2 days ago



finish();





I want to keep the finish() that is what makes the app exit when touched - all I want to do is add the looping so the video keeps playing until I touch it to exit.
– Danny
2 days ago




2 Answers
2



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


Videoview


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


finish();



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
}
});





The code above (the looping part) flags up lots of errors in the code
– Danny
2 days ago





@Danny ok fine.then keep finish(); method...please check my update answer..still you getting any issues let me know...with error log
– Gowthaman M
2 days ago






Thanks for the help Gowthaman just updated the code with what you've edited above but getting the error : error: non-static method start() cannot be referenced from a static context
– Danny
2 days ago






I've created a pastebin to show my code so far in case it's my placement etc link
– Danny
2 days ago






okay so I found the answer - I declared VideoView as Final and then your code above worked! Thanks for the help!
– Danny
2 days ago



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


finish();


videoView.stopPlayback()





I call finish() so that the app exits when I touch it
– Danny
2 days ago


finish()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift