Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

//MediaPlayer

//#1 activity layout


//#2 put mp3 in sdcard/Music
//#3 activity onCreate
final MediaPlayer player = new MediaPlayer();
try{
player.setDataSpurce("/sdcard/Music/maine.mp3");
player.prepare();

}catch(Exception e){
e.printStackTrace();
}
//start button click
player.start();
//pause button click
player.pause();
//stop button
player.stop();
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////
//Play video by MediaController and VideoView
//MediaController: contains playback contols
//VideoView: play and control video player

//#1 add VideoView to activity layout


<VideoView id="@+id/videoview_id"/>
//#2 put video in sdcard/media
//#3 get VideoVoew ref in actvity
//#4 attach video view to mediacontroller
MediaController media = new MediaController(this);
media.setAnchorView(videoView);
Uri uri = Uri.parse(Enviroment.getExternalStorageDirectory().getPath() +
"//media/1.mp4"); //file location
//Setting media controller and uri
videoView.setMediaController(media); videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start(); //start videoview
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////
//MediaRecorder: record audio and video

//#1 activity layout


<Button android:onClick="startRecording"/>
<Button android:onClick="stopRecording"/>
//#2 create recorder and file
MediaRecorder recorder; File audiofile = null; static final String TAG =
"MediaRecording";
//#3 get reference views in onCreate()

//#4 start redording


public void startRecording(View view) throws IOException{
startButton.setEnabled(false); stopButton.setEnabled(true);
File dir = Enviroment.getExternalStorageDirectory(); //create file
try{
audiofile = File.createTempFile("sound", ".3gp", dir);
}catch(IOException e){
Log.e(TAG, "external storage access error");
return;
}
//creating media recorder and specifying audio source, output format, encoder
and output format
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
recorder.prepare();
recorder.start();
}
//#5 stop recording
public void stopRecording(View view){
startButton.setEnabled(true); stopButton.setEnabled(false);
//stopping recorder
recorder.stop(); recorder.release();
//create sound file and add it to media library
addRecordingToMediaLibrary();
}
//#6 add file to media library
protected void addRecordingToMediaLibrary(){
//creating content values of size 4
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());
//creating content resolver and storing it in external content uri
ContentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
//sending broadcast message to scan media file so that it can be avilable
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
Toast.makeText(, "Added File " + newUri);
}

You might also like