MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.example);
mediaPlayer.setDataSource(uri);
// the uri can be system file or by http, rtsp protocal
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.pause();
mediaPlayer.start();
mediaPlayer.stop();
mediaPlayer.reset();
// If going to stop stage, must reset to go to preparing
mediaPlayer.release();
MediaPlayer mp = MediaPlayer.create(this, R.raw.ex);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
// holder is a class to deal with screen
mp.setDisplay(surfaceHolder);
// AudioTrack(int streamType, int sampleRateInHz,
// int channelConfig, int audioFormat,
// int bufferSizeInBytes, int mode)
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
frequency,
Channel,
SampBit,
minBufSize,
AudioTrack.MODE_STREAM);
// create the view (in this case it is already included in the layout resource)
VideoView videoview = (VideoView) findViewById(R.id.videoview);
videoview.setKeepScreenOn(true);
// used if streaming
if (videouri != null) videoview.setVideoURI(videouri);
// absolute path if it is a file
else videoview.setVideoPath(videopath);
// let's add a media control so we can control the playback
mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(videoview);
videoview.setMediaController(mediacontroller);
if (videoview.canSeekForward())
videoview.seekTo(videoview.getDuration()/2);
// start the playback
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
// initialize the MediaRecorder
MediaRecorder mediarecorder = new MediaRecorder();
// configure the data source
// the source of the audio input
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// output format
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// encoding
mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// use absolute path to file where output is stored
mediarecorder.setOutputFile("/sdcard/audiorecordexample.3gpp");
// prepare to record
mediarecorder.prepare();
Then when the recoding needs to
mediarecorder.start();
mediarecorder.pause();
mediarecorder.start();
mediarecorder.stop();
mediarecorder.release();
short[] buffer = new short[10000];
recorder = new AudioRecord( // source to record from
MediaRecorder.AudioSource.MIC,
11025,
// channel config—mono, stereo, etc.
AudioFormat.CHANNEL_CONFIGURATION_MONO,
// audio encoding
AudioFormat.ENCODING_PCM_16BIT,
// buffer size
buffer.length
);
recorder.startRecording();
while(recordablestate) {
try {
// read in up to buffer size
int readBytes = recorder.read(buffer, 0, buffer.length);
// do something with the bytes that are read
} catch (Exception t) {
recordablestate = false;
}
}
// initialize
MediaRecorder mediarecorder = new MediaRecorder();
// set data source
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediarecorder.setOutputFile("/sdcard/someexamplevideo.mp4");
// provide a surface to show the preview in. in this case a VideoView
is used
videoview = (VideoView) findViewById(R.id.videosurface);
SurfaceHolder holder = videoview.getHolder();
mediarecorder.setPreviewDisplay(holder.getSurface());
// prepare
mediarecorder.prepare();
// start recording
mediarecorder.start();
ContentValues content = new ContentValues();
// VERY IMPORTANT! Must reference the absolute path of the data.
content.put(MediaStore.MediaColumns.DATA, "/sdcard/AudioExample.3gpp");
content.put(MediaStore.MediaColumns.TITLE, "AudioRecordExample");
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/amr");
content.put(MediaStore.Audio.Media.ARTIST, "Me");
content.put(MediaStore.Audio.Media.IS_MUSIC, true);
// get the Content Resolver
ContentResolver resolve = getContentResolver();
// insert into the content resolver
Uri uri = resolve.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, content);
// announce to everyone that cares that it was inserted
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { // dosomething swipeRefreshLayout.setRefreshing(false); } });