如何在其他活动中停止媒体播放器?

编程入门 行业动态 更新时间:2024-10-19 11:39:02
本文介绍了如何在其他活动中停止媒体播放器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我这样启动媒体播放器:

I start mediaplayer like this:

if (mp != null) { mp.stop(); mp.reset(); mp.release(); } mp = MediaPlayer.create(this, R.raw.background); mp.start();

如何停止其他活动?它继续在另一项活动中发挥作用.如何在其他活动中使用 onDestroy ?

How can I stop in another activity? It continues to play in another activity. How can I use onDestroy in another activity?

推荐答案

在您的项目中使用如下所示的Separate类.

Use the Separate class like below in your project.

import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.SoundPool; public class AudioPlay { public static MediaPlayer mediaPlayer; private static SoundPool soundPool; public static boolean isplayingAudio=false; public static void playAudio(Context c,int id){ mediaPlayer = MediaPlayer.create(c,id); soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100); if(!mediaPlayer.isPlaying()) { isplayingAudio=true; mediaPlayer.start(); } } public static void stopAudio(){ isplayingAudio=false; mediaPlayer.stop(); } }

播放歌曲

`AudioPlay.playAudio(mContext, R.raw.audiofile);` // play it from your preferred activity. and you can change raw file to your path also its depends upon your requirement.

然后在任何活动中使用此行 AudioPlay.stopAudio(); 停止音频.希望这可以帮助.

then stop the audio using this lines AudioPlay.stopAudio(); from any activity. hope this helps.

更多推荐

如何在其他活动中停止媒体播放器?

本文发布于:2023-11-27 03:58:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636488.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:媒体播放器   活动中   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!