Android Simple example code for playing ringtones with Soundpool

Source: Internet
Author: User

Mainactivity.java

 PackageCom.example.pengdonglin.soundpool_demo;ImportAndroid.annotation.SuppressLint;ImportAndroid.media.AudioManager;ImportAndroid.media.SoundPool;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.util.SparseIntArray;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportJava.util.HashMap; Public classMainactivityextendsActionbaractivityImplementsView.onclicklistener {Button btn1, btn2, btn3;    Soundpool Soundpool; //Hashmap<integer, integer> soundmap = new Hashmap<integer, integer> ();Sparseintarray Soundmap =NewSparseintarray (); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); BTN1=(Button) Findviewbyid (R.id.button); BTN2=(Button) Findviewbyid (R.id.button2); Btn3=(Button) Findviewbyid (R.id.button3); Soundpool=NewSoundpool (Audiomanager.stream_music, 5); Soundmap.put (1, Soundpool.load ( This, R.raw.carbon, 1)); Soundmap.put (2, Soundpool.load ( This, R.raw.cesium, 1)); Soundmap.put (3, Soundpool.load ( This, R.raw.fermium, 1)); Btn1.setonclicklistener ( This); Btn2.setonclicklistener ( This); Btn3.setonclicklistener ( This); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); //noinspection simplifiableifstatement        if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.button:soundpool.play (Soundmap.get (1), 1, 1, 0, 0, 1);  Break;  CaseR.id.button2:soundpool.play (Soundmap.get (2), 1, 1, 0, 0, 1);  Break;  CaseR.id.button3:soundpool.play (Soundmap.get (3), 1, 1, 0, 0, 1);  Break; }    }}

Activity_main.xml

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Android:paddingbottom= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity ">    <TextViewAndroid:text= "@string/hello_world"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/textview" />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "New button"Android:id= "@+id/button"Android:layout_below= "@+id/textview"Android:layout_alignparentleft= "true"Android:layout_alignparentstart= "true"Android:layout_margintop= "46DP" />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "New button"Android:id= "@+id/button2"Android:layout_below= "@+id/button"Android:layout_alignparentleft= "true"Android:layout_alignparentstart= "true"Android:layout_margintop= "42DP" />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "New button"Android:id= "@+id/button3"Android:layout_below= "@+id/button2"android:layout_centervertical= "true"Android:layout_alignparentleft= "true"Android:layout_alignparentstart= "true"Android:layout_margintop= "42DP"/></Relativelayout>

Androidmanifest.xml

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.pengdonglin.soundpool_demo" >    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    </Application></Manifest>

Android Simple example code for playing ringtones with 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.