Android development: Audio Playback

Source: Internet
Author: User

 

Android development: Audio Playback

/*

* Android development: Audio Playback

* Beijing Android Club group: 167839253

* Created on: 2011-8-23

* Author: blueeagle

* Email: liujiaxiang@gmail.com

*/

 

 

Audio Playback

There are two ways to play audio on the Android platform: soundpool and mediaplayer. Soundpool is suitable for short promotions but high response speed requirements. However, mediaplay is suitable for long periods of time but does not have high requirements.

Audio files are generally stored in the res raw directory.

Soundpool Description: The soundpool initialization process is asynchronous, that is, when the soundpool is initialized, the system automatically starts a background thread to complete initialization. Therefore, it does not affect the running of other programs on the front-end. However, there is also a problem. After the initialization operation is called, it cannot be played immediately. It takes some time to wait. Otherwise, an error may occur. In addition, soundpool can play multiple audio files at the same time, but mediaplayer can only play one video at the agreed time.

 

The source code is as follows:

/** Android development of audio playback * mymeidatest01.java * created on: 2011-8-23 * Author: blueeagle * Email: liujiaxiang@gmail.com */package COM. blueeagle; import Java. util. hashmap; import android. app. activity; import android. content. context; import android. media. audiomanager; import android. media. mediaplayer; import android. media. soundpool; import android. OS. bundle; import android. view. view; import android. widget. but Ton; import android. widget. textview; public class mymeidatest01 extends activity {button button1; button button2; button button3; button button4; textview mytextview; mediaplayer mymediaplayer; soundpool mysoundpool; hashmap <integer, integer> soundpoolmap; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); initsounds (); ini Tui ();} public void playsound (INT sound, int loop) {// method of playing the soundpool 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; mysoundpool. play (soundpoolmap. get (sound), volum E, volume, 1, loop, 1f) ;}private void initui () {// todo auto-generated method stub mytextview = (textview) findviewbyid (R. id. mytextview); button1 = (button) findviewbyid (R. id. button1); button2 = (button) findviewbyid (R. id. button2); button3 = (button) findviewbyid (R. id. button3); button4 = (button) findviewbyid (R. id. button4); button1.setonclicklistener (New button. onclicklistener () {@ override public Vo Id onclick (view v) {// todo auto-generated method stub mytextview. settext ("Play Sound using mediaplayer"); If (! Mymediaplayer. isplaying () mymediaplayer. start () ;}}); button2.setonclicklistener (New button. onclicklistener () {@ override public void onclick (view v) {// todo auto-generated method stub mytextview. settext ("Pause mediaplayer playback sound"); If (mymediaplayer. isplaying () mymediaplayer. pause () ;}}); button3.setonclicklistener (New button. onclicklistener () {@ override public void onclick (view v) {// todo auto-gen Erated method stub mytextview. settext ("Playing sound with soundpool"); playsound () ;}}); button4.setonclicklistener (New button. onclicklistener () {@ override public void onclick (view v) {// todo auto-generated method stub mytextview. settext ("Pause soundpool Playing Sound"); mysoundpool. pause (1) ;}});} private void initsounds () {// todo auto-generated method stub mymediaplayer = mediaplayer. create (this, R. raw. music); m Ysoundpool = new soundpool (4, audiomanager. stream_music, 100); soundpoolmap = new hashmap <integer, integer> (); soundpoolmap. put (1, mysoundpool. load (this, R. raw. kick, 1); // initialize the sound operation. When using soundpool, the sound is generally put into a hashmap to facilitate sound management and operation. }}

The XML layout file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: id = "@ + ID/mytextview" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "no sound played"/> <button Android: id = "@ + ID/button1" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "play audio with media"/> <button Android: id = "@ + ID/button2" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Pause media playback sound"/> <button Android: id = "@ + ID/button3" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Play Sound using soundpool"/> <button Android: id = "@ + ID/button4" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Pause soundpool playback sound"/> </linearlayout>

Related Article

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.