Camera SurfaceView在启动几次后崩溃

时间:2013-07-11 15:10:08

标签: android android-camera surfaceview

我正在开发一款游戏,它有两个活动,一个是相机surfaceView和一个火按钮,除了作为imageView的武器。两个活动有相同的代码。在活动之间切换几次应用程序崩溃。这是我的代码,下面是堆栈跟踪:

    public class MainActivity extends Activity implements SurfaceHolder.Callback,OnClickListener {

Camera mCamera;
SurfaceView mPreview;
Button shoot;
TextView rounds;
int shootsound;
int reloadsound;
int music;

SoundPool spool;




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    new TapContextSDK(getApplicationContext()).initialize();  
    new TapContextSDK(getApplicationContext()).showAd();   
    mPreview = (SurfaceView)findViewById(R.id.preview);
    mPreview.getHolder().addCallback(this);
    mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    mCamera = Camera.open();
    shoot=(Button)findViewById(R.id.button1);
    shoot.setOnClickListener(this);
    rounds=(TextView)findViewById(R.id.textView1);



    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    spool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);

    shootsound= spool.load(this, R.raw.shoot, 2);
    reloadsound= spool.load(this, R.raw.reload, 2);
    music= spool.load(this, R.raw.music, 2);

    new TapContextSDK(getApplicationContext()).initialize();  
    new TapContextSDK(getApplicationContext()).showAd(); 


}

@Override
public void onPause() {
    super.onPause();
    mCamera.stopPreview();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mCamera.release();

}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {       
    Parameters parameters = mCamera.getParameters();
    List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
    Camera.Size previewSize = previewSizes.get(4); //480h x 720w

    parameters.setPreviewSize(previewSize.width, previewSize.height);
    parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);

    mCamera.setParameters(parameters);

    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    if(display.getRotation() == Surface.ROTATION_0) {                        
        mCamera.setDisplayOrientation(90);
    } else if(display.getRotation() == Surface.ROTATION_270) {
        mCamera.setDisplayOrientation(180);
    }

    mCamera.startPreview();
}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        mCamera.setPreviewDisplay(mPreview.getHolder());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.i("PREVIEW","surfaceDestroyed");
}

public void shoot(){
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    spool.play(shootsound, volume, volume, 1, 0, 1f);
    }

public void reload(){
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    spool.play(reloadsound, volume, volume, 1, 0, 1f);
    }

public void music(){
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    spool.play(music, volume, volume, 1, 0, 1f);
    }

public void vibrateMe() {


    Vibrator vibrate = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);

    vibrate.vibrate(100);
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

    switch(arg0.getId()) {


    case R.id.button1:

        int tmp = Integer.valueOf( rounds.getText().toString());

        if(tmp==0)
        {
            rounds.setText("12");
            reload();
        }
        else
        {
            shoot();
            vibrateMe();
            rounds.setText( String.valueOf( tmp-1) );
        }



      break;

}
     }}

堆栈追踪:

07-11 17:42:15.241: E/AndroidRuntime(28651): FATAL EXCEPTION: main
07-11 17:42:15.241: E/AndroidRuntime(28651): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jouni.camsniper/com.jouni.camsniper.Machinegun}: java.lang.RuntimeException: Fail to connect to camera service
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.os.Looper.loop(Looper.java:154)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.app.ActivityThread.main(ActivityThread.java:4945)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at java.lang.reflect.Method.invokeNative(Native Method)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at java.lang.reflect.Method.invoke(Method.java:511)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at dalvik.system.NativeStart.main(Native Method)
07-11 17:42:15.241: E/AndroidRuntime(28651): Caused by: java.lang.RuntimeException: Fail to connect to camera servicef
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.hardware.Camera.native_setup(Native Method)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.hardware.Camera.<init>(Camera.java:307)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.hardware.Camera.open(Camera.java:283)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at com.jouni.camsniper.Machinegun.onCreate(Machinegun.java:52)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.app.Activity.performCreate(Activity.java:4531)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
07-11 17:42:15.241: E/AndroidRuntime(28651):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
07-11 17:42:15.241: E/AndroidRuntime(28651):    ... 11 more
07-11 17:42:18.924: D/Process(28651): killProcess, pid=28651

任何建议如何让应用程序更稳定(没有崩溃)??

0 个答案:

没有答案