停止 RecyclerView MediaPlayer 中的上一个项目

Stopping previous item in RecyclerView MediaPlayer

我有一个 RecyclerView,其中每个项目都包含一个播放在线声音的按钮 url。

问题是我不希望这些项目同时播放声音,当你点击不同的声音时,前一个声音会消失,但我不知道我该怎么做?

如果你能指导我解决这个问题,我将不胜感激

public class VersionAdapter3 extends RecyclerView.Adapter<VersionAdapter3.VersionViewholder> {

Context context;
List<Version3> versionsList;
final Mediaplayer mediaplayer=new Mediaplayer();




public VersionAdapter3(Context context, List<Version3> versionsList) {
    this.context = context;
    this.versionsList = versionsList;

}

  @NonNull
@Override
public VersionViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.raw3,parent,false);
    return new VersionViewholder(view);
}

@Override
public void onBindViewHolder(@NonNull VersionViewholder holder, int position) {
    Version3 version3 = versionsList.get(position);
    holder.codeNameTxt.setText(version3.getCodeName());
    holder.versionTxt.setText(version3.getVersion());
    holder.descriptiontxt.setText(version3.getDescription());


    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
   try {
        mediaPlayer.reset();
        mediaPlayer.setDataSource(version3.getSong());
    } catch (IOException e) {
        e.printStackTrace();
    }

    mediaPlayer.prepareAsync();




    boolean isExpandable = versionsList.get(position).isExpandable();
    holder.expandableLayout.setVisibility(isExpandable ? View.VISIBLE : View.GONE);

   holder.btnplay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            if (mediaPlayer!=null) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                    version3.setIsplaying(false);

                }

            }
            try {

                            if (holder.btnplay.getText().toString().equals("pause")) {
                                mediaPlayer.pause();
                                version3.setIsplaying(false);
                                holder.btnplay.setText("play");

                            }

                        else {

                             holder.btnplay.getText().toString().equals("play");
                                mediaPlayer.start();
                                holder.btnplay.setText("pause");
                                version3.setIsplaying(true);
                            }


            } catch (Exception e) {
                e.printStackTrace();
            }

                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        holder.btnplay.setText("play");
                    }
                });

            }

    });



}
@Override
public int getItemCount() {
    return versionsList.size();
}

public class VersionViewholder extends RecyclerView.ViewHolder {

    TextView codeNameTxt, versionTxt, descriptiontxt;
    LinearLayout linearLayout;
    RelativeLayout expandableLayout;
    Button btnplay;
    public VersionViewholder(@NonNull View itemView) {


        super(itemView);
   codeNameTxt = itemView.findViewById(R.id.codename);
        versionTxt = itemView.findViewById(R.id.version);
        descriptiontxt = itemView.findViewById(R.id.description);
        btnplay = itemView.findViewById(R.id.track_play);
        linearLayout = itemView.findViewById(R.id.linear);
        expandableLayout = itemView.findViewById(R.id.expandable);


        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
           public void onClick(View view) {
                Version3 version3 = versionsList.get(getAdapterPosition());
                version3.setExpandable(!version3.isExpandable());
                notifyItemChanged(getAdapterPosition());

            }
        });

    }
}

}

当我点击每个项目时它会展开并且之前的声音消失但按钮仍然显示在暂停模式而不是播放模式并且我不希望在展开新项目时声音消失并且我想要当我点击下一步按钮时它会消失 只是在你暂停前一个按钮的情况下它可以工作但我希望当我点击新的播放按钮并且前一个按钮完全关闭时它会自动关闭 如果您能帮助我修复我的代码并告诉我如何编辑它,我将不胜感激

这是一个例子:

MainActivity class:

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private VersionAdapter3 adapter;

    private List<Version3> generateVersionList() {
        List<Version3> versionsList = new ArrayList<>();
        versionsList.add(new Version3("codeName 01", "version 01", "Description 01", "https://storage.googleapis.com/2waytech-apagar/Just%20a%20Miracle%20Apart%20-%20Lost%20European.mp3", Version3.State.STOPPED));
        versionsList.add(new Version3("codeName 02", "version 02", "Description 02", "https://storage.googleapis.com/2waytech-apagar/Newspaper%20And%20Lace%20-%20Lost%20European.mp3", Version3.State.STOPPED));
        versionsList.add(new Version3("codeName 03", "version 03", "Description 03", "https://storage.googleapis.com/2waytech-apagar/The%20Man%20in%20the%20House%20of%20Cards%20-%20Lost%20European.mp3", Version3.State.STOPPED));
        return versionsList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.list);

        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);

        adapter = new VersionAdapter3(generateVersionList());

        recyclerView.setLayoutManager(llm);
        recyclerView.setAdapter(adapter);
    }
}

VersionAdapter3 class:

public class VersionAdapter3 extends RecyclerView.Adapter<VersionAdapter3.VersionViewHolder> {
    private final List<Version3> versionsList;
    private final MediaPlayer mediaPlayer;
    private Integer currentlyPlayingIndex = -1;

    public VersionAdapter3(List<Version3> versionsList) {
        this.versionsList = versionsList;
        this.mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(mediaPlayer -> {});
    }

    private void setupMediaPlayer() {
        mediaPlayer.setOnPreparedListener(MediaPlayer::start);
        mediaPlayer.setAudioAttributes(
                new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                        .setUsage(AudioAttributes.USAGE_MEDIA)
                        .build()
        );
    }

    private void onPlayBtnTapped(int newIndex) {

        if(newIndex != currentlyPlayingIndex && currentlyPlayingIndex != -1) {
            versionsList.get(currentlyPlayingIndex).setState(Version3.State.STOPPED);
            notifyItemChanged(currentlyPlayingIndex);
            mediaPlayer.stop();
        }

        if(newIndex == currentlyPlayingIndex) {
            if(mediaPlayer.isPlaying()) {
                versionsList.get(newIndex).setState(Version3.State.PAUSED);
                mediaPlayer.pause();
            }
            else {
                versionsList.get(newIndex).setState(Version3.State.PLAYING);
                mediaPlayer.start();
            }

        } else {

            try {
                currentlyPlayingIndex = newIndex;
                Version3 version = versionsList.get(newIndex);
                String dataSource = version.getSong();
                version.setState(Version3.State.PLAYING);
                mediaPlayer.reset();
                mediaPlayer.setDataSource(dataSource);
                mediaPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        notifyItemChanged(newIndex);
    }

    @Override
    public void onViewAttachedToWindow(@NonNull VersionViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        setupMediaPlayer();
    }

    @NonNull
    @Override
    public VersionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.raw3, parent, false);
        return new VersionViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull VersionViewHolder holder, int position) {

        Version3 version3 = versionsList.get(position);

        holder.codeNameTxt.setText(version3.getCodeName());
        holder.versionTxt.setText(version3.getVersion());
        holder.descriptiontxt.setText(version3.getDescription());
        holder.adjustBtnTextByState(version3.getState());
        holder.btnplay.setOnClickListener(view -> onPlayBtnTapped(position));
    }

    @Override
    public int getItemCount() {
        return versionsList.size();
    }

    public static class VersionViewHolder extends RecyclerView.ViewHolder {

        TextView codeNameTxt, versionTxt, descriptiontxt;
        Button btnplay;

        public VersionViewHolder(@NonNull View itemView) {
            super(itemView);
            codeNameTxt = itemView.findViewById(R.id.codename);
            versionTxt = itemView.findViewById(R.id.version);
            descriptiontxt = itemView.findViewById(R.id.description);
            btnplay = itemView.findViewById(R.id.track_play);
        }

        @SuppressLint("SetTextI18n")
        public void adjustBtnTextByState(Version3.State state) {
            switch (state) {
                case STOPPED:
                    btnplay.setText("stopped");
                    break;
                case PLAYING:
                    btnplay.setText("playing");
                    break;
                case PAUSED:
                    btnplay.setText("paused");
                    break;
            }
        }
    }

}

版本 3 class

public class Version3 {

    private String codeName;
    private String version;
    private String description;
    private String song;
    private State state;

    public Version3(String codeName, String version, String description, String song, State state) {
        this.codeName = codeName;
        this.version = version;
        this.description = description;
        this.song = song;
        this.state = state;
    }

    public String getCodeName() {
        return codeName;
    }

    public void setCodeName(String codeName) {
        this.codeName = codeName;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getSong() {
        return song;
    }

    public void setSong(String song) {
        this.song = song;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public enum State {
        STOPPED, PLAYING, PAUSED
    }
}

main_activity.xml布局文件:

<?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=".MainActivity">

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

</androidx.constraintlayout.widget.ConstraintLayout>

和 raw3.xml 布局文件:

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/codename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="codename"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth="match_parent" />

    <TextView
        android:id="@+id/version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:text="version"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/codename"
        app:layout_constraintWidth="match_parent" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:text="description"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/version"
        app:layout_constraintWidth="match_parent" />

    <Button
        android:id="@+id/track_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="stop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/description" />

</androidx.constraintlayout.widget.ConstraintLayout>