MediaPlayer play audio files from different sources:
One, play the application's resource file
1. Call MediaPlayer's Create (context context, int resid) method to load the specified resource file.
2. Call MediaPlayer's start (), pause (), stop () and other methods to control playback.
Note: Audio resource files are typically placed in the/res/raw directory of Android apps.
Second, play the application's original resource file
1. Call the context's Getassets () method to get the app's Assetmanager.
2. Call the OPENFD (String name) method of the Assetmanager object to open the specified native resource, which returns a Assetfiledescriptor object.
3. Call Assetfiledescriptor's Getfiledescriptor (), Getstartoffset (), and GetLength () methods to get the audio file's filedescriptor, start position, length, and so on.
4. Create the MediaPlayer object and call the MediaPlayer object's Setdataresource (FileDescriptor fd, long offset, long length) method to load the audio resource.
5. Call the Prepare () method of the MediaPlayer object to prepare the audio.
6. Call MediaPlayer's start (), pause (), stop () and other methods to control playback.
Note: Although MediaPlayer provides the Setdatasource (FileDescriptor fd) method to mount the specified audio resource, this method seems to have a problem when actually used: regardless of the program call OPENFF (String name) method
When specifying which primitive resource to open, MediaPlayer will always play the first original audio resource.
Third, play the audio file on the external memory.
1. Create the MediaPlayer object and call the Setdatasource (String Path) method of the MediaPlayer object to mount the specified audio file.
2. Call the Prepare () method of the MediaPlayer object to prepare the audio.
3. Call MediaPlayer's start (), Stop () and other methods to control playback.
Four, play audio files from the network
There are two ways to play audio files from the network:
1. Use the MediaPlayer static create (context context, Uri Uri) method directly;
2. Call MediaPlayer's Setdatasource (context context URI Uri) to mount the corresponding audio file for the specified Uri.
To play the audio file from the network in the second way:
1. Create a URI object based on the location of the audio file on your network.
2. Create the MediaPlayer object and call the MediaPlayer object's Setdatasource (context context, Uri Uri) method to mount the corresponding audio file for the URI.
3. Call the Prepare () method of the MediaPlayer object to prepare the audio.
4. Call MediaPlayer's start (), pause (), stop () and other methods to control playback.
Use MediaPlayer to play audio-----Two