Sound轻量级播放声音资源首选(只能加载本地声音,网络声音使用MediaPlayer)

核心:注意,SoundPool 调用play方法才会有声音

private SoundPool soundPool=new SoundPool.Builder().setMaxStreams(6).build();
 int load = soundPool.load("本地音频路径", 1);
 soundPool.play(load,  1.0f, 1.0f, 1, 0, 1.0f);

源码

    public int load(String path, int priority) {
    
    
        int id = 0;
        try {
    
    
            File f = new File(path);
            ParcelFileDescriptor fd = ParcelFileDescriptor.open(f,
                    ParcelFileDescriptor.MODE_READ_ONLY);
            if (fd != null) {
    
    
                id = _load(fd.getFileDescriptor(), 0, f.length(), priority);
                fd.close();
            }
        } catch (java.io.IOException e) {
    
    
            Log.e(TAG, "error loading " + path);
        }
        return id;
    }

package com.enjoy.mediademo;

import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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

layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MediaPlayer的基本使用:点击播放,再点击停止播放,再点击重新播放

 private MediaPlayer mediaPlayer= new MediaPlayer();;

 if (animations.isRunning()&& mediaPlayer.isPlaying()) {
    
    
                        animations.stop();
                        mediaPlayer.stop();


                    } else {
    
    
                        animations.start();
                        String url = data.get(position).getSoundMsgDto().getUrl();
                        Log.e("wy", "onClick url: "+url );
                        if(url.contains("http")){
    
    
//                             load = soundPool.load(url, 1);
                            try {
    
    
                                mediaPlayer.reset();
                                mediaPlayer.setDataSource(url);
                                mediaPlayer.prepare();
                                mediaPlayer.start();
                            } catch (Exception e) {
    
    
                                e.printStackTrace();
                            }
                        }else {
    
    
//                             load = soundPool.load("https:" + url, 1);
                            try {
    
    
                                mediaPlayer.reset();
                                mediaPlayer.setDataSource(url);
                                mediaPlayer.prepare();
                                mediaPlayer.start();
                            } catch (Exception e) {
    
    
                                e.printStackTrace();
                            }
                        }
                    }

猜你喜欢

转载自blog.csdn.net/xiyangyang8110/article/details/118650576#comments_22689059
今日推荐