0

私はビデオプレーヤーを開発しています.ビデオを記録している瞬間に、StopButtonがクリックされたとき、または最大制限に近づいたときに保存します. その後、再生ボタンをクリックして再生できます。私が欲しいのは、最大制限に達すると、60 秒のビデオ録画と言って、ビデオを再生するアクティビティを自動的に起動するなどのアクションを実行できることです。以下は私のコードです:

public class VideoComponent extends Activity  {
    private SurfaceHolder surfaceHolder;
    private SurfaceView surfaceView;
    public MediaRecorder mrec = new MediaRecorder();
    private Button startRecording = null;
    private Button stopRecording = null;
    private Button play = null;
    int BytesPerElement = 2;

    File video;
    private Camera mCamera;
    File audiofile = null;
    boolean recording=false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);

        startRecording = (Button)findViewById(R.id.buttonstart);
        stopRecording = (Button)findViewById(R.id.button1);
        play=(Button)findViewById(R.id.play);


        mCamera = Camera.open();
        surfaceView = (SurfaceView) findViewById(R.id.surface_camera);


        surfaceHolder = surfaceView.getHolder();


        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        startRecording.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    if(recording==false)
                        startRecording();

                } catch (IOException e) {
                    Log.i("test" , "Video Not starting");

                }

            }
        });
        stopRecording.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                    recording=false;
                    stopRecording();

            }
        });
        play.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i= new Intent(getApplicationContext(),VideoPlayer.class);
                i.putExtra("URI", audiofile.getAbsolutePath());
                startActivity(i);
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
          Log.i("Test" , "Menu thing");
        menu.add(0, 0, 0, "StartRecording");
        menu.add(0, 1, 0, "StopRecording");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
        case 0:
            try {
                  Log.i("Test" , "Start Recording");
                startRecording();
            } catch (Exception e) {
                String message = e.getMessage();
                Log.i("Self", "Problem Start"+message);
                mrec.release();
            }
            break;

        case 1: //GoToAllNotes
            mrec.stop();
            mrec.release();
            mrec = null;
            break;

        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void startRecording() throws IOException 
    {
        recording=true;
         File sampleDir = Environment.getExternalStorageDirectory();
            try {
              audiofile = File.createTempFile("Video", ".3gp", sampleDir);
            } catch (IOException e) {

              return;}
            int minBufferSize = AudioRecord.getMinBufferSize(8000, 
                 AudioFormat .CHANNEL_IN_MONO, 
                 AudioFormat.ENCODING_PCM_16BIT);
        mrec = new MediaRecorder();  // Works well

        mCamera.unlock();
        mrec.setCamera(mCamera);

        mrec.setPreviewDisplay(surfaceHolder.getSurface());
        mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
        mrec.setMaxDuration(60000);
        mrec.setAudioSamplingRate(16000);
        mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
        mrec.setPreviewDisplay( surfaceHolder.getSurface());
        mrec.setOutputFile(audiofile.getAbsolutePath()); 
        mrec.prepare();
        mrec.start();


    }

    protected void stopRecording() {
        mrec.stop();
        mrec.release();
        mCamera.lock();
        mCamera.release();
        Log.i("testing","going to call Destroyer");
        //surfaceDestroyed(surfaceHolder);

        //mCamera.stopPreview();


         //finish();
    }

   private void releaseMediaRecorder(){
       Log.i("testing","re;ease Media record"); 
       if (mrec != null) {
            mrec.reset();   // clear recorder configuration
            mrec.release(); // release the recorder object
            mrec = null;
            mCamera.lock();           // lock camera for later use
        }
    }

    private void releaseCamera(){
           Log.i("testing","re;ease Camera");
        if (mCamera != null){
            mCamera.release();        // release the camera for other applications
            mCamera = null;
        }
    }
}

ビデオ Player クラス:

public class VideoPlayer extends Activity {
VideoView videoView;
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    //Create a VideoView widget in the layout file
    //use setContentView method to set content of the activity to the layout file which contains videoView
    this.setContentView(R.layout.video);
    Intent i= getIntent();
    String uri=i.getStringExtra("URI");
    Log.i("test" , uri);

    videoView = (VideoView)this.findViewById(R.id.videoView);

    //add controls to a MediaPlayer like play, pause.
    MediaController mc = new MediaController(this);
    videoView.setMediaController(mc);

    //Set the path of Video or URI
    videoView.setVideoURI(Uri.parse(uri));

  //Set the focus
    videoView.requestFocus();
    videoView.start();      }   }
4

1 に答える 1

0

CountDownTimer() の使用を検討してください ...

new CountDownTimer(1000, 1000) {

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!");
 }
  }.start();

http://developer.android.com/reference/android/os/CountDownTimer.html

于 2013-03-18T06:03:23.757 に答える