如何在其他活动中停止媒体播放器? [英] How can I stop media player in another activity?

查看:66
本文介绍了如何在其他活动中停止媒体播放器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样启动媒体播放器:

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.

这篇关于如何在其他活动中停止媒体播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆