在 android 的 mediapalyer 中同时播放 2 个音频

2 audios playing at the same time in mediapalyer in android

请帮忙!!

我在回收站视图中使用媒体播放器,问题是当单击不同项目的播放按钮时,它们会同时播放。我怎样才能停止以前的音频并开始新的音频?

适配器代码如下:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>  {
    final MediaPlayer mediaPlayer = new MediaPlayer();


 public void onBindViewHolder(@NonNull MyAdapter.ViewHolder viewHolder, int position) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(item.get(position).getAudio());
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

viewHolder.play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer.isPlaying()) {
                        mediaPlayer.pause();
                        viewHolder.play.setImageResource(R.drawable.play);

                } else {
                    mediaPlayer.start();
                    viewHolder.play.setImageResource(R.drawable.pause);
                }

我想一个答案是使用标志。但是不知道怎么实现。

有多个音频同时播放,因为 MediaPlayer 对象有多个实例,因为它在每次调用 onBindViewHolder() 时实例化;即 RecyclerView 的每一行都有一个唯一的对象。

要解决此问题,您只需要为所有音频使用一个对象,因此传输

final MediaPlayer mediaPlayer = new MediaPlayer();

成为适配器的直接字段 class。

更新:

现在改变播放按钮点击的逻辑

public void onBindViewHolder(@NonNull MyAdapter.ViewHolder viewHolder, int position) {

    viewHolder.play.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View view) {
            
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                    viewHolder.play.setImageResource(R.drawable.play);
                } 
                
                try {
                    mediaPlayer.reset();
mediaPlayer.setDataSource(item.get(position).getAudio());
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                viewHolder.play.setImageResource(R.drawable.pause);
                            
            }
        }

}

更新 2:

尝试将播放按钮代码传输到 ViewHolder 而不是 onBindViewHolder,而不是 position,使用 getAbsoluteAdapterPosition()

public static class ViewHolder extends RecyclerView.ViewHolder {

    //.....
    
    public ViewHolder(View itemView) {
        super(itemView);
        play = itemView.findViewById(...);
        
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            
                // ..... Add the mediaPlayer code
                                
            }
        }
    }

}

这是 MediaPlayer() 的双重实例化问题 class。你要做的就是像这样制作一个对象class:

import android.media.MediaPlayer
import android.net.Uri
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

object MusicObject {

    private lateinit var mediaPlayer: MediaPlayer
    private val executor: ExecutorService = Executors.newCachedThreadPool()
    private var flag = 0
    private var musicName = ""
    private var musicID = 1
    private var musicUri: Uri? = null

    // Function to play music
    fun playMusic(mp: MediaPlayer,name: String,ID: Int,Uri: Uri) {
        if (flag > 0) {
            pauseMusic()
        }
        flag++
        musicName = name
        musicID = ID
        musicUri = Uri
        mediaPlayer = mp
        executor.execute {
            mediaPlayer.start()
        }
    }

    // Function to play currently playing music again
    fun playMusicAgain() {
        executor.execute {
            mediaPlayer.start()
        }
    }

    // Function to pause music
    fun pauseMusic() {
        mediaPlayer.pause()
    }

    // Function to stop music
    fun stopMusic() {
        if (mediaPlayer.isPlaying) {
            mediaPlayer.stop()
        }
    }

    fun getMediaPlayer(): MediaPlayer {
        return mediaPlayer
    }
}

并调用此 class 来播放和暂停音乐。

播放音乐:

MusicObject.playMusic(MediaPlayer.create(this, songUri), idOfMusic, songID, songUri)

暂停音乐:

MusicObject.pauseMusic()