Use MediaPlayer to play audio-----Two

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.