Is there any way to get the play button to stay hidden in the background during recording until the user intervene to click the screen to allow the button to resurface the play button to stop the recording process?? Is the code coded in the .xml file or somewhere else.. How it be done??
imso 0 Junior Poster in Training
Dani AI
Generated
Short answer: put the play/stop control in an overlay and keep it hidden in XML, then toggle its visibility from your Activity when the user taps the preview. The layout (XML) only declares the views and their initial state; the show/hide behavior and timers belong in your Java code.
A simple, reliable pattern:
- Use a FrameLayout with the SurfaceView as the first child and a small controls container on top with
android:visibility="gone". - Attach a touch/gesture listener to the preview to show the container, and use a Handler to auto-hide it after a few seconds.
- Use
View.VISIBLE/View.GONE(or fade animations) to show/hide the control.
Example toggle logic (new code, not posted earlier):
final View controls = findViewById(R.id.controls);
controls.setVisibility(View.GONE);
final Handler hideHandler = new Handler();
final Runnable hideRunnable = new Runnable() {
@Override public void run() { controls.setVisibility(View.GONE); }
};
preview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_UP) {
if (controls.getVisibility() == View.VISIBLE) {
controls.setVisibility(View.GONE);
hideHandler.removeCallbacks(hideRunnable);
} else {
controls.setVisibility(View.VISIBLE);
hideHandler.postDelayed(hideRunnable, 3000); // auto-hide
}
}
return true;
}
}); Notes and cautions: use fade animations for nicer UX; protect MediaRecorder.stop() with null checks and try/catch (some devices throw if stop is called incorrectly); release camera/recorder in lifecycle callbacks; and implement runtime permission checks for CAMERA, RECORD_AUDIO and storage on modern Android. For new projects consider CameraX for easier preview/recording APIs. See Android docs on requesting permissions and CameraX for up-to-date guidance. This approach addresses 's question about XML vs code; thanks to for pointing to examples, and note the thread was closed as duplicate by .
Recommended Answers
Jump to Post— abelLazm 183Check this link here is code of same application you are developing and also some queries about it
All 3 Replies
imso 0 Junior Poster in Training
After reading the link you provided i'm still quite lost on how should i integrate the surfaceview to the buttons similar to youtube to stop and start recording.. Sorry i'm very kinna bad in programming..
public class CameraTest extends Activity implements SurfaceHolder.Callback {
private static final String TAG ="CAMERA_TUTORIAL";
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Camera camera;
private boolean previewRunning;
File tempFile = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button btnStart = (Button) findViewById(R.id.button4);
btnStart.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startRecording();
}
});
Button btnStop = (Button) findViewById(R.id.button5);
btnStop.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
stopRecording();
}
});
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
if (camera != null) {
Camera.Parameters params = camera.getParameters();
camera.setParameters(params);
}
else {
Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
finish();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (previewRunning) {
camera.stopPreview();
}
Camera.Parameters p = camera.getParameters();
p.setPreviewSize(320, 240);
p.setPreviewFormat(PixelFormat.JPEG);
camera.setParameters(p);
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
previewRunning = true;
}
catch (IOException e) {
Log.e(TAG,e.getMessage());
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
camera.stopPreview();
previewRunning = false;
camera.release();
}
private MediaRecorder mediaRecorder;
private final int maxDurationInMs = 20000;
private final int videoFramesPerSecond = 20;
public boolean startRecording(){
try {
camera.unlock();
mediaRecorder = new MediaRecorder();
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setMaxDuration(maxDurationInMs);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
//mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
tempFile = new File(Environment.getExternalStorageDirectory(),"1.3gp");
mediaRecorder.setOutputFile(tempFile.getPath());
mediaRecorder.setVideoSize(surfaceView.getWidth(),surfaceView.getHeight());
//mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.prepare();
mediaRecorder.start();
return true;
} catch (IllegalStateException e) {
Log.e(TAG,e.getMessage());
e.printStackTrace();
return false;
} catch (IOException e) {
Log.e(TAG,e.getMessage());
e.printStackTrace();
return false;
}
}
public void stopRecording(){
mediaRecorder.stop();
camera.lock();
}
} peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster
Thread locked as it is duplication of another thread. If you wish to follow discussion please check here
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.