首页 > 其他分享 >50. Android 多媒体技术——SoundPool播放音效

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

时间:2022-09-20 08:23:24浏览次数:58  
标签:recyclerView 50 SoundPool 音效 new import android data public

50. Android 多媒体技术——SoundPool播放音效
50.1 SoundPool
MediaPlayer虽然也能播放音频,但是它有资源占用量较高,延迟时间较长,不支持多个音频同时播放等缺点。

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

50.2 使用SoundPool
准备音频文件

在这里插入图片描述

新建一个SoundActivity

在这里插入图片描述

修改跳转

在这里插入图片描述

布局

<?xml version="1.0" encoding="utf-8"?>
<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>


调用

适配器 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);
        }
    }
}

 


调用

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();
    }
}

运行

在这里插入图片描述

是有声音的。OK

 

标签:recyclerView,50,SoundPool,音效,new,import,android,data,public
From: https://www.cnblogs.com/55zjc/p/16709777.html

相关文章