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;
        }
    }
}

视频播放器类:

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 回答