【问题标题】:how to stop current fragment media player when moved to next fragment?移动到下一个片段时如何停止当前片段媒体播放器?
【发布时间】:2021-02-16 07:00:30
【问题描述】:

我有两个带有 tabLayout 的片段,当我想在另一个片段中播放声音时,我试图停止片段的当前声音 这是我的适配器类

public class Adapter extends RecyclerView.Adapter<Adapter.holder> {

public Context context;
String data[];
int images[];
int sound[];

holder h;
int playPosition = -1;

public Adapter() {
}

//constructor
public Adapter(String[] data, int[] images, int[] sound, Context context) {

    this.data = data;
    this.images = images;
    this.sound = sound;
    this.context = context;

    h = null;
    playPosition = -1;
}

//class holder
public class holder extends RecyclerView.ViewHolder {
    AppCompatImageView imageView;
    TextView textView;
    MediaPlayer mPlayer;


    public holder(@NonNull View itemView) {
        super(itemView);

        textView = itemView.findViewById(R.id.text_view);
        imageView = itemView.findViewById(R.id.img_view);
    }
}

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

@Override
public void onBindViewHolder(@NonNull final holder holder, final int position) {

    holder.textView.setText(data[position]);
    holder.imageView.setImageResource(images[position]);

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

            try {
                if (holder.mPlayer != null) {
                    holder.mPlayer.release();
                    holder.mPlayer = null;
                }
                holder.mPlayer = MediaPlayer.create(context, sound[position]);
                // stops the previous sound
                if (h != null && playPosition != -1 && position != playPosition) {
                    h.mPlayer.stop();
                    h.mPlayer = MediaPlayer.create(context, sound[playPosition]);

                }
                holder.mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {

                        holder.mPlayer.start();
                        Toast.makeText(context, "started", Toast.LENGTH_SHORT).show();

                        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mediaPlayer) {
                                mediaPlayer.stop();
                                mediaPlayer.release();
                                holder.mPlayer = null;
                                Toast.makeText(context, "Finished", Toast.LENGTH_SHORT).show();
                                // holder.playIcon.setVisibility(View.VISIBLE);
                            }
                        });
                    }
                });

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

            }
            h = holder;
            playPosition = position;
        }
    });

}

@Override
public int getItemCount() {
    return data.length;
}

}

这是我的片段

public class NatureForest extends Fragment {

RecyclerView recyclerView;

public NatureForest() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_main, container, false);
    String tab2Text[] = {"Forest forest","Forest creek","Forest leaves","Forest birds",
            "Forest waterfall","Forest wind","Forest fire",
            "Forest grasshopper", "Forest frogs"};
    int tab2Images[] = {R.drawable.ic_rain2,R.drawable.ic_rain2,R.drawable.ic_rain2,R.drawable.ic_rain2,
            R.drawable.ic_rain2,R.drawable.ic_rain2,R.drawable.ic_rain2,R.drawable.ic_rain2,R.drawable.ic_rain2};
    int sound[]={R.raw.sounds,R.raw.sounds,R.raw.sounds,R.raw.sounds,R.raw.sounds,
            R.raw.sounds,R.raw.sounds,R.raw.sounds,R.raw.sounds,R.raw.sounds,
            R.raw.sounds};
    recyclerView =view.findViewById(R.id.recycler_view);

    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
    recyclerView.setAdapter(new Adapter(tab2Text,tab2Images,sound,getContext()));
    return view;
}

}

这里如果播放一个片段中的声音,同时从第二个片段播放另一个声音,则前一个片段的声音不会停止。

【问题讨论】:

    标签: java android android-recyclerview android-mediaplayer


    【解决方案1】:

    onPause()

    :系统调用此方法作为用户离开片段的第一个指示。这通常是您应该提交任何应该在当前用户会话之外持久化的更改

    只需覆盖此方法并暂停该片段中的媒体播放器实例

    【讨论】:

    • 如何从我的片段中适配器的持有者类访问媒体播放器类?
    • @Override public void onPause() { super.onPause();适配器适配器 = 新适配器();适配器.stop(); }
    • 您想暂停正在片段中播放的媒体播放器。因此,一旦您转到其他选项卡,将调用片段的 onPause 方法(您要离开的片段),并且您拥有用于播放的媒体播放器实例,只需使用 tat 并暂停它
    • 是的,我明白了,但是我的 mediaPlayer 实例如何在适配器类中,所以我怎样才能在我的片段中停止它?
    • 你可以通过构造函数将它作为参数传递,你从'调用适配器,但我不明白为什么你需要适配器中的媒体播放器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多