-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Video and Audio Playback and Recording
In Android apps, there are often cases where we need to play audio or video files. Checking the Supported Media Formats we can see that several audio formats (MP3, AAC, FLAC, Vorbis) and several video formats (H.264, MPEG-4) are playable by default. Let's take a look at how to play media within an app using MediaPlayer and VideoView. We will also look at how to record or capture media with the MediaRecorder.
In this section we will take a look at how to play audio content using the MediaPlayer and capture audio with the MediaRecorder.
To play local audio in the supported formats, first we should put the local audio file into the res/raw
folder. For example put this sample_audio.mp3 into res/raw/sample_audio.mp3
.
Now we can use the MediaPlayer in order to playback any local files:
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.sample_audio);
mediaPlayer.start();
On call to start()
method, the music will start playing from the beginning. If this method is called again after the pause()
method, the music would start playing from where it left off and not from the beginning. To play back audio from a local file path, simply do:
Uri myUri = "...."; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
// or just mediaPlayer.setDataSource(mFileName);
mediaPlayer.prepare(); // must call prepare first
mediaPlayer.start(); // then start
We can release the resources taken by the player by calling release
to free up system resources:
mediaPlayer.release();
mediaPlayer = null;
MediaPlayer has many key methods to control playback such as:
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.sample_audio);
mediaPlayer.start(); // start the audio from last location
mediaPlayer.pause(); // pause audio
mediaPlayer.reset(); // reset audio to beginning of track
mediaPlayer.isPlaying(); // returns true/false indicating the song is playing
mediaPlayer.seekTo(100); // move song to that particular millisecond
mediaPlayer.getCurrentDuration(); // current position of song in milliseconds
mediaPlayer.getDuration(); // total time duration of song in milliseconds
Check the MediaPlayer docs for a full list of methods. These are the basics for playing audio but check out this androidhive and this tutorial if you want to understand how to hook up a graphical interface for managing the audio playback.
If you are using MediaPlayer to stream network-based content, your application must request network access in the manifest:
<uses-permission android:name="android.permission.INTERNET" />
Now we can stream remote audio files in the supported formats with:
String url = "https://dl.dropboxusercontent.com/u/10281242/sample_audio.mp3";
final MediaPlayer mediaPlayer = new MediaPlayer();
// Set type to streaming
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
// Listen for if the audio file can't be prepared
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// ... react appropriately ...
// The MediaPlayer has moved to the Error state, must be reset!
return false;
}
});
// Attach to when audio file is prepared for playing
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
// Set the data source to the remote URL
mediaPlayer.setDataSource(url);
// Trigger an async preparation which will file listener when completed
mediaPlayer.prepareAsync();
With that, the remote file will start streaming once preparation is completed and loading the audio file won't block the UI thread. For more details, see the official MediaPlayer playback guide.
You can record audio using the MediaRecorder APIs if supported by the device hardware.
First, let's add the correct permissions to our AndroidManifest.xml
:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Recording audio is as simple as starting and stopping the MediaRecorder
:
// File path of recorded audio
private String mFileName;
// Verify that the device has a mic first
PackageManager pmanager = this.getPackageManager();
if (pmanager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) {
// Set the file location for the audio
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
// Create the recorder
mediaRecorder = new MediaRecorder();
// Set the audio format and encoder
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// Setup the output location
mediaRecorder.setOutputFile(mFileName);
// Start the recording
mediaRecorder.prepare();
mediaRecorder.start();
} else { // no mic on device
Toast.makeText(this, "This device doesn't have a mic!", Toast.LENGTH_LONG).show();
}
When you want to stop capturing audio, simply notify the recorder:
// Stop the recording of the audio
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
If you wanted to now playback the recorded audio:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(mFileName);
mediaPlayer.prepare(); // must call prepare first
mediaPlayer.start(); // then start
If you wanted to add this audio to the media library on the phone:
// Setup values for the media file
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio file");
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();
// Construct uris
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
// Trigger broadcast to add
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
For more details, check out this tutorialspoint article and the official Audio Capture guide.
In this section we will take a look at how to play video content using the VideoView and capture video with the MediaRecorder.
Playing local video in a supported format can be done using the VideoView
. First, setup the VideoView
in your layout:
<VideoView
android:id="@+id/video_view"
android:layout_width="320px"
android:layout_height="240px" />
Next, we can store local files such as small_video.mp4 in res/raw/small_video.mp4
and than play the video in the view with:
VideoView mVideoView = (VideoView) findViewById(R.id.video_view);
mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.small_video));
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.start();
See this edumobile tutorial for a more detailed look at using VideoView.
To play back remote video in a supported format, we can still use the VideoView
. First, setup the correct permissions in the Android Manifest.xml
:
<uses-permission android:name="android.permission.INTERNET" />
Now, we can play remote video with:
final VideoView mVideoView = (VideoView) findViewById(R.id.video_view);
mVideoView.setVideoPath("http://techslides.com/demos/sample-videos/small.mp4");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
}
});
You can see a more complete example of remote streaming with this androidbegin tutorial.
Capturing video can be done using intents to capture video using the camera. First, let's setup the necessary permissions in AndroidManifest.xml
:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Next, we can trigger recording video by starting an intent triggering a video capture action:
private static final int VIDEO_CAPTURE = 101;
Uri videoUri;
public void startRecordingVideo() {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
File mediaFile = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/myvideo.mp4");
videoUri = Uri.fromFile(mediaFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
startActivityForResult(intent, VIDEO_CAPTURE);
} else {
Toast.makeText(this, "No camera on device", Toast.LENGTH_LONG).show();
}
}
and then we need to manage the onActivityResult
for when the video has been captured:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VIDEO_CAPTURE) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Video has been saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
playbackRecordedVideo();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Video recording cancelled.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Failed to record video", Toast.LENGTH_LONG).show();
}
}
}
public void playbackRecordedVideo() {
VideoView mVideoView = (VideoView) findViewById(R.id.video_view);
mVideoView.setVideoURI(videoUri);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.start();
}
For a more detailed look, check out the techtopia tutorial on video recording.
There are a few ways of video playback on the android device. Most of which include downloading the content to the device for playback. If you want to stream a video from a network hosted source. the youtube api gives you the ability to do so using their provided 3gp stream. The provided YouTube Android Player API allows you to do so with very little code. Check out this truiton tutorial to learn more about playing video with YouTube SDK.
- http://mrbool.com/how-to-play-video-formats-in-android-using-videoview/28299
- http://developer.android.com/guide/topics/media/mediaplayer.html
- http://developer.android.com/training/managing-audio/index.html
- http://developer.android.com/guide/appendix/media-formats.html
- https://developers.google.com/youtube/android/player/
- http://www.vogella.com/tutorials/AndroidMedia/article.html
- http://developer.android.com/guide/topics/media/audio-capture.html
- http://www.tutorialspoint.com/android/android_audio_capture.htm
- http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/
- http://www.edumobile.org/android/android-beginner-tutorials/how-to-play-a-video-file/
- http://www.androidbegin.com/tutorial/android-video-streaming-videoview-tutorial/
- http://developer.android.com/reference/android/media/MediaPlayer.html
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.