Reputation: 263
So I am writing an android code where there are 3 buttons:record, pause, take frame. Record button records a video, pause button pauses the video displaying in VideoView and Take frame button shows the paused frame in imageView. Now I need 3 threads - one main thread is for user interaction (pause button), one sub thread records the video(record button) and second sub thread saves the frame(take frame button).
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mvideoview = (VideoView) findViewById(R.id.videoView);
imgview = (ImageView) findViewById(R.id.imageView);
mrecord = (Button) findViewById(R.id.button);
mpause = (Button) findViewById(R.id.button2);
mtakeframe = (Button) findViewById(R.id.button4);
mrecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dispatchTakeVideoIntent();
mvideoview.start();
}
});
mpause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mvideoview.pause();
}
});
mtakeframe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap bmp = takeFrame();
imgview.setImageBitmap(bmp);
}
});
}
private void dispatchTakeVideoIntent() {
//code directs towards the video recorder screen
}
protected void onActivityResult(int requestcode, int resultcode, Intent intent) {
//recorded video sent back and its path stored
}
public Bitmap takeFrame() {
//paused frame shown in imageView
}
I tried putting different threads at onClick(View view) of different buttons but at the end the application crashes saying the error "Only the original thread that created a view hierarchy can touch its views." And also if I want to add one more button that is PLAY(plays the paused video) then how should I write it, I can't understand. Can someone help me here in how to thread such a program in Android?
Upvotes: 2
Views: 66
Reputation: 11642
View object can modify or do any action only by UI thread If another thread tries to process on the View object, it should call like this,
runOnUiThread(new Runnable() {
@Override
public void run() {
// do the view operation here
}
});
For more information you can refer this, this
Upvotes: 2