安卓开发学习笔记(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);//播放所选音频
            }
        });
    }
}

问题:再导入音频文件后,音频文件出现问号,不能使用这个资源

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