The Media Player
- Introduction
- You can play audio or video from media files stored in the application's resources (raw
resources), from standalone files in the filesystem, or from a data stream arriving over
a network connection.
- See the
list of supported media formats.
- The MediaPlayer State Diagram
- The edges indicate legal transitions
- MediaPlayer for Audio
- Audio data can be in res/raw, in a file, or streamed from a URL.
- Raw audio data
- Use the MediaPlayer.create method to create the MediaPlayer
object and prepare the stream synchronously.
- Set any listeners you want
- Use start to play.
- File and URL audio data
- Create a MediaPlayer object
- Set any listeners you want
- Set to streaming with setAudioStreamType(AudioManager.STREAM_MUSIC)
- Set the source with setDataSource
- If streaming from the internet, give the app permission to use the internet
with
<uses-permission android:name="android.permission.INTERNET" />
- Prepare with either prepare (synchronous) or prepareAsync
(asynchronous, with a call back to onCompletion)
- Use start to play when the stream is prepared.
- Controls
- start() to start and resume after pause
- pause() to pause
- stop to stop
- release to release resources
- Remember to set permission for internet for streamed music.
- Example
MainActivity.java
activity_main.xml
styles.xml
strings.xml
AndroidManifest.xml
Media/chin_chin_choo.mp3
- VideoView for Video
- Using MediaPlayer for video requires some hard work (see below).
- VideoView can play video data from file, or streamed from a URL.
It's a wrapper around MediaPlayer.
- File data
- Make a VideoView variable point at a VideoView UI element.
- Set any listeners you want
- Use setVideoPath to specify the file
- Upload using the Device Manager to /storage/emulated/0/Movies.
That's what is returned by getExternalStoragePublicDirectory.
- Access to the storage requires permission in the manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />.
- Access to the storage also requires user permission.
Use an ActivityResultLauncher with
ActivityResultContracts.RequestPermission to request it.
- Use start to play
- URL data
- Make a VideoView variable point at a VideoView UI element.
- Set any listeners you want
- Use setVideoURI to set the URL
- Use start to play
- Controls
- start() to start and resume after pause
- pause() to pause
- stopPlayback() to stop playback, which also releases the resources.
- Adding a video controller
- A MediaController provides play, pause, fast-forward, and rewind controls.
- Use setAnchorView to tell the controller about the VideoView.
- Use setMediaController to link the VideoView to the controller.
- Example
MainActivity.java
activity_main.xml
styles.xml
strings.xml
AndroidManifest.xml
Media/mfog.mp4
- MediaPlayer for Video
- Video can be played directly on a SurfaceView.
- It is necessary to establish the surface size, e.g., based on the media size determined
while it is being prepared (and it needs to be visible while being prepared).
- Create a MediaPlayer object
- Set any listeners you want, including setOnVideoSizeChangedListener
- Get a handle on the surface, and set a callback to know when it's ready, and attach it
to the media player.
- Set the source with setDataSource
- Prepare with either prepare or prepareAsync
- Fix size and start to play when the stream is prepared.
- Controls as for audio
- Remember to set permission for internet for streamed videos.
- Remember to set permission for reading external storage for videos on the sdcard.