Android簡單的利用SoundPool進行播放鈴聲的執行個體代碼

來源:互聯網
上載者:User

標籤:

 

MainActivity.java

package com.example.pengdonglin.soundpool_demo;import android.annotation.SuppressLint;import android.media.AudioManager;import android.media.SoundPool;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.util.SparseIntArray;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import java.util.HashMap;public class MainActivity extends ActionBarActivity implements View.OnClickListener {    Button btn1, btn2, btn3;    SoundPool soundpool;    //HashMap<Integer, Integer> soundMap = new HashMap<Integer, Integer>();    SparseIntArray soundMap = new SparseIntArray();    @Override    protected void onCreate(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 = new SoundPool(10, 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 boolean onCreateOptionsMenu(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 boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    @Override    public void onClick(View v) {        switch(v.getId())        {            case R.id.button:                soundpool.play(soundMap.get(1), 1, 1, 0, 0, 1);                break;            case R.id.button2:                soundpool.play(soundMap.get(2), 1, 1, 0, 0, 1);                break;            case R.id.button3:                soundpool.play(soundMap.get(3), 1, 1, 0, 0, 1);                break;        }    }}

 

activity_main.xml

<RelativeLayout xmlns: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">    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/textView" />    <Button        android: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" />    <Button        android: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" />    <Button        android: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"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.pengdonglin.soundpool_demo" >    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

 

Android簡單的利用SoundPool進行播放鈴聲的執行個體代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.