Sometimes microphone doesnt get released for other apps - android

Im working on a personal assistant using pocketsphinx speech recognizer in android. This is the way my app works every time a special word is heard the personal assistant will answer back and do a task. I have been having some problems with the releasing of the microphone. I dont know if it is a bug. It only happens sometimes when I close the application the microphone is still looking for that word and answering using text to speech. Even though screen is off. When I tried to record a video it says microphone is used by another application. So I have to open my app again and close the app to release the microphone. In my knowledge the only lifecycles to release resources are onStop, onPause, and onDestroy. It cannot be my phone that is malfunctioning I have tested the app with two different phones and still sometimes it happens in bot of them. Any help would be appreciated.
This is the way Im releasing the microphone, camera and the text to speech. Thanks in advance
private edu.cmu.pocketsphinx.SpeechRecognizer recognizer;
#Override
public void onPause() {
super.onPause();
if (tts != null) {
tts.shutdown();
}
if (camera != null) {
camera.release();
camera = null;
}
if (recognizer != null) {
recognizer.stop();
recognizer.cancel();
recognizer.shutdown();
recognizer = null;
}
}
#Override
protected void onStop() {
super.onStop();
if (tts != null) {
tts.shutdown();
}
if (camera != null) {
camera.release();
camera = null;
}
if (recognizer != null) {
recognizer.cancel();
recognizer.shutdown();
recognizer = null;
}
}
#Override
public void onDestroy() {
super.onDestroy();
if (tts != null) {
tts.shutdown();
}
if (camera != null) {
camera.release();
camera = null;
}
if (recognizer != null) {
recognizer.cancel();
recognizer.shutdown();
}
}

Try overriding the onbackpressed method and post ur recorder stop code there. And then finally add in the end youractivity.finish;
This shall destroy ur activity as soon as back is pressed and should stop the recorder

Related

Android Live Wallpaper crashes when tap on camera

I have a live wallpaper that just draws triangles, very simple, and runs smoothly all time. After testing found that when I open the camera to take pictures the phone freezes for about 10 seconds, then the camera opens and the wallpaper crashes and the message "Unfortunately, Live Wallpaper has stopped." appears. Also found that when I open any barcode scanner app the problem raises again because the scanner uses the camera too. Seems the problem raises when the camera app runs. Any ideas what's causing it?
hi #Bullet Camera is just open ones, if it open through any other apps then, you can can not access your camera, Solution is that, where you use camera please release that after no used.
main problem is that, in your app (app in that you are using camera) , so , you release camera after used.
like following :
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
when you need
private void releaseCameraAndPreview() {
if (camera != null) {
camera.release();
camera = null;
}
}
or
public void stopCamera() {
if (cameraDevice != null) {
cameraDevice.stopPreview();
cameraDevice.setPreviewCallback(null);
cameraDevice.release();
cameraDevice = null;
System.out.println("in to the stop video");
}
}

Camera release Invalid Op on Moverio

I'm getting an Error on the Moverio Glasses bt200 while releasing Camera. On Nexus 10 everything works fine. The part, where I'm getting the Error looks like this:
protected void onPause() {
super.onPause();
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview);
releaseCamera();
}
private void releaseCamera() {
if(mCamera != null) {
mCamera.release();
mCamera = null;
}
}
The Error-Message is:
hardware/ti/omap4xxx/camera/BaseCameraAdapter.cpp:1794 setState - Adapter state switch INTIALIZED_STATE Invalid Op! event = CAMERA_STOP_IMAGE_CAPTURE
Does anybody have any Idea, why I'm getting this Error?
Thanks in advance.
change onPause() to...
protected void onPause() {
super.onPause();
releaseCamera();
}

where should I release the camera in my android activity

I am making an android flashlight app. I it has two activities, a main activity and a settings activity where the camera led can be toggled on and off. It also has another class where all the camera changes are handled like opening it, releasing it, and turning the light on and off.
I kept getting errors when turning on the led based on shared preference settings because I was not opening and releasing the camera in the correct activity lifecycle stages. I fixed the issues by releasing the camera when onPause is called in either activity and turning the led on or off (based on shared preference settings) when onResume is called in either activity.
The problem I am having now is that if the led is on, it turns off briefly when switching from one activity to the other because I have to release the camera and then open again in the new activity. Can anyone help me figure out a way to eliminate this problem? Where should I open and release the camera? I have tried releasing it in on destroy but the led stays on when the app is minimized to the background, which is undesirable. Thanks for any suggestions.
In both main and settings activity I have:
#Override
protected void onResume() {
super.onResume();
if (sp.getBoolean("LED_TOGGLE_CB", false) == true) {
flash.turnFlashOn();
}
}
#Override
protected void onPause() {
super.onPause();
flash.killCamera();
}
In the Flash class I have:
void getCamera() {
if(camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch(RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
void turnFlashOn() {
getCamera();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
}
public void killCamera() {
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}

Google GDK: Differences in calling app with voice trigger or menu affecting camera service?

I'm trying to create a Glass GDK app that uses the Camera service to show a preview. Unfortunately, I currently have a bug where a RuntimeException is thrown when trying to open a Camera using Camera.open(). I only encounter this bug when opening the activity through a voice trigger, not by selecting the app from the "launcher" menu.
Is there a difference in how an Activity is launched through this menu versus the voice trigger?
Some of the relevant code is below.
#Override
public void onCreate(Bundle savedInstanceState) {
mGestureDetector = createGestureDetector(this);
super.onCreate(savedInstanceState);
ctx = this;
act = this;
setContentView(R.layout.activity_main);
preview = new Preview(this, (SurfaceView)findViewById(R.id.surfaceView));
((FrameLayout) findViewById(R.id.preview)).addView(preview);
preview.setKeepScreenOn(true);
}
#Override
protected void onResume() {
super.onResume();
try {
if (camera == null) {
Log.d(TAG, "Opening a camera on resume.");
camera = Camera.open();
preview.setCamera(camera);
camera.startPreview();
}
} catch(java.lang.RuntimeException e) {
Log.e(TAG, e.getMessage());
}
}
#Override
protected void onPause() {
if(camera != null) {
camera.stopPreview();
preview.setCamera(null);
Log.d(TAG, "Releasing a camera on pause.");
camera.release();
camera = null;
}
super.onPause();
}
#Override
protected void onDestroy() {
if(camera != null) {
camera.stopPreview();
preview.setCamera(null);
Log.d(TAG, "Releasing a camera on destory.");
camera.release();
camera = null;
}
super.onDestroy();
}
Since it doesn't work when using the voice trigger, it sounds like a possible race condition where the microphone isn't released by the time your activity is displayed on the screen.
Can you try an approach that uses exponential back-off to capture the camera? Basically try to capture the camera and if you get an exception, try again after a short amount of time, increasing the wait time slightly for a fixed number of attempts.
Please also consider filing a bug on the issue tracker, especially if you can reliably find out how much of a delay is needed before the camera/mic can be acquired.
The problem is caused by the delay between the voice recogniser closing event and the camera open event, which is causing a memory overload.
To avoid the problem when launching the app which will be triggered with voice,
pause the app for certain time (1000 Milli seconds will do) from opening the camera soon.
In the below code I am delaying my QR scanner to open from opening for 1000 Milli seconds. This works fine for me. If you want a you can increase the time interval.
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, 0);
}
};
// sleeper time
handler.sendEmptyMessageDelayed(0, 1000);

Update: Soundboard Application Source Code

OK I got it to work. This will save the file as a ringtone, notification, or alarm based on the context menu. (Only ringtone function is shown due to space conservation)
Need help with:
For some reason no sound plays after awhile.(about 20 or so presses and won't play again until you back out of the app and launch it again) Also I've been told "/sdcard/media/etc" isn't "the correct way" to do it.
If anyone has any suggestions on how to
1.release/pause/stop the sound from playing when the home button is pressed, a text is recieved, or the back button is pressed to exit the app, etc
and
2.the correct way to get the sdcard. I'd appreciate it.
MediaPlayer mp1;
MediaPlayer mp2;
MediaPlayer mp3;
MediaPlayer mp4;
MediaPlayer mp5;
protected void onDestroy() {
super.onDestroy();
if(mp1 != null){
mp1.release();
}
if(mp2 != null){
mp2.release();
}
if(mp3 != null){
mp3.release();
}
if(mp4 != null){
mp4.release();
}
if(mp5 != null){
mp5.release();
}
}
protected void onPause() {
super.onPause();
if(mp1 != null){
mp1.stop();
}
if(mp2 != null){
mp2.stop();
}
if(mp3 != null){
mp3.stop();
}
if(mp4 != null){
mp4.stop();
}
if(mp5 != null){
mp5.stop();
}
}
}
protected void onResume() {
super.onResume();
}
Answer to Q1: Add a onPause method to your activity and call MediaPlayer's stop() method. You should also add a onDestroy method and call release to free the resources used by the mediaplayer.
Answer to Q2: Take a look at this post. For more details read the Android Developers info on Data Storage

Categories

Resources