I found and use some method bellow but it is not work for me:
myThread.stop() //it is not safe but I am tried that
myThread.interupt
Here is my program: I wanna play video using Videoview when video finish. If user no choose the next video in 120s then my app will finish. My video view code:
Uri uri = Uri.parse(filePath); videoView = findViewById(R.id.videoView); videoView.setVideoURI(uri); waitingThread w8 = new waitingThread(); //set params video before play videoView.setOnPreparedListener(mediaPlayer -> { PlaybackParams playbackParams = new PlaybackParams(); playbackParams.setSpeed(DeviceConfig.getInstance().getVideoPeed());// 1.25 1.5 2 2.5 mediaPlayer.setPlaybackParams(playbackParams); // I am tryied using stop thread here // w8.stop() // or w8.interrupt(); videoView.start(); }); videoView.setOnErrorListener((mediaPlayer, i, i1) -> { Log.d(TAG,"Video Error"); //send error message to server return false; }); //I call thread when video complete videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { **waiting();** -> w8.start //edited i start thread here } });
My thread waiting
private class waitingThread extends Thread{
public void run() {
try {
while (!isInterrupted()) {
timeCount ;
Thread.sleep(1000);
Log.d(TAG, "Time count : " timeCount);
if(timeCount == 120){
finish();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//i am try to using this funtion but still not work too.
public void stopping(){
Thread.currentThread().interrupt();
// timeCount = 0;
// Log.d(TAG, "Stopping, reset time :" timeCount);
}
}
Brief my idea: When video start play, thread waiting will be stopped. When video finish program will waiting a time if no video chose in time to wait program will finish, if any video chose then thread w8 stop.
My problem: when I choose the next video, my thread "w8" still keep running. That is make my app finished while video playing Plz help me how to fix that problem or any same idea to work are appreciated
CodePudding user response:
You don't want to call interrupt on Thread.currentThread. Thread.currentThread is the thread currently running- it's the thread you're calling the function on. It's not the thread object you just created. Instead it would be this.interrupt(). Or just get rid of the function entirely and call interrupt directly.
CodePudding user response:
Introducing your own boolean variable might help
class waitingThread extends Thread{
boolean stop;
public void run(){
while(!stop){
//your task
}
stop = false; //
}
public void stopping(){
stop= true;
}
}