我的行动流程:
(点击开始按钮)
(点击停止按钮)
(保存录制的视频文件)
第9步是再次录制更多视频。
但是,此步骤发生错误。
这是我的来源:
public class Talk extends Activity实现了SurfaceHolder.Callback {
Context context;
SurfaceView sfv_Preview;
ProgressBar progressBar;
private SurfaceHolder holder;
private MediaRecorder recorder = null;
Camera camera;
boolean recording = false;
private static final String OUTPUT_FILE = "/sdcard/Vitalk/temp.mp4";
private static final String OUTPUT_FILE_NAME = "temp.mp4";
private static final int RECORDING_TIME = 10000;
//Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_talk);
File mediaStorageDir = new File("/sdcard/Vitalk/");
if (!mediaStorageDir.exists()) mediaStorageDir.mkdirs();
sfv_Preview = (SurfaceView) findViewById(R.id.sfv_Preview);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
holder = sfv_Preview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
initRecorder();
context = this;
((ImageButton)findViewById(R.id.ibt_Record)).setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
sfv_Preview.setVisibility(View.VISIBLE);
recorder.start();
recording = true;
Log.d("recod", "click start");
}
else if (MotionEvent.ACTION_UP == event.getAction() || event.getAction() == MotionEvent.ACTION_CANCEL) {
Log.d("recod", "click stop");
if (recording) {
recorder.stop();
recording = false;
recorder.release();
new Handler().post(new Runnable() {
@SuppressLint("NewApi")
@Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
}
});
uploadFile(OUTPUT_FILE);
recorder = null;
initRecorder();
prepareRecorder();
}
}
return false;
}
});
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
prepareRecorder();
}
@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
}
private void initRecorder() {
Camera camera = Camera.open(1);
camera.setDisplayOrientation(90);
camera.unlock();
recorder = new MediaRecorder();
recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_480P));
recorder.setOutputFile(OUTPUT_FILE);
recorder.setMaxDuration(RECORDING_TIME);
recorder.setMaxFileSize(10485760); // Approximately 5 megabytes
}
private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());
recorder.setOrientationHint(90);
try {
recorder.prepare();
//finish();
} catch (IllegalStateException e) {
e.printStackTrace();
//finish();
} catch (IOException e) {
e.printStackTrace();
//finish();
}
}
public void uploadFile(String sourceFileUri) {
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL("http://133.130.117.56/visualtalk/func/FileUpload.php");
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
int serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200){
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
Log.e("Upload file to server Exception", "Exception : "
+ e.getMessage(), e);
}
new Handler().post(new Runnable() {
@SuppressLint("NewApi")
@Override
public void run() {
sfv_Preview.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.INVISIBLE);
}
});
}
}
有什么问题?..
答案 0 :(得分:0)
也许您应该在下一个camera.release()
open()