Reputation: 63
i'm learning about android multithreading on my own and am using this powerpoint as a main resource: http://grail.cba.csuohio.edu/~matos/notes/cis-493/lecture-notes/Android-Chapter13-MultiThreading.pdf
i am attempting the Handler post approach. on slide 38, it mentions that there needs to be a foreground runnable and a background runnable.
if i want my foreground runnable to be a videoview, would i need to put all my videoview code in a try/catch? or is creating the videoview itself creating a foreground runnable on its own? is there another way to go about this?
i appreciate any help at all as i'm pretty new to java and android.
Upvotes: 0
Views: 1482
Reputation: 14367
Firstly look up foreground vs background android docs and how activities work in general. You wont need any sort of handler for video views to work. Video views are views.
Then look at what handlers are and how they work here - http://developer.android.com/reference/android/os/Handler.html
For your task - Write a separate handler in your activity that is responsible for your videoview like so - basically listen for time as the video plays and then pop up once the played time matches the time where you want to pop up the toast.
Handler handler = new Handler();
...
handler.post(timefortoast);
....
int curVideoPosition;
private final Runnable timefortoast= new Runnable() {
public void run() {
try {
if (video.isPlaying()) {
cumulativeTimeSpent += 1;
}
// prepare and send the data here..
curVideoPosition = video.getCurrentPosition();
//Match cumulative time spent and curvideoposition to pop up whatever you want, toast or whatever in the middle of the video
and then detach the callback in ondestroy
handler.removeCallbacks(timefortoast);
Upvotes: 1
Reputation: 23279
You do not need to use Handlers
/Threads
to implement a VideoView
at all - this should run on the UI thread just like normal views.
From your comments though I understand this is a learning exercise, so you can implement a Runnable
task that performs what you need it to do. It's not really related to your VideoView
, just something you want to execute seperately.
For example:
private Runnable task = new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "HELLO!", Toast.LENGTH_LONG).show();
videoView.stopPlayback();
}
};
Then you can call this by:
Handler handler = new Handler()
handler.postDelayed(task, 5000);
The code within task
will execute 5 seconds after calling handler.postDelayed()
I would suggest you get a VideoView
working normally, then add your handler.
Upvotes: 1