安卓開發學習筆記(17)音頻--使用SoundPool播放(實例:模擬手機選擇鈴聲)

佈局界面:使用LisetView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <ListView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/listView"
       android:entries="@array/bellname">

   </ListView>

</RelativeLayout>

設置鈴聲的列表

bellname.xml在values下

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="bellname">
        <item>布穀鳥叫聲</item>
        <item>風鈴聲</item>
        <item>門鈴聲</item>
        <item>電話聲</item>
        <item>鳥叫聲</item>
        <item>水流聲</item>
        <item>公雞叫聲</item>
    </array>
</resources>

實現鬧鐘鈴聲

MainActivity.java

package com.example.firstandroid;

import androidx.annotation.RequiresApi;
import                                                                                                                                                                                                                                                                                        androidx.appcompat.app.AppCompatActivity;

import android.media.AudioAttributes;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView=findViewById(R.id.listView);
        /*********創建SoundPool對象,並且設置音頻相關屬性***********/
        AudioAttributes attr =new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)//設置音效使用場景
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)//設置音效類型
                .build();
        final SoundPool soundPool=new SoundPool.Builder()
                .setAudioAttributes(attr)//設置音效池的屬性
                .setMaxStreams(10)//設置最多可容納10個音頻流
                .build();
        /*************將要播放的音頻保存到HashMap中****************/
        final HashMap<Integer,Integer>soundmap=new HashMap<Integer, Integer>();//創建一個HashMap對象
        soundmap.put(0,soundPool.load(this,R.raw.U1,1));
        soundmap.put(1,soundPool.load(this,R.raw.U2,1));
        soundmap.put(2,soundPool.load(this,R.raw.U3,1));
        soundmap.put(3,soundPool.load(this,R.raw.U4,1));
        soundmap.put(4,soundPool.load(this,R.raw.U5,1));
        soundmap.put(5,soundPool.load(this,R.raw.U6,1));
        soundmap.put(6,soundPool.load(this,R.raw.U7,1));
        /*********************播放音頻**************************/
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                soundPool.play(soundmap.get(position),1,1,0,0,1);//播放所選音頻
            }
        });
    }
}

問題:再導入音頻文件後,音頻文件出現問號,不能使用這個資源

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章