Keep app running in background on Android - java

I am really new to Android Programming. I am working on a simple app: FlashLight for Android.
I need the app to continue run in background if the user presses the menubutton or locks the screen. The light should not stop until the user presses the button the stop the light.
Below is my code:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity
extends Activity {
ImageButton btnSwitch;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// flash switch button
btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
// First check if device is supporting flashlight or not
hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
}
});
alert.show();
return;
}
// get the camera
getCamera();
// displaying button image
toggleButtonImage();
// Switch button click event to toggle flash on/off
btnSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
}
// Get the camera
private 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());
}
}
}
// Turning On flash
private void turnOnFlash() {
if(!isFlashOn) {
if(camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
}
// Turning Off flash
private void turnOffFlash() {
if(isFlashOn) {
if(camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
// changing button/switch image
toggleButtonImage();
}
}
/*
* Toggle switch button images
* changing image states to on / off
* */
private void toggleButtonImage() {
if(isFlashOn) {
btnSwitch.setImageResource(R.drawable.btn_switch_on);
} else {
btnSwitch.setImageResource(R.drawable.btn_switch_off);
}
}
/*
* Playing sound
* will play button toggle sound on flash on / off
* */
private void playSound() {
if(isFlashOn) {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
protected void onPause() {
super.onPause();
// on pause turn off the flash
turnOffFlash();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onResume() {
super.onResume();
// on resume turn on the flash
if(hasFlash) {
turnOnFlash();
}
}
#Override
protected void onStart() {
super.onStart();
// on starting the app get the camera params
getCamera();
}
#Override
protected void onStop() {
super.onStop();
// on stop release the camera
if(camera != null) {
camera.release();
camera = null;
}
}
}

You;re turning off the light in onPause. When the screen is locked or the app is minimized, onPause is called. If you don't want that to happen, don't call it there.

I think you are searching for Services: http://developer.android.com/guide/components/services.html
They are very similar to an Activity but do dot provide a user interface and can run in the background.

Related

setPreviewDisplay causes activity to crash in reloading of surfaceview

I have an application that uses a barcode scanner to pass data to the next activity. I have managed to pass the data over to the next activity but when I return to the barcode scanner the app crashes. Im presuming it is because it is trying to open the Surfaceview again.
How would I continue with the surfaceview when going back to the barcode scanner
****Barcode Scanner ****
public class BarcodeScannerNew extends AppCompatActivity {
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
private Button scanButton;
private ImageScanner scanner;
private boolean barcodeScanned = false;
private boolean previewing = true;
static {
System.loadLibrary("iconv");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_barcode_window);
initControls();
}
private void initControls() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
// Instance barcode scanner
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 10);
scanner.setConfig(0, Config.Y_DENSITY, 10);
mPreview = new CameraPreview(BarcodeScannerNew.this, mCamera, previewCb,
autoFocusCB);
FrameLayout preview = (FrameLayout) findViewById(R.id.cameraPreview);
preview.addView(mPreview);
scanButton = (Button) findViewById(R.id.ScanButton);
scanButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (barcodeScanned) {
barcodeScanned = false;
mCamera.setPreviewCallback(previewCb);
mCamera.startPreview();
previewing = true;
mCamera.autoFocus(autoFocusCB);
}
}
});
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
releaseCamera();
}
return super.onKeyDown(keyCode, event);
}
/**
* A safe way to get an instance of the Camera object.
*/
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open();
} catch (Exception e) {
}
return c;
}
private void releaseCamera() {
if (mCamera != null) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
private Runnable doAutoFocus = new Runnable() {
public void run() {
if (previewing)
mCamera.autoFocus(autoFocusCB);
}
};
Camera.PreviewCallback previewCb = new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
int result = scanner.scanImage(barcode);
if (result != 0) {
previewing = false;
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
TextView labelScanResult = (TextView)findViewById(R.id.labelScanResult);
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
Log.i("<<<<<<Asset Code>>>>> ",
"<<<<Bar Code>>> " + sym.getData());
String scanResult = sym.getData().trim();
// shows dialog showAlertDialog(scanResult);
labelScanResult.setText(scanResult);
/* Toast.makeText(BarcodeScanner.this, scanResult,
Toast.LENGTH_SHORT).show();*/
Intent i = new Intent(BarcodeScannerNew.this, BarcodeScanner.class);
i.putExtra("" , labelScanResult.getText().toString());
startActivity(i);
finish();
barcodeScanned = true;
break;
}
}
}
};
// Mimic continuous auto-focusing
Camera.AutoFocusCallback autoFocusCB = new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 1000);
}
};
private void showAlertDialog(String message) {
new AlertDialog.Builder(this)
.setCancelable(false)
.setMessage(message)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.homebutton){
startActivity(new Intent(this,MainActivity.class));
}
return super.onOptionsItemSelected(item);
}
}
Camera activity
package com.example.dsouchon.myapplication;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private Camera.PreviewCallback previewCallback;
private Camera.AutoFocusCallback autoFocusCallback;
public CameraPreview(Context context, Camera camera,
Camera.PreviewCallback previewCb,
Camera.AutoFocusCallback autoFocusCb) {
super(context);
mCamera = camera;
previewCallback = previewCb;
autoFocusCallback = autoFocusCb;
/*
* Set camera to continuous focus if supported, otherwise use
* software auto-focus. Only works for API level >=9.
*/
/*
Camera.Parameters parameters = camera.getParameters();
for (String f : parameters.getSupportedFocusModes()) {
if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
autoFocusCallback = null;
break;
}
}
*/
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
}
catch (IOException e) {
Log.d("DBG", "Error setting camera preview: " + e.getMessage());
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
/*
* If your preview can change or rotate, take care of those events here.
* Make sure to stop the preview before resizing or reformatting it.
*/
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
try {
// Hard code camera surface rotation 90 degs to match Activity view in portrait
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mHolder);
mCamera.setPreviewCallback(previewCallback);
mCamera.startPreview();
mCamera.autoFocus(autoFocusCallback);
} catch (Exception e) {
Log.d("DBG", "Error starting camera preview: " + e.getMessage());
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
Logs
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.setPreviewDisplay(android.view.SurfaceHolder)' on a null object reference
at com.example.dsouchon.myapplication.CameraPreview.surfaceCreated(CameraPreview.java:56)
at android.view.SurfaceView.updateWindow(SurfaceView.java:607)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:184)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:921)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2164)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

Android KeyEvents from Service API22

Asking in the name of a friend because I already have an account with rep.
"I have a small project going on that is supposed to activate my phone's flashlight when I press the volume up button and disable it when I press the volume down button. This is so I can easily access the flashlight even when wearing gloves. My problem here is that the app should be running as a service in the background and work when the screen is turned off and that is not possible... or so it seems."
What we have tried:
Background Activity
Media Session
Compiling SDL C++ and polling Keys from there
Broadcast Reciever
But none of these worked.
Here is the current version of the service file:
//imports
public class FlashService extends Service {
public static final String BROADCAST = "android.intent.action.MEDIA_BUTTON";
private static final String TAG = "OutdoorFlashlightService";
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_REDELIVER_INTENT;
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#SuppressWarnings("deprecation")
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.w(TAG, "Service started!");
MediaSession _mediaSession = new MediaSession(getApplicationContext(), TAG);
if (_mediaSession == null) {
Log.e(TAG, "initMediaSession: _mediaSession = null");
return;
}
Token _mediaSessionToken = _mediaSession.getSessionToken();
_mediaSession.setCallback(new Callback() {
public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
Log.d(TAG, "onMediaButtonEvent called: " + mediaButtonIntent);
return super.onMediaButtonEvent(mediaButtonIntent);
}
public void onPause() {
Log.d(TAG, "onPause called (media button pressed)");
super.onPause();
}
public void onPlay() {
Log.d(TAG, "onPlay called (media button pressed)");
super.onPlay();
}
public void onStop() {
Log.d(TAG, "onStop called (media button pressed)");
super.onStop();
}
});
_mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
PlaybackState state = new PlaybackState.Builder().setActions(PlaybackState.ACTION_PLAY)
.setState(PlaybackState.STATE_STOPPED, PlaybackState.PLAYBACK_POSITION_UNKNOWN, 0).build();
_mediaSession.setPlaybackState(state);
_mediaSession.setActive(true);
}
}
Here is the failed attempt at a BroadcastReciever, that never actually recieved anything:
package de.platzhirsch.outdoorflashlight;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.util.Log;
import android.view.KeyEvent;
#SuppressWarnings("deprecation")
public class MyBroadcastReceiver extends BroadcastReceiver {
private Camera cam;
private boolean lighting = false;
public MyBroadcastReceiver() {
super();
Log.w("", "Receiver registered!");
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.w("", "Got msg!");
String intentAction = intent.getAction();
KeyEvent event = null;
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
}
if (event == null) {
return;
}
Log.w("", "triggered...");
int keycode = event.getKeyCode();
int action = event.getAction();
long eventtime = event.getEventTime();
if (event.getAction() == KeyEvent.ACTION_DOWN) {
Camera.Parameters parameters;
if (cam == null) {
cam = Camera.open();
}
switch (keycode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (!lighting) {
parameters = cam.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cam.setParameters(parameters);
cam.startPreview();
lighting = true;
}
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (lighting) {
parameters = cam.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
cam.setParameters(parameters);
cam.stopPreview();
cam.release();
cam = null;
lighting = false;
}
break;
}
}
}
}
What we are looking for is really just a way to get the Volume KeyEvents to the service
Note: The app is only intended to run on my friend's device so if all else fails a dirty device specific solution with Hardware IDs will suffice

Flashlight won't turn on

Hi I followed I followed a tutorial here and wrote out the code, I tested it but the flashlight does not turn on and I cant figure out what's gone wrong.
I left out the parts where there was sound because I preferred there not to be any.
package net.netne.benpaterson35;
import android.app.*;
import android.os.*;
import android.hardware.*;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity
{
Button mainButton1;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainButton1 = (Button) findViewById(R.id.mainButton1);
hasFlash = getApplicationContext().getPackageManager().
hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
.create();
alert.setTitle("Sorry");
alert.setMessage("This device doesn't have flash a flash light");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which){
finish();
}
});
alert.show();
return;
}
getCamera();
mainButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
if (isFlashOn){
turnOffFlash();}
else {
turnOnFlash();
}
};
});
}
private void getCamera(){
if (camera == null){
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Failed to open camera", e.getMessage());
}
}
}
private void turnOnFlash() {
if (!isFlashOn){
if (camera == null || params == null){
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
}
}
private void turnOffFlash() {
if (isFlashOn){
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
}
}
#Override
protected void onDestroy()
{
super.onDestroy();
}
#Override
protected void onPause()
{
super.onPause();
turnOffFlash();
}
#Override
protected void onRestart()
{
super.onRestart();
}
#Override
protected void onResume()
{
super.onResume();
if(hasFlash)
turnOnFlash();
}
#Override
protected void onStart()
{
super.onStart();
getCamera();
}
#Override
protected void onStop()
{
super.onStop();
if (camera != null) {
camera.release();
camera = null;
}
}
}
Thanks for reading :)
if you are working on new device the code you followed wont work on newer devices i made it work on Lollipop device by following this code give it a try
mCam = Camera.open();
Camera.Parameters p = mCam.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCam.setParameters(p);
mPreviewTexture = new SurfaceTexture(0);
try {
mCam.setPreviewTexture(mPreviewTexture);
} catch (IOException ex) {
// Ignore
}
mCam.startPreview();
Did you add
<uses-permission android:name="android.permission.CAMERA" />
to your AndroidManifest?
Highly suspect you didn't add the permission.

Application Crashes when resuming the app

I am creating a simple Flashlight App, but every time I leave the app and reopen it, it crashes. Am I missing something in my code, because I am not sure what, below is an error when it crashes. Please let me know what I need to do in order to fix this resume issue:
package com.example.gkvxm.materiallight;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Toast;
import com.dd.CircularProgressButton;
import java.io.IOException;
public class FlashLightActivity extends Activity implements SurfaceHolder.Callback {
private boolean isLigtOn = false;
private Camera camera;
#Override
protected void onStart(){
super.onStart();
SurfaceView preview = (SurfaceView)findViewById(R.id.PREVIEW);
SurfaceHolder mHolder = preview.getHolder();
mHolder.addCallback(this);
}
#Override
protected void onStop(){
super.onStop();
if(camera!=null){
camera.release();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_light);
Context context = this;
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(FlashLightActivity.this, "Your Device is not supported", Toast.LENGTH_SHORT).show();
Log.e("err", "Device is not supported");
return;
}
camera = Camera.open();
final Camera.Parameters p = camera.getParameters();
final CircularProgressButton circularButton1 = (CircularProgressButton) findViewById(R.id.btnWithText);
circularButton1.setIndeterminateProgressMode(true);
circularButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isLigtOn) {
turnOffFlash(p);
Toast.makeText(FlashLightActivity.this, "Lights Off!", Toast.LENGTH_SHORT).show();
} else {
turnOnFlash(p);
Toast.makeText(FlashLightActivity.this, "Lights On!", Toast.LENGTH_SHORT).show();
}
if (circularButton1.getProgress() == 0) {
simulateSuccessProgress(circularButton1);
} else {
circularButton1.setProgress(0);
}
}
});
}
private void turnOnFlash(Camera.Parameters p){
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLigtOn = true;
}
private void turnOffFlash(Camera.Parameters p){
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLigtOn = false;
}
#Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){
}
#Override
public void surfaceCreated(SurfaceHolder holder){
try{
Log.i("SurfaceHolder","Setting preview");
camera.setPreviewDisplay(holder);
} catch (IOException e){
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder){
Log.i("SurfaceHOlder", "stopping preview");
camera.stopPreview();
holder = null;
}
private void simulateSuccessProgress(final CircularProgressButton button) {
ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 100);
widthAnimation.setDuration(1500);
widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
button.setProgress(value);
}
});
widthAnimation.start();
}
private void simulateErrorProgress(final CircularProgressButton button) {
ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 99);
widthAnimation.setDuration(1500);
widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
button.setProgress(value);
if (value == 99) {
button.setProgress(-1);
}
}
});
widthAnimation.start();
}
}
Error :
05-22 03:08:35.646 13909-13909/com.example.gkvxm.materiallight E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.gkvxm.materiallight, PID: 13909
java.lang.RuntimeException: Camera is being used after Camera.release() was called
at android.hardware.Camera._stopPreview(Native Method)
at android.hardware.Camera.stopPreview(Camera.java:732)
at com.example.gkvxm.materiallight.FlashLightActivity.surfaceDestroyed(FlashLightActivity.java:129)
at android.view.SurfaceView.updateWindow(SurfaceView.java:564)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:238)
at android.view.View.dispatchWindowVisibilityChanged(View.java:8785)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1164)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1164)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1164)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1164)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1318)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Camera is being used after Camera.release() was called. So, looking at the activity lifecycle, you need to open the camera in onStart() or in onRestart(). So Maybe this code will work fine ...
package com.example.gkvxm.materiallight;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Toast;
import com.dd.CircularProgressButton;
import java.io.IOException;
public class FlashLightActivity extends Activity implements SurfaceHolder.Callback {
private boolean isLigtOn = false;
private Camera camera;
#Override
protected void onStart(){
super.onStart();
camera = Camera.open();
final Camera.Parameters p = camera.getParameters();
SurfaceView preview = (SurfaceView)findViewById(R.id.PREVIEW);
SurfaceHolder mHolder = preview.getHolder();
mHolder.addCallback(this);
}
#Override
protected void onStop(){
super.onStop();
if(camera!=null){
camera.release();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_light);
Context context = this;
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(FlashLightActivity.this, "Your Device is not supported", Toast.LENGTH_SHORT).show();
Log.e("err", "Device is not supported");
return;
}
camera = Camera.open();
final Camera.Parameters p = camera.getParameters();
final CircularProgressButton circularButton1 = (CircularProgressButton) findViewById(R.id.btnWithText);
circularButton1.setIndeterminateProgressMode(true);
circularButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isLigtOn) {
turnOffFlash(p);
Toast.makeText(FlashLightActivity.this, "Lights Off!", Toast.LENGTH_SHORT).show();
} else {
turnOnFlash(p);
Toast.makeText(FlashLightActivity.this, "Lights On!", Toast.LENGTH_SHORT).show();
}
if (circularButton1.getProgress() == 0) {
simulateSuccessProgress(circularButton1);
} else {
circularButton1.setProgress(0);
}
}
});
}
private void turnOnFlash(Camera.Parameters p){
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLigtOn = true;
}
private void turnOffFlash(Camera.Parameters p){
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLigtOn = false;
}
#Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){
}
#Override
public void surfaceCreated(SurfaceHolder holder){
try{
Log.i("SurfaceHolder","Setting preview");
camera.setPreviewDisplay(holder);
} catch (IOException e){
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder){
Log.i("SurfaceHOlder", "stopping preview");
camera.stopPreview();
holder = null;
}
private void simulateSuccessProgress(final CircularProgressButton button) {
ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 100);
widthAnimation.setDuration(1500);
widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
button.setProgress(value);
}
});
widthAnimation.start();
}
private void simulateErrorProgress(final CircularProgressButton button) {
ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 99);
widthAnimation.setDuration(1500);
widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
button.setProgress(value);
if (value == 99) {
button.setProgress(-1);
}
}
});
widthAnimation.start();
}
}
As the Log says Camera is being used after Camera.release() was called. So, looking at the activity lifecycle you need to open camera in onStart() or in onRestart()
This exception is because of camera.release(); is called in the onStop() method of your activity. After releasing the camera instance you are reusing the released instance.
camera = Camera.open();
move the above line from OnCreate() to onStart()
You are releasing the camera instance in onPause() which is correct due to documentation:
public final void release ()
Added in API level 1 Disconnects and releases the Camera object
resources.
You must call this as soon as you're done with the Camera object.
But you also need to re-open it in onResume(), like you do in onCreate, since onCreate() not always is called when you resume the app.
As they state here:
Important: Call release() to release the camera for use by other
applications. Applications should release the camera immediately in
onPause() (and re-open() it in onResume()).
See this for more information about the android app lifecycle.
Good luck!
It may be you are camera.release(); on onStop(). And when surfaceDestroyed call you have used camera.stopPreview();
so camera is alredy release and you are going to stopePreview. thats way you are geting Camera is being used after Camera.release() was called this error.
so you have to cange that.
#Override
protected void onStop(){
super.onStop();
}
and on surfaceDestroyed
#Override
public void surfaceDestroyed(SurfaceHolder holder){
Log.i("SurfaceHOlder", "stopping preview");
camera.stopPreview();
camera.release();
holder = null;
}
I have same problem with camera class. Use this line onResume()
mCameraPreview.getHolder().removeCallback(mCameraPreview);
and its work now.
I had an app with the same issue,
when I removed "on click listener" inside my on create function, the problem was solved.
I think we should never insert any implementation function or other things inside our onCreate method...
It's better to use onCreate only for declaring and instantiating variables.

Android: playing large video MP4 files through http

I have 300 MB videos on my http server...I'm trying to make good simple code to play (in 3G mode) these videos remotely in my application - sd storing or local storing is not a option. Of course I started from demo source in SDK android-17 (MediaPlayerDemo_Video, VideoViewDemo...) and in 3G mode my video doesn't play (I'm testing on Samsung Galaxy Nexus). After that I made new try in next code:
/**
* Listing 15-4: Initializing and assigning a Surface View to a Media Player
*/
import java.io.IOException;
import mobile.dariknews.R;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
public class SurfaceViewVideoViewActivity extends Activity
implements SurfaceHolder.Callback {
static final String TAG = "SurfaceViewVideoViewActivity";
private MediaPlayer mediaPlayer;
public void surfaceCreated(SurfaceHolder holder) {
try {
// When the surface is created, assign it as the
// display surface and assign and prepare a data
// source.
mediaPlayer.setDisplay(holder);
mediaPlayer.setDataSource("http://snimkitevi-bg.com/darik/1.MP4");
//mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
Log.e(TAG, "Illegal Argument Exception", e);
} catch (IllegalStateException e) {
Log.e(TAG, "Illegal State Exception", e);
} catch (SecurityException e) {
Log.e(TAG, "Security Exception", e);
} catch (Exception e) {
Log.e(TAG, "IO Exception", e);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mediaPlayer.release();
}
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) { }
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.surfaceviewvideoviewer);
// Create a new Media Player.
mediaPlayer = new MediaPlayer();
// Get a reference to the Surface View.
final SurfaceView surfaceView =
(SurfaceView)findViewById(R.id.surfaceView);
// Configure the Surface View.
surfaceView.setKeepScreenOn(true);
// Configure the Surface Holder and register the callback.
SurfaceHolder holder = surfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400, 300);
// Connect a play button.
Button playButton = (Button)findViewById(R.id.buttonPlay);
playButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mediaPlayer.start();
}
});
// Connect a pause button.
Button pauseButton = (Button)findViewById(R.id.buttonPause);
pauseButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mediaPlayer.pause();
}
});
// Add a skip button.
Button skipButton = (Button)findViewById(R.id.buttonSkip);
skipButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mediaPlayer.seekTo(mediaPlayer.getDuration()/2);
}
});
/**
* Listing 15-5: Controlling playback using the Media Controller
*/
MediaController mediaController = new MediaController(this);
mediaController.setMediaPlayer(new MediaPlayerControl() {
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
public int getBufferPercentage() {
return 0;
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public int getDuration() {
return mediaPlayer.getDuration();
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public void pause() {
mediaPlayer.pause();
}
public void seekTo(int pos) {
mediaPlayer.seekTo(pos);
}
public void start() {
mediaPlayer.start();
}
});
}
}
after that new try in
import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.MediaController;
import android.widget.VideoView;
public class PlayerActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoplayer);
new LoadOutbox().execute();
}
/**
* Background Async Task to Load all OUTBOX messages by making HTTP Request
* */
class LoadOutbox extends AsyncTask<String, String, String> {
int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting Outbox JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setKeepScreenOn(true);
// Attach a Media Controller MediaController mediaController = new
// MediaController( this); videoView.setMediaController(
// mediaController);
MediaController mediaController = new MediaController(
PlayerActivity.this);
videoView.setMediaController(mediaController);
Uri uri = Uri.parse("http://snimkitevi-bg.com/darik/1.MP4");
videoView.setVideoURI(uri);
videoView.start();
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
}
});
}
}
public static Bitmap getLocalImageBitmap(String url) {
return MainNewsActivities.getLocalImageBitmap(url);
}
}
After that I played with 50 other tests with MediaPlayer and VIewVideo... All results are same - slow and freezed video playing. My need is pretty simple - I have big video files (mp4) on my website and I want to play them on my app. So I'm here - what is the right formula for playing of large remote files in android application?
PS: Of course I tried to use GStreamer but this framework is not compatible with last android environment - I played with in 2 days...My video is OK - in wifi mode I play http://snimkitevi-bg.com/darik/1.MP4
So could you give me some good advases, examples, tutorials how MediaPlayer could play normally, buffered and fastly big video files in 3G internet connection?

Categories