我正在尝试制作一个简单的录像机应用程序,其中我指定最大文件大小为给定数字。这样的应用程序工作正常,但是一旦录制完成,我无法显示通知用户已保存视频剪辑的Toast。这是我的代码:
package com.example.videocapture2;
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements SurfaceHolder.Callback,android.media.MediaRecorder.OnInfoListener{
MediaRecorder recorder;
SurfaceHolder holder;
Button Rec = null;
boolean recording = false;
int count =1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
recorder = new MediaRecorder();
initRecorder();
setContentView(R.layout.activity_main);
SurfaceView cameraView = (SurfaceView) findViewById(R.id.videoview);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//recorder.start();
//cameraView.setClickable(true);
// cameraView.setOnClickListener(this);
Rec = (Button)findViewById(R.id.mybutton);
Rec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (recording) {
recorder.stop();
recording = false;
// Let's initRecorder so we can record again
initRecorder();
prepareRecorder();
} else {
recording = true;
recorder.start();
}
}
});
}
private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile("/sdcard/videocapture_example"+count+".mp4");
recorder.setMaxDuration(50000); // 50 seconds
recorder.setMaxFileSize(2*1048576); // Approximately 2 megabytes
count++;
}
private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
public void surfaceCreated(SurfaceHolder holder) {
prepareRecorder();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (recording) {
recorder.stop();
recording = false;
}
recorder.release();
//Toast.makeText(getApplicationContext(), "Video is saved", Toast.LENGTH_LONG).show();
//finish();
}
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
// TODO Auto-generated method stub
System.out.println("Reached onInfoListener");
if(what==android.media.MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED)
{
Toast.makeText(getApplicationContext(), "Video clip recorded", Toast.LENGTH_LONG).show();
}
}
}
我发现由于
recorder.setMaxFileSize(2*1048576); // Approximately 2 megabytes
在 initRecorder()方法中,一旦达到指定的文件大小,就会通过特定的 代码向OnInfoListener发送通知。所以我在我的MainActivity中覆盖了这个方法,但是,在视频文件被完全记录后,吐司不显示,但是我能够访问视频片段并播放它。
在上述条件下,我应该怎么做这个吐司?
答案 0 :(得分:1)
我认为你还没有分配onInfoListener。即你需要:
recorder.setOnInfoListener(this);
因为您的活动扩展了MediaRecorder.OnInfoListener