2013-01-08 35 views
1

我正在嘗試應用程序,當我們將短信發送至手機時,相機將自動開啓視頻模式,並開始錄製視頻。當手機不是睡眠模式應用程序工作正常。但問題是,如果手機處於睡眠模式,視頻記錄不起作用,它被拋出停止在mMediaRecorder.prepare();執行。這個我使用接收SMS代碼:當手機處於睡眠模式時,視頻錄製無法正常工作

public void onReceive(final Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     final Bundle bundle = intent.getExtras(); 

     pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); 
     wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My tag"); 
     wl.acquire(); 



     Thread timer = new Thread() 
      { 
       public void run() 
       { 
        try { 


         if(bundle != null) 
         { 
          Object[] pdus = (Object[]) bundle.get("pdus"); 

          SmsMessage[] msg = new SmsMessage[pdus.length]; 

          for(int i = 0; i <msg.length; i++) 
          { 
           msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 
           incomingMsg = msg[i].getMessageBody().toString(); 
          } 
         } 

         System.out.println("incomingMsg :"+incomingMsg); 

         if(incomingMsg.length() > 0 && incomingMsg.trim().contains("start")) 
         { 
          System.out.println("starting activity"); 
          Intent in =new Intent(context, CameraRecorderActivity.class); 
          in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          context.startActivity(in); 
         } 
         else if(incomingMsg.length() > 0 && incomingMsg.trim().contains("stop")) 
         { 
          System.out.println("sending broadcast"); 
          Intent in =new Intent(SMS_RECEIVE); 
//       in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          in.putExtra("key", "stop"); 
          context.sendBroadcast(in); 
         } 

         sleep(100); 

         } catch (InterruptedException e) { 
          // TODO: handle exception 
          }finally{ 
//        wl.release(); 
           } 
          } 
      }; timer.start(); 


    } 

這個代碼我習慣錄製視頻,//我在這裏得到真正的問題

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Create an instance of Camera. 

    KeyguardManager manager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE); 
    KeyguardLock lock = manager.newKeyguardLock(KEYGUARD_SERVICE); 
    lock.disableKeyguard(); 

    mCamera = getCameraInstance(); 
    // Create preview view and set it as the content of our activity. 
    mPreview = new CameraPreview(this, mCamera); 
    int i = R.id.videoview; 
    Object o = this.findViewById(i); 
    FrameLayout preview = (FrameLayout) o; 
    preview.addView(mPreview); 



    System.out.println("before run method.."+mCamera); 




    new Handler().postDelayed(new Runnable(){ 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 

      System.out.println("in run method.."+mCamera); 
      if (isRecording) { 
       // stop recording and release camera 
       mMediaRecorder.stop(); // stop the recording 
       releaseMediaRecorder(); // release the MediaRecorder object 
       mCamera.lock();   // take camera access back from MediaRecorder 

       // inform the user that recording has stopped 
       setCaptureButtonText("Capture"); 
       isRecording = false; 
      } else { 
       // initialize video camera 
       if (prepareVideoRecorder()) { 
        // Camera is available and unlocked, MediaRecorder is prepared, 
        // now you can start recording 
        System.out.println("media recorder before start..."); 
        mMediaRecorder.start(); 
        System.out.println("media recorder start..."); 
        // inform the user that recording has started 
        setCaptureButtonText("Stop"); 
        isRecording = true; 
       } else { 
        // prepare didn't work, release the camera 
        releaseMediaRecorder(); 
        // inform user 
       } 
      } 

     } 

    }, 10000); 


mHandleMessageReceiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 
      String task = intent.getExtras().getString("key"); 
      System.out.println("receiving broadcast :"+task); 

      if(task.equals("stop")) 
      { 
       if (isRecording) { 
//     unregisterReceiver(mHandleMessageReceiver); 
        // stop recording and release camera 
        mMediaRecorder.stop(); // stop the recording 
        releaseMediaRecorder(); // release the MediaRecorder object 
        mCamera.lock();   // take camera access back from MediaRecorder 

        // inform the user that recording has stopped 
        setCaptureButtonText("Capture"); 
        isRecording = false; 


        finish(); 
       } else { 
        // initialize video camera 
        if (prepareVideoRecorder()) { 
         // Camera is available and unlocked, MediaRecorder is prepared, 
         // now you can start recording 
         mMediaRecorder.start(); 

         // inform the user that recording has started 
         setCaptureButtonText("Stop"); 
         isRecording = true; 
        } else { 
         // prepare didn't work, release the camera 
         releaseMediaRecorder(); 
         // inform user 
        } 
       } 
      } 
     } 
    }; 

    registerReceiver(mHandleMessageReceiver,new IntentFilter(SMS_RECEIVE)); 

// Add a listener to the Capture button 
    captureButton = (Button) findViewById(R.id.mybutton); 
    captureButton.setOnClickListener(
     new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (isRecording) { 
        // stop recording and release camera 
        mMediaRecorder.stop(); // stop the recording 
        releaseMediaRecorder(); // release the MediaRecorder object 
        mCamera.lock();   // take camera access back from MediaRecorder 

        // inform the user that recording has stopped 
        setCaptureButtonText("Capture"); 
        isRecording = false; 
       } else { 
        // initialize video camera 
        if (prepareVideoRecorder()) { 
         // Camera is available and unlocked, MediaRecorder is prepared, 
         // now you can start recording 
         mMediaRecorder.start(); 

         // inform the user that recording has started 
         setCaptureButtonText("Stop"); 
         isRecording = true; 
        } else { 
         // prepare didn't work, release the camera 
         releaseMediaRecorder(); 
         // inform user 
        } 
       } 
      } 
     } 
    ); 
} 

public void setCaptureButtonText(String s) { 
    captureButton.setText(s); 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    // The Surface has been created, now tell the camera where to draw the preview. 
    try { 
     mCamera.setPreviewDisplay(holder); 
     mCamera.startPreview(); 
    } catch (IOException e) { 
     Log.d(TAG, "Error setting camera preview: " + e.getMessage()); 
    } 
} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
    // TODO Auto-generated method stub 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    // TODO Auto-generated method stub 
} 

/** Create a File for saving an image or video */ 
private static File getOutputMediaFile(int type){ 
    // To be safe, you should check that the SDCard is mounted 
    // using Environment.getExternalStorageState() before doing this. 
// File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp"); 


    File rootDir = Environment.getExternalStorageDirectory(); 
    File path = new File(rootDir.getAbsolutePath(),"/Srinivas/"); 
    // This location works best if you want the created images to be shared 
    // between applications and persist after your app has been uninstalled. 
    // Create the storage directory if it does not exist 
    if (! path.exists()){ 
     if (! path.mkdirs()){ 
      Log.d("MyCameraApp", "failed to create directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE){ 
     mediaFile = new File(path + File.separator + 
     "IMG_"+ timeStamp + ".jpg"); 
    } else if(type == MEDIA_TYPE_VIDEO) { 
     mediaFile = new File(path.getPath() + File.separator + 
     "VID_"+ timeStamp + ".3gp"); 
    } else { 
     return null; 
    } 

    System.out.println("mediaFile :"+mediaFile); 
    return mediaFile; 
} 

private boolean prepareVideoRecorder(){ 

    try { 
    //mCamera = getCameraInstance(); 
    mMediaRecorder = new MediaRecorder(); 
    mCamera.stopPreview() ; 
    // Step 1: Unlock and set camera to MediaRecorder 
    mCamera.unlock(); 
    mMediaRecorder.setCamera(mCamera); 
    // Step 2: Set sources 
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
// mMediaRecorder.setVideoSize(240, 240); 
    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) 
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW)); 
    // Step 4: Set output file 
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); 

    System.out.println("1"); 
    // Step 5: Set the preview output 
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); 
    // Step 6: Prepare configured MediaRecorder 
    System.out.println("2"); 
     mMediaRecorder.prepare(); 
     System.out.println("3"); 
    } catch (IllegalStateException e) { 
     Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage()); 
     e.printStackTrace(); 
     releaseMediaRecorder(); 
     return false; 
    } catch (IOException e) { 
     Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage()); 
     e.printStackTrace(); 
     releaseMediaRecorder(); 
     return false; 
    } catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return true; 
} 
+0

不要讓設備休眠 –

回答

0

這是由設計。相機在睡眠設備中不起作用。如果您的應用在收到短信時醒來,您可以啓動「活動」,並打開屏幕。

爲此,Activity.onCreate()應設置以下標誌:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
相關問題