在 Android 应用的所有活动中播放背景音乐 [英] Play background music in all activities of Android app

查看:19
本文介绍了在 Android 应用的所有活动中播放背景音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到现在我花了大约 20 个小时,我的问题仍然存在.我正在创建一个具有多个活动(mainMenu、aboutUs、setting)的 android 应用程序.我按照以下链接的最佳答案进行操作,没关系.使用 asyncTask 播放的音乐不会因使用 cancel 而停止

I spend about 20 hours until now and my problem there still is . I am creating an android application that has several Activities (mainMenu , aboutUs,setting). I followed the best answered of below link and that was O.K . Music played using asyncTask isn't stopping by using cancel

当我运行我的应用程序时(我的代码在 mainActivity 中),音乐开始播放,并且在导航到其他活动时它不会停止.这很好.但是我在我的 setting_activity Activity 中放了一个 ToggleButton,我希望这个按钮可以启动和停止这个音乐.现在我的问题是如何从 setting_activity 停止和/或重新开始音乐?

When i run my app,(my code is in mainActivity ), the music begin and it does not stop when navigate to other Activities . This is GOOD . But i put a ToggleButton in my setting_activity Activity that i hope this Button starts and stops this music. NOW my question is how can i stop and/or start music again from setting_activity ?

在另一个解决方案中:我创建了一个 MusicManager 类,我称它为 start 和 stop .但这也是几个问题:

in another solution : I create a class MusicManager and i call it`s start and stop . But this was several problems too :

  1. 音乐从 mainMenu_activity 开始,但只播放了大约 15 秒,然后就停止了.
  2. 我无法停止其他活动的音乐.此时我在 mainMenua_ctivity 中播放音乐,如下行代码:

  1. Music started in mainMenu_activity but only play for about 15 seconds and then stopped.
  2. I could not stop the music from another activities.At this time i play music in mainMenua_ctivity as this line codes :

MusicManager mm = new MusicManager(this, R.raw.background_sound);
mm.play();

我怎样才能停止播放?3. 当我导航到其他活动时音乐停止了.

How can i stop playing that ? 3. The music stopped when i navigate to other activities .

public class MusicManager implements OnPreparedListener {

    static MediaPlayer mPlayer;
    Context context;
    private int mySoundId;

    public MusicManager(Context ctx, int musicID) {
        context = ctx;
        mySoundId = musicID;
        mPlayer = MediaPlayer.create(context, mySoundId);
        mPlayer.setOnPreparedListener(this);
    }

    public void play() {
        mPlayer = MediaPlayer.create(context, mySoundId);

    }

    public void stop() {
        mPlayer.stop();
        mPlayer.release();
    }

    @Override
    public void onPrepared(MediaPlayer player) {
        player.start();
        mPlayer.setLooping(true);
        mPlayer.setVolume(25, 25);

    }

}

最后我想在没有停止/开始音乐的情况下在所有活动中播放背景音乐.我该怎么做?

Finally i want to play a background music in all activities without stop/start music . How can i do it ?

推荐答案

你可以把音乐播放器放在一个服务中.这将使其独立于活动,您仍然可以通过意图控制播放.

You could put the music player in a service. This would make it independent from the Activities and you would still be able to control the playback through intents.

这里有一些关于它的代码示例:https://stackoverflow.com/a/8209975/2804473下面的代码由 Synxmax 在 StackOverflow 上编写,并在上面的链接中介绍:

Here are some code example about it: https://stackoverflow.com/a/8209975/2804473 The code below is written by Synxmax here at StackOverflow, and covered in the link above:

public class BackgroundSoundService extends Service {
    private static final String TAG = null;
    MediaPlayer player;
    public IBinder onBind(Intent arg0) {

        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.idil);
        player.setLooping(true); // Set looping
        player.setVolume(100,100);

    }
    public int onStartCommand(Intent intent, int flags, int startId) {
        player.start();
        return 1;
    }

    public void onStart(Intent intent, int startId) {
        // TO DO
    }
    public IBinder onUnBind(Intent arg0) {
        // TO DO Auto-generated method
        return null;
    }

    public void onStop() {

    }
    public void onPause() {

    }
    @Override
    public void onDestroy() {
        player.stop();
        player.release();
    }

    @Override
    public void onLowMemory() {

    }
}

这篇关于在 Android 应用的所有活动中播放背景音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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