[ Web Proxy ]
URL:
Viewing: https://developer.android.com/media/platform/mediaplayer/basics [Back]  [Original]

Get started with Media Player  |  Android media  |  Android Developers Skip to main content
Essentials Design & Plan Develop Google Play Blog
Search:
Android Studio
Android Developers [Android Developers]

Get started with Media Player Stay organized with collections Save and categorize content based on your preferences.

This document introduces the basic concepts you should be familiar with before you work with Media Player.

Sound and video classes

The following classes play sound and video in the Android framework:

Manifest declarations

Before you start development on your application using MediaPlayer, make sure your manifest has the appropriate declarations to allow use of related features.

Use the MediaPlayer class

The MediaPlayer class is n essential component of the media framework. An object of this class can fetch, decode, and play both audio and video with minimal setup. MediaPlayer supports several media sources, including:

For a list of media formats that Android supports, see the Supported Media Formats page.

Examples of working with audio sources

Here is an example of how to play audio that's available as a local raw resource (saved in your application's res/raw/ directory):

Kotlin

var mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1)
mediaPlayer.start() // no need to call prepare(); create() does that for you

Java

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you

In this case, a "raw" resource is a file that the system does not try to parse in any particular way. However, the content of this resource shouldn't be raw audio. It should be a properly encoded and formatted media file in one of the supported formats.

And here is how you might play from a URI available locally in the system (that you obtained through a Content Resolver, for instance):

Kotlin

val myUri: Uri = .... // initialize Uri here
val mediaPlayer = MediaPlayer().apply {
    setAudioAttributes(
        AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .build()
    )
    setDataSource(applicationContext, myUri)
    prepare()
    start()
}

Java

Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioAttributes(
    new AudioAttributes.Builder()
        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
        .setUsage(AudioAttributes.USAGE_MEDIA)
        .build()
);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();

Playing from a remote URL using HTTP streaming looks like this:

Kotlin

val url = "http://........" // your URL here
val mediaPlayer = MediaPlayer().apply {
    setAudioAttributes(
        AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .build()
    )
    setDataSource(url)
    prepare() // might take long! (for buffering, etc)
    start()
}

Java

String url = "http://........"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioAttributes(
    new AudioAttributes.Builder()
        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
        .setUsage(AudioAttributes.USAGE_MEDIA)
        .build()
);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
Note: If you're passing a URL to stream an online media file, the file must be capable of progressive download.Caution: You must either catch or pass IllegalArgumentException and IOException when using setDataSource() because the file you are referencing might not exist.

Use Asynchronous preparation to improve performance

Keep performance in mind when you use MediaPlayer. For example, the call to prepare() can take a long time to execute, because it might involve fetching and decoding media data. So, like any method that may take a long time to execute, never call it from your application's UI thread. Doing so causes the UI to stop responding until the method returns, which is a bad user experience and can cause an ANR (Application Not Responding) error.

To avoid hanging your UI thread, spawn another thread to prepare the MediaPlayer and notify the main thread when done. The framework supplies a convenient way to accomplish the prepareAsync() method for doing this task. This method starts preparing the media in the background and returns immediately. When the media is done preparing, the onPrepared() method of the MediaPlayer.OnPreparedListener, configured through setOnPreparedListener() is called.

Learn more

Jetpack Media3 is the recommended solution for media playback in your app. Read more about it.

These pages cover topics relating to recording, storing, and playing back audio and video:

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2025-03-19 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-03-19 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page