valmyUri:Uri=....// initialize Uri herevalmediaPlayer=MediaPlayer().apply{setAudioAttributes(AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).setUsage(AudioAttributes.USAGE_MEDIA).build())setDataSource(applicationContext,myUri)prepare()start()}
Java
UrimyUri=....;// initialize Uri hereMediaPlayermediaPlayer=newMediaPlayer();mediaPlayer.setAudioAttributes(newAudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).setUsage(AudioAttributes.USAGE_MEDIA).build());mediaPlayer.setDataSource(getApplicationContext(),myUri);mediaPlayer.prepare();mediaPlayer.start();
HTTP ストリーミングを使用してリモート URL から再生する方法は次のとおりです。
Kotlin
valurl="http://........"// your URL herevalmediaPlayer=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
Stringurl="http://........";// your URL hereMediaPlayermediaPlayer=newMediaPlayer();mediaPlayer.setAudioAttributes(newAudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).setUsage(AudioAttributes.USAGE_MEDIA).build());mediaPlayer.setDataSource(url);mediaPlayer.prepare();// might take long! (for buffering, etc)mediaPlayer.start();
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-27 UTC。"],[],[],null,["# Get started with Media Player\n\nThis document introduces the basic concepts you should\nbe familiar with before you work with Media Player.\n\nSound and video classes\n-----------------------\n\nThe following classes play sound and video in the Android framework:\n\n- [`MediaPlayer`](/reference/android/media/MediaPlayer): This class is the primary API for playing sound and video.\n- [`AudioManager`](/reference/android/media/AudioManager): This class manages audio sources and audio output on a device.\n\nManifest declarations\n---------------------\n\nBefore you start development on your application using MediaPlayer, make sure\nyour manifest has the appropriate declarations to allow use of related features.\n\n- **Internet Permission:** If you are using MediaPlayer to stream\n network-based content, your application must request network access.\n\n \u003cuses-permission android:name=\"android.permission.INTERNET\" /\u003e\n\n- **Wake Lock Permission:** If your player application needs to keep the\n screen from dimming or the processor from sleeping, or uses the\n [`MediaPlayer.setScreenOnWhilePlaying(boolean)`](/reference/android/media/MediaPlayer#setScreenOnWhilePlaying(boolean)) or\n [`MediaPlayer.setWakeMode(android.content.Context, int)`](/reference/android/media/MediaPlayer#setWakeMode(android.content.Context,%20int)) methods, you\n must request this permission.\n\n \u003cuses-permission android:name=\"android.permission.WAKE_LOCK\" /\u003e\n\nUse the MediaPlayer class\n-------------------------\n\nThe [`MediaPlayer`](/reference/android/media/MediaPlayer) class is n essential component of the media framework.\nAn object of this class can fetch, decode, and play\nboth audio and video with minimal setup. `MediaPlayer` supports several media\nsources, including:\n\n- Local resources\n- Internal URIs, such as one you might obtain from a Content Resolver\n- External URLs (streaming)\n\nFor a list of media formats that Android supports, see the [Supported Media\nFormats](/guide/topics/media/media-formats) page.\n\n### Examples of working with audio sources\n\nHere is an example of how to play audio that's available as a local raw resource\n(saved in your application's `res/raw/` directory): \n\n### Kotlin\n\n var mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1)\n mediaPlayer.start() // no need to call prepare(); create() does that for you\n\n### Java\n\n MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);\n mediaPlayer.start(); // no need to call prepare(); create() does that for you\n\nIn this case, a \"raw\" resource is a file that the system does not try to parse\nin any particular way. However, the content of this resource shouldn't be raw\naudio. It should be a properly encoded and formatted media file in one of the\nsupported formats.\n\nAnd here is how you might play from a URI available locally in the system (that\nyou obtained through a Content Resolver, for instance): \n\n### Kotlin\n\n val myUri: Uri = .... // initialize Uri here\n val mediaPlayer = MediaPlayer().apply {\n setAudioAttributes(\n AudioAttributes.Builder()\n .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)\n .setUsage(AudioAttributes.USAGE_MEDIA)\n .build()\n )\n setDataSource(applicationContext, myUri)\n prepare()\n start()\n }\n\n### Java\n\n Uri myUri = ....; // initialize Uri here\n MediaPlayer mediaPlayer = new MediaPlayer();\n mediaPlayer.setAudioAttributes(\n new AudioAttributes.Builder()\n .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)\n .setUsage(AudioAttributes.USAGE_MEDIA)\n .build()\n );\n mediaPlayer.setDataSource(getApplicationContext(), myUri);\n mediaPlayer.prepare();\n mediaPlayer.start();\n\nPlaying from a remote URL using HTTP streaming looks like this: \n\n### Kotlin\n\n val url = \"http://........\" // your URL here\n val mediaPlayer = MediaPlayer().apply {\n setAudioAttributes(\n AudioAttributes.Builder()\n .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)\n .setUsage(AudioAttributes.USAGE_MEDIA)\n .build()\n )\n setDataSource(url)\n prepare() // might take long! (for buffering, etc)\n start()\n }\n\n### Java\n\n String url = \"http://........\"; // your URL here\n MediaPlayer mediaPlayer = new MediaPlayer();\n mediaPlayer.setAudioAttributes(\n new AudioAttributes.Builder()\n .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)\n .setUsage(AudioAttributes.USAGE_MEDIA)\n .build()\n );\n mediaPlayer.setDataSource(url);\n mediaPlayer.prepare(); // might take long! (for buffering, etc)\n mediaPlayer.start();\n\n| **Note:** If you're passing a URL to stream an online media file, the file must be capable of progressive download.\n| **Caution:** You must either catch or pass [`IllegalArgumentException`](/reference/java/lang/IllegalArgumentException) and [`IOException`](/reference/java/io/IOException) when using [`setDataSource()`](/reference/android/media/MediaPlayer#setDataSource(android.content.Context,%20android.net.Uri)) because the file you are referencing might not exist.\n\nUse Asynchronous preparation to improve performance\n---------------------------------------------------\n\nKeep performance in mind when you use [`MediaPlayer`](/reference/android/media/MediaPlayer). For example, the call\nto [`prepare()`](/reference/android/media/MediaPlayer#prepare()) can take a long time to execute, because it might involve\nfetching and decoding media data. So, like any method that may take a long time\nto execute, **never call it from your application's UI thread**. Doing so causes\nthe UI to stop responding until the method returns, which is a bad user\nexperience and can cause an ANR (Application Not Responding) error.\n\nTo avoid hanging your UI thread, spawn another thread to prepare the\n[`MediaPlayer`](/reference/android/media/MediaPlayer) and notify the main thread when done. The framework supplies\na convenient way to accomplish the [`prepareAsync()`](/reference/android/media/MediaPlayer#prepareAsync()) method for doing this\ntask. This method starts preparing the media in the background and returns\nimmediately. When the media is done preparing, the [`onPrepared()`](/reference/android/media/MediaPlayer.OnPreparedListener#onPrepared(android.media.MediaPlayer)) method\nof the [`MediaPlayer.OnPreparedListener`](/reference/android/media/MediaPlayer.OnPreparedListener), configured through\n[`setOnPreparedListener()`](/reference/android/media/MediaPlayer#setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)) is called.\n\nLearn more\n----------\n\nJetpack Media3 is the recommended solution for media playback in your app. [Read\nmore](/media/media3) about it.\n\nThese pages cover topics relating to recording, storing, and playing back audio\nand video:\n\n- [Supported Media Formats](/guide/topics/media/media-formats)\n- [MediaRecorder](/guide/topics/media/mediarecorder)\n- [Data Storage](/guide/topics/data/data-storage)"]]