I want to start camera and also to automatically start recording just by clicking an app in android. I have the code to start the camera but I do not know how to start auto capture of the video. Please help.
the code I have for launching camera-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c1_main);
Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
StartActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY);
}
I found about view.performclick but do not know how to use for camera
Use MediaRecorder for this purpose. Though it will require more work but will give you much more control. Follow this link http://android-er.blogspot.tw/2011/04/start-video-recording-using.html. Since you don't reuire any button click for recording, keep a delay before starting the camera. Do it like this
myButton.setPressed(true); //you won't click the button
myButton.invalidate();
myButton.postDelayed(new Runnable() {
public void run() {
myButton.setPressed(false);
myButton.invalidate();
releaseCamera(); //release camera from preview before MediaRecorder starts
if(!prepareMediaRecorder()){
Toast.makeText(AndroidVideoCapture.this,"could not prepare MediaRecorder",Toast.LENGTH_LONG).show();
finish();
}
mediaRecorder.start();
}
},5000); //causes delay of 5 seconds befor recording starts
Ok, Make following, changes in your code.
Button play;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c1_main);
play = findViewById ( R.id.btnPlay ); // assuming you have this button in your .xml file.
play.setOnClickListener ( new OnClickListener()
{
#Override
public void onClick ( View view )
{
Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
StartActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY);
}
});
}
Related
In my project I am trying to make a media player which plays a shoutcast stream. Everything seemed to be working well until I pressed the back button on my device, which I think stops the activity and causes the device to recreate the activity when launched again. The problem is , when the activity is recreated , I lose the control of the mediaplayer and a new mediaplayer is created.
I need to be able to have the mediaplayer's control back at that point. How is it possible?
This part of code belongs to onCreate
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
if (mediaPlayer == null){
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getString(R.string.yayin));
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
}
if(!isPlaying){
btn.setBackgroundResource(R.drawable.oynat);
}
else{
btn.setBackgroundResource(R.drawable.durdur);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isPlaying){
playOnReady();
isPlaying = true;
btn.setBackgroundResource(R.drawable.durdur);
}
else{
mediaPlayer.reset();
isPlaying = false;
btn.setBackgroundResource(R.drawable.oynat);
}
}
});
This part of code belongs to the function playOnReady()
private void playOnReady(){
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
Take a look at the Android Activity lifecycle flowchart: https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You need to account for the path where onPause is called when you leave the Activity and then onResume is called when you enter it again. The solution for you could be as simple as moving some/all of your code from onCreate into onResume.
Using this tutorial I have managed to build a service which handles the mediaplayer that I can have the control of anytime I need.
i am new in this space i am working with mini app.
i have Animal images in Main template and want when click for example Dog image it must switch another page and there will be dog description and image gallery.
so i need to know how to create multiple pages and redirect it.
this is my app.
Thanks ...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//////////////////////// Dog
Button button3 = (Button)this.findViewById(R.id.button3);
final MediaPlayer dog = MediaPlayer.create(this, R.raw.dog);
button3.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// If the music is playing
if(dog.isPlaying() == true)
// Pause the music player
dog.pause();
// If it's not playing
else
// Resume the music player
dog.start();
}
});
You must create differents activity for different pages.
And use Intent to change page.
Example :
Intent intent = new Intent(MainActivy.this, OtherActivity.class);
startActivity(intent);
In my application i have an image button that basically switches on and off the flash LED. The code is running fine for the first time i.e. On first Click it Switches On the LED and on the Second Click it Switches it Off. But then Nothing happens third Click onwards. I am testing this on Nexus S.
Following is the Code for the ImageButton Click Method.
public void ToggleTorch(){
final ImageButton tt = (ImageButton)findViewById(R.id.tt);
tt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
if (isFlashOn){
mycam.stopPreview();
isFlashOn = false;
} else {
mycam.startPreview();
isFlashOn = true;
}
}
});
}
From what i think, it has to do something with the SurfaceView as i think it is not being destroyed while calling stopPreview but i am not sure..
Following is the Code for the onCreate Method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Check if Flash Light is Available
Boolean has_flash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(has_flash){
setContentView(R.layout.activity_main);
SurfaceView preview = (SurfaceView)findViewById(R.id.pSv);
SurfaceHolder holder = preview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
disableSleepMode();
initFlashLight();
ToggleTorch();
screenTorchOn();
} else {
setContentView(R.layout.activity_main);
disableSleepMode();
screenTorchOn();
}
}
any help would be appreciated. Thanks.
instead of calling stop preview release camera and make camera instance null. To restart camera, initialize camera again. Make two different method one for initialize camera and another to release it. This will solve your problem.
Below is my code and I'm gonna integrate with the application. My problem is when the button clicked video recorder invoked and video recoded smoothly but I want to get the response from the camera when the video recording is done every time.
public class AndroidVideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnVideoRecorder = (Button)findViewById(R.id.buttonClick);
btnVideoRecorder.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.VIDEO_CAMERA");
startActivity(intent);
}
});
}
}
Can someone please guide me.
Thanks for your time in advance.
Try changing
startActivity(intent);
to
startActivityForResult(intent);
Then when the video is done recordng, it will return to your app and you will be able to do something with the video
I made a basic app with 2 buttons, start and stop. When I start the app and hit the start button the sound starts, and when I the end button it stops, BUT if I try to start up again with the start button it doesn't start again.
Code:
buttonStart = (Button)findViewById(R.id.ButtonStart);
buttonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClickButton(buttonStart);
}
});
buttonEnd = (Button)findViewById(R.id.ButtonEnd);
buttonEnd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClickButton(buttonEnd);
}
});
beat = MediaPlayer.create(this, R.raw.beat);
public void onClickButton(Button button){
if(button == buttonStart){
beat.start();
beat.setLooping(true);
}
else if(button == buttonEnd){
beat.stop();
//beat.setLooping(false);
}
}
if you stop the media player then the instance of the media player is destroyed so if you want to play again. Then you have to create the instance of media player again. Put this code in your buttonStart
beat = MediaPlayer.create(this, R.raw.beat);
beat.start();
beat.setLooping(true);
Best of luck and don't forget to tick that this answer is useful to you.