id | title | brief | sdk | dateupdated | ||||
---|---|---|---|---|---|---|---|---|
1B4B92C7-3A0C-6678-167C-F042E0B3B5B2 |
Play Audio |
This recipe shows how to play audio from a raw resource using the MediaPlayer class. |
|
2018-02-16 |
-
Create a new Xamarin.Android application named PlayAudio.
-
Add a sub folder named raw under Resources.
-
Add a file named test.mp3 under raw.
-
In the Activity, create a class variable for the
MediaPlayer
.
MediaPlayer _player;
- In the
OnCreate
method, callMediaPlayer.Create()
, passing the context and the resource identifier for the mp3.
_player = MediaPlayer.Create(this, Resource.Raw.test);
- Call the
Start
method of theMediaPlayer
.
_player.Start();
When the audio to play is included as a resource, the MediaPlayer.Create
method can be used to set up the data source to
the audio file and prepare the player for playback automatically. If the audio
were at a location such as on the web or an SD card, the application would have
to set the datasource and call Prepare
(or PrepareAsync
) before starting playback.