Audio playback in Android (MediaPlayer and Soundpool)

Source: Internet
Author: User

Playback of audio and video in Android the first thing we think of is the MediaPlayer class, which provides methods for playing, pausing, stopping, and repeating playback. This class is located under the Android.media package, see the API documentation. In fact, in addition to this class there is a music playback class that is Soundpool, these two classes have different analysis for everyone to understand

MediaPlayer:

This class is suitable for playing large files, which should be stored on an SD card rather than in a resource file, and this class can only play one audio file at a time.

This type of usage is as follows:

1. Play from the resource file

MediaPlayer player = new mediaplayer.create (this,r.raw.test);

Player.stare ();

2. Playback from File system

MediaPlayer player = new MediaPlayer ();

String Path = "/sdcard/test.mp3";

Player.setdatasource (path);

Player.prepare ();

Player.start ();

3. Play from the network

(1) by means of a URI:

String path= "Http://**************.mp3"; Here's a network address for a song.

Uri uri = uri.parse (path);

MediaPlayer player = new mediaplayer.create (This,uri);

Player.start ();

(2) How to set up the data source:

MediaPlayer player = new mediaplayer.create ();

String path= "Http://**************.mp3"; Here's a network address for a song.

Player.setdatasource (path);

Player.prepare ();

Player.start ();

Soundpool:

This type of feature is low-latency playback, suitable for playing real-time audio to play multiple sounds at the same time, such as the explosion of bombs in the game and other small resources files, this kind of audio is more suitable to be placed in the resource folder Res/raw and the program into the APK file.

Use the following:

Soundpool Soundpool = new Soundpool (4, Audiomanager.stream_music, 100);

Hashmap<integer, integer> soundpoolmap = new Hashmap<integer, integer> ();

Soundpoolmap.put (1, Soundpool.load (this, r.raw.dingdong1, 1));

Soundpoolmap.put (2, Soundpool.load (this, r.raw.dingdong2, 2));

public void PlaySound (int sound, int loop) {

Audiomanager mgr = (audiomanager) this.getsystemservice (Context.audio_service);

float streamvolumecurrent = Mgr.getstreamvolume (Audiomanager.stream_music);

float Streamvolumemax = Mgr.getstreammaxvolume (Audiomanager.stream_music);

float volume = Streamvolumecurrent/streamvolumemax;

Soundpool.play (Soundpoolmap.get (sound), volume, volume, 1, loop, 1f);

Parameters: 1, Map value 2, current volume 3, Maximum volume 4, priority 5, replay count 6, playback speed

}

This.playsound (1, 0);

Audio playback in Android (MediaPlayer and Soundpool)

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.