views:

99

answers:

0

import java.io.File; import java.io.FileOutputStream; import java.io.IOException;

import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.provider.Settings; import android.util.Log; import android.view.View; import android.widget.Button;

public class audio extends Activity {

public MediaRecorder mrec = null; private Button startRecording = null; private Button stopRecording = null; private static final String TAG = "SoundRecordingDemo"; File audiofile; private File video;

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mrec = new MediaRecorder(); Log.i(TAG , "Video starting"); startRecording = (Button)findViewById(R.id.startrecording); stopRecording = (Button)findViewById(R.id.stoprecording); startRecording.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { try { Context appContext = getApplicationContext(); startRecording.setEnabled(false); stopRecording.setEnabled(true); stopRecording.requestFocus(); startRecording(); }catch (Exception ee) { Log.e(TAG,"Caught io exception " + ee.getMessage()); }

} });

stopRecording.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { startRecording.setEnabled(true); stopRecording.setEnabled(false); startRecording.requestFocus(); stopRecording(); processaudiofile(); }

});

stopRecording.setEnabled(false); startRecording.setEnabled(true);

} protected void processaudiofile() { ContentValues values = new ContentValues(4); long current = System.currentTimeMillis(); values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName()); values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000)); values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp"); values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath()); ContentResolver contentResolver = getContentResolver();

Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Uri newUri = contentResolver.insert(base, values);

// this does not always seem to work cleanly.... sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri)); }

protected void startRecording() throws IOException { // mrec.setPreviewDisplay(s); mrec.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA); mrec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mrec.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mrec.setMaxDuration(20000); // length of video in MS mrec.setVideoSize(320, 240); mrec.setVideoFrameRate(15); mrec.setOutputFile(video.getPath());

// mrec.prepare(); //mrec.setOutputFile(/sdcard/yousuck2.3gp); // mRecorder.setOutputFile("/sdcard/yousuck2.3gp");

if (video == null) { File sampleDir = Environment.getExternalStorageDirectory();

try { video = File.createTempFile("videofile", ".3gp", sampleDir); } catch (IOException e) { Log.e(TAG,"sdcard access error"); return; } }

mrec.setOutputFile(video.getAbsolutePath());

mrec.prepare(); mrec.start();

}

protected void stopRecording() { mrec.stop(); mrec.release(); }

}