• Android学习笔记 50. Android 多媒体技术——SoundPool播放音效


    Android学习笔记

    Android 多媒体技术

    50. Android 多媒体技术——SoundPool播放音效

    50.1 SoundPool

    MediaPlayer虽然也能播放音频,但是它有资源占用量较高,延迟时间较长,不支持多个音频同时播放等缺点。

    SoundPool一般用来播放密集、急促而短暂的音效。

    50.2 使用SoundPool

    准备音频文件

    在这里插入图片描述

    新建一个SoundActivity

    在这里插入图片描述

    修改跳转

    在这里插入图片描述

    布局

    
    <androidx.constraintlayout.widget.ConstraintLayout 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=".SoundActivity">
        
        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recyclerView"
            />
    
    androidx.constraintlayout.widget.ConstraintLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    调用

    适配器 MyAdapter.java

    package com.dingjiaxiong.mymediarecorder;
    
    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    
    import java.util.List;
    
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnClickListener {
    
        private final List<SoundActivity.Sound> data;
        private final Context context;
        private final RecyclerView recyclerView;
        private OnItemClickListener listener;
    
        public MyAdapter(List<SoundActivity.Sound> data, RecyclerView recyclerView, Context context) {
            this.data = data;
            this.recyclerView = recyclerView;
            this.context = context;
        }
    
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            TextView textView = new TextView(context);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = 18;
            layoutParams.leftMargin = 18;
            textView.setLayoutParams(layoutParams);
            textView.setOnClickListener(this);
            return new MyViewHolder(textView);
        }
    
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            ((TextView) holder.itemView).setText(data.get(position).getName());
        }
    
        @Override
        public int getItemCount() {
            return data.size();
        }
    
        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClick(recyclerView.getChildAdapterPosition(v));
            }
        }
    
        public void setOnItemClickListener(OnItemClickListener listener) {
            this.listener = listener;
        }
    
        interface OnItemClickListener {
            void onItemClick(int position);
        }
    
        class MyViewHolder extends RecyclerView.ViewHolder {
    
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    调用

    package com.dingjiaxiong.mymediarecorder;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import android.media.SoundPool;
    import android.os.Bundle;
    import android.view.View;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class SoundActivity extends AppCompatActivity implements MyAdapter.OnItemClickListener {
    
        private SoundPool soundPool;
    
        static class Sound {
            String name;
            int soundId;
    
            public Sound(String name, int soundId) {
                this.name = name;
                this.soundId = soundId;
            }
    
            public int getSoundId() {
                return soundId;
            }
    
            public String getName() {
                return name;
            }
        }
    
        List<Sound> data;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sound);
            RecyclerView recyclerView = findViewById(R.id.recyclerView);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
    
            soundPool = new SoundPool.Builder().setMaxStreams(6).build();
    
            data = new ArrayList<>();
            data.add(new Sound("a4", soundPool.load(this, R.raw.a4, 1)));
            data.add(new Sound("a5", soundPool.load(this, R.raw.a5, 1)));
            data.add(new Sound("a6", soundPool.load(this, R.raw.a6, 1)));
            data.add(new Sound("a7", soundPool.load(this, R.raw.a7, 1)));
            data.add(new Sound("a8", soundPool.load(this, R.raw.a8, 1)));
            data.add(new Sound("a9", soundPool.load(this, R.raw.a9, 1)));
            MyAdapter myAdapter = new MyAdapter(data, recyclerView, this);
            myAdapter.setOnItemClickListener(this);
            recyclerView.setAdapter(myAdapter);
    
        }
    
        @Override
        public void onItemClick(int position) {
            Sound sound = data.get(position);
            soundPool.play(sound.getSoundId(),
                    1.0f, 1.0f, 1, 0, 1.0f);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            for (Sound datum : data) {
                soundPool.unload(datum.getSoundId());
            }
            soundPool.release();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    运行

    在这里插入图片描述

    是有声音的。OK

  • 相关阅读:
    gcc编译升级&&解决GLIBC_2.18 not found
    第12章_数据库其它调优策略
    自动化脚本一键安装 jdk,hadoop,hive
    java培训技术数据绑定流程原理
    CMIP6数据处理
    获取微信openid和基本信息的总结
    array_map与array_walk的用法与区别详解
    大数据概述、前世今生、处理流程、学习路线、开发工具详解
    vue3中使用better-scroll
    STM32——04-初识STM32单片机
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126314493