In my Android app, I am trying to simply go back to my main Activity once a video that I am playing ends. I have tried many workarounds, but I can't find a way to call StartActivity from the video onCompletionListener - I am getting the "cannot make a static reference to the non-static method startActivity(Intent) from the type Activity" error.
I tried getting a context from the Activity that preceded the videoView, and passing that to the intent/startActivity. That allowed the app to compile, but then I got a runtime exception.
Here is the code as it stands now, which gets the "cannot make a static reference" error - any help would be appreciated!
public class Videoscreen extends Activity{
public static VideoView myVideoView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoplay);
myVideoView = (VideoView) findViewById(R.id.main_videoview);
System.out.println("playing video oncreate");
playVideo();
}
public static void playVideo(){
// video finish listener
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer vmp) {
Intent intent = new Intent();
intent.setClass(Videoscreen.this, Game.class);
Videoscreen.startActivity(intent);
}
});
String low_word = SpellingView.get_low_word();
Uri bubblesUri = Uri.parse("android.resource://org.lalloinc.ilovetrucks/raw/"+ low_word + "_vid");
myVideoView.setVideoURI(bubblesUri);
myVideoView.start();
}
}
If you know the time of the video then you try:
String uri1 = "android.resource://" + getPackageName() + "/" + R.raw.race3;
vd.setVideoURI(Uri.parse(uri1));
vd.start();
new Thread() {
public void run() {
try{
sleep(50000);
} catch (Exception e) {
}
Intent intent = new Intent(Video.this, Another.class);
startActivity(intent);
finish();
}
}.start();
if you don't know the time then you get the time as:
int vtime = vd.getDuration();
And then at thread sleep you just put this integer.
If you started the Video Activity from the Activity you would like to go back to later, just calling finish() at the end of the video will do the job.
Starting the main Activity again creates a not necessarily wanted stack of activities.
Related
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
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.
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;
}
}
Using the following code I can play a video on android in a new Activity. I would however like to preload the video (during a loading screen) and then show it when it is fully loaded. Is it possible to perhaps somehow trigger the activity to show later? In theory It would also be ok to not use a different activity.
public class VideoPlayer extends Activity implements OnCompletionListener,OnPreparedListener
{
private VideoView mVV;
#Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.videoplayer);
String url = getIntent().getStringExtra("url");
if(url == null)
finish();
mVV = (VideoView)findViewById(R.id.myvideoview);
mVV.setOnCompletionListener(this);
mVV.setOnPreparedListener(this);
mVV.setVideoURI(Uri.parse(url));
mVV.start();
}
public void stopPlaying() {
mVV.stopPlayback();
this.finish();
}
#Override
public void onCompletion(MediaPlayer mp) {
finish();
}
#Override
public void onPrepared(MediaPlayer mp) {
}
}
from my main activity:
private void playVideo(String url) {
Intent videoPlaybackActivity = new Intent(this, VideoPlayer.class);
videoPlaybackActivity.putExtra("url", url);
startActivity(videoPlaybackActivity);
}
I presume I can use the onPrepared function, but I'm not sure how to do the activity triggering to show the activity later.
http://developer.android.com/reference/android/widget/VideoView.htmlHere are the methods for the VideoView. So there is indeed a:
setOnPreparedListener(MediaPlayer.OnPreparedListener l)
Probably the best way is to use fragments in the activity and show the fragment in the activity where the mVV = (VideoView)findViewById(R.id.myvideoview); is declared when the onprepared is called.
Or hide and show the view in the activity. (using fragments is probably nicer).
Think that will work and that that is what you wanted?
I'm using a small 2 second sound effect attached to a button. When clicked, the sound effect plays and the user is taking to the next activity. The function I created is outside onCreate and uses Intent to send the user to the next activity. When I add the mp variables I get an error saying there's a problem with playGame(). What is causing the MediaPlayer to not play when it's placed in this function? Eclipse suggests changing mp.create() to MediaPlayer.create but that doesn't fix the issue.
public class SplashScreenActivity extends ActionBarActivity {
public MediaPlayer mp;
public void playGame(View view) {
mp.create(this, R.raw.bulletricochet);
mp.start();
// Do something in response to button
Intent intent = new Intent(this, QuizActivity.class);
startActivity(intent);
}
public void playRules(View view) {
Intent intentR = new Intent(this, RulesActivity.class);
startActivity(intentR);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
}}
Your mp object is null and yet you are calling create method on it.
To create a MediaPlayer object use MediaPlayer.create. You say you tried it but probably didn't assign the return value to your mp variable.
mp = MediaPlayer.create(this, R.raw.bulletricochet);
I have an application that makes a list of videos with play button. When I click on the play button, a separate activity is started using intent. I just want that when the video playback is finished, the activity should automatically finish itself and go back to the main Activity. Here is my code for creating videoview.
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
Intent i = getIntent();
Bundle extras = i.getExtras();
filename = extras.getString("videofilename");
mVideoView = (VideoView)findViewById(R.id.videoview);
path=filename;
if (path == "") {
Toast.makeText(
ViewVideo.this,
"no video selected,
Toast.LENGTH_LONG).show();
} else {
mVideoView.setVideoPath(path);
mc = new MediaController(this);
mVideoView.setMediaController(mc);
mVideoView.requestFocus();
mVideoView.start();
}
}
any suggestions???
Register an OnCompletionListener to the videoView, in the listener implement the call to finish().
Edit (to answer to comment):
use the method setOnCompletionListener:
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion (MediaPlayer mp) {
// your code to clean up and finish the activity...
}
});
You can set a MediaPlayer.OnCompletionListener on the VideoView using VideoView.setOnCompletionListener, you'll then be able to finish the containing activity when the video finishes playing.
I'm trying to make my app to play intro clip for only when I start activities.
But from my code it's always play the clip after wakeup before resume to app although I did not closed the app. What can I do to fix this prob?
From main:
startActivity(new Intent(this, MyIntro.class));
From MyIntro:
public class MyIntro extends Activity implements OnCompletionListener {
int a;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.intro);
playIntro();
}
public void onConfigurationChanged(Configuration newConfig) {
setContentView(R.layout.intro);
}
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
this.finish();
}
private void playIntro(){
setContentView(R.layout.intro);
VideoView video = (VideoView) this.findViewById(R.id.VideoView01);
Uri uri = Uri.parse("android.resource://real.app/" + R.raw.intro);
video.setVideoURI(uri);
video.requestFocus();
video.setOnCompletionListener(this);
video.start();
}
}
What function have you overridden in your main Activity - the one where you call
startActivity(new Intent(this, MyIntro.class))
?
I would assume it's onResume() and the line above is executed too many times, because of that. Read again the explanation of Activity lifecycle here, it's the first thing I do, when I have problems like that.
Get back to us with a little more info about the main Activity.
Evil hack:
Add a static pointer to your own activity, fill or override it when "onCreate" gets called. If it's null, play your movie, otherwise, don't.
You could do the same with a static boolean really.
private static boolean isRunning = false;
protected void onCreate(Bundle bundle) {
super.onCreate(bundle)
if(!isRunning)
{
isRunning = true;
//Play your video here
}
}
There are much more elegant and correct ways of doing this, but if you're in a hurry this will probably work.