安卓停止背景音乐 [英] Android Stop Background Music

查看:559
本文介绍了安卓停止背景音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑 - 发现了一个简易5-10线解!看到了自己的回答以下! YAY !!!!!!!!!

EDIT - FOUND A EASY 5-10 LINE SOLUTION!!! See MY OWN ANSWER BELOW!!! YAY!!!!!!!!!

我搜索了5个小时,几十个SO岗位,没有答案,这似乎是最简单的明显吓坏的事!

I've searched for 5 hours, dozens of SO posts, no answers, and this seems like the most simple obvious freaking thing!!!

编辑 - 顺便说一句,这不是一个音乐播放器应用程序,只是一个应用程序来查看图片和文字,打开多个活动,如菜单和一下,查看差异类型的图片等。我只想玩一些简单的背景音乐,一边看直通的图片和文字,这是为什么这么难?

EDIT- btw, this is NOT a music player app, just an app to view pics and text, opens multiple activities like menu and about, view diff types of pictures etc. i just want to play some simple background music while looking thru the pics and text, why is that so difficult?

另一个编辑 - 看来主要问题真的是: 为什么preSSING Home键不叫的onPause或的onStop ??? - 因此,我可以知道何时停止媒体播放器?如何做游戏我下载了市场上做到这一点???

another EDIT - it seems the main question really is: "WHY DOES PRESSING THE HOME BUTTON NOT CALL onPause or onStop ???" -so i can know when to stop the media player? and how do the games i download on the market accomplish this???

回家活动开始

然后

媒体播放器启动:

player = MediaPlayer.create(this, R.raw.idil);
player.setLooping(true);
player.setVolume(100,100);
player.start();

播放器的onCreate之外声明

Player declared outside of onCreate

MediaPlayer player;

在其他活动开了,背景音乐也不会中断,这是很好的,这就是我想要的。

When other activities are opened, the background music continues uninterrupted, which is GOOD, that is what I want.

现在,当我与我的非常简单的应用程序正在做(这只是显示了一些图片和文本的差异活动),我可以单击后退多次去家/原活性,然后有更多的时间来退出,或者说,我只是preSS家退出,是因为IM DONE这个应用程序,我不需要听音乐了。

Now, when I am done with my very simple app (that just shows some pics and texts in diff activities), I either click BACK multiple times to get to the home/original activity, and then one more time to "exit", OR, I simply press home to "exit" because im DONE with this app and I do not need to hear that music anymore.

OPTION 1

呼叫 player.stop(); 的onPause 覆盖,这是不可以我想要的,因为背景音乐停止时,我离开家活动的其他活动,如菜单和关于,我也不要使用什么样的暂停和开放新活动时恢复,因为我不想让pretty的背景音乐为跳过或中断。

Call player.stop(); in an onPause override, this is not what I want because background music stops when i leave the home activity for other activities like 'menu' and 'about', I also do not what to use pause and resume when opening new activities because I do not want the pretty background music to 'skip' or be interrupted.

选项2

@Override
    protected void onPause() {
        super.onPause();
        if (this.isFinishing()){
            player.stop();
        }
    }

这是更好,因为背景音乐不活动之间停下来,当我返回从我家活动preSS,音乐停止了,我可以继续享受我的Andr​​oid有趣的手机在和平与宁静,的问题是,当pressing HOME键退出我的应用程序,那个讨厌的背景音乐继续播放。

This is better because background music does not stop between activities, and when I press BACK from my home activity, the music stops, and I can continue to enjoy my android fun phone in peace and quiet, but the problem is, when pressing the HOME button to "exit" my app, that pesky background music keeps playing.

奇怪的是

@Override
protected void onStop() {
        super.onStop();
        if (this.isFinishing()){
            player.stop();
        }
    }

是否一样的onPause(我也了解实际差异)

Does the same as onPause (and i do understand the actual differences)

编辑 - 此外,它似乎没有问题,如果player.stop();高于或低于super.onStop();但它影响的东西,我不能看,无论哪种方式,依然无解:(

EDIT- ALSO it doesnt seem to matter if player.stop(); is above or below super.onStop(); but it affecting something i cant see, either way, still no SOLUTION :(

OOOOO

OOOOO

编辑 - ANOTHER方案 - 但不工作

EDIT- ANOTHER OPTION- BUT DOES NOT WORK

public void onUserLeaveHint() {
    player.stop();
    super.onUserLeaveHint();
}

该停止的时候我preSS回家的音乐,但它也将停止,当我开始新的活动:(

this stops the music when i press HOME but it also stops it when i start new activities :(

编辑 - 一个非常普遍的解决我见过的多个地方,但似乎可笑不得不做的:

EDIT - A VERY COMMON WORK AROUND IVE SEEN MULTIPLE PLACES, but seems to rediculous to have to do:

essentialy保留已被打开和关闭活动的数量计数(onResume和会的onPause概率是最好的,但指向不相关的),当该数为0,停止背景音乐。是的,这是pretty的简单,但为什么我要programattically做到这一点,其实最大的问题要问这个职位是:

essentialy keep count of the number of activities that have been opened and closed (onResume and onPause would prob be best, but that points irrelevant) and when that count reaches 0, stop the background music. YES that is pretty simple, but why do I have to programattically do this, actually the BIGGEST QUESTION FOR THIS POST IS:

要把它的onDestroy是不是一种选择,因为的onDestroy当系统内存不足时,才会调用,或强制关闭应用程序,并且是有据可查的。

To put it in onDestroy is not an option because onDestroy is only called when the system is low on memory, or you force close you app, and that is well documented.

覆盖HOME键也没办法,因为我已经阅读了不可能的,我已经阅读了在非常皱了皱眉,无论哪种方式进出口避免了

Overriding the HOME button also is no option as I have read its "not possible" and I have read its "extremely frowned upon", either way Im avoiding that

我不认为有必要创建服务,甚至如果我这样做的时候,我会停止该服务,看来我也有同样的问题。

I dont see the need to create SERVICE, and even if I did, when would I stop the service, it seems I would have the same problem

现在这里是一个完全吹拂我的心灵,游戏和每次应用我已经从Android Market下载具有非常优美的背景音乐的东西,而当我preSS BACK或HOME,因为我做的是玩游戏可爱的音乐停止,不守在后台播放。

NOW HERE IS THE THING that completely blows my mind, every game and every app I have downloaded from the android market has very beautiful background music, and when I press BACK or HOME because I am done playing that lovely game the music stops, not keeping playing in the background.

我很沮丧,我觉得它的,因为我觉得我失去了一些东西非常简单,因为这是与任何应用程序中的最基本的生命周期的问题之一。

I am very frustrated, and I feel its because I feel like I am missing something very simple, because this is one of the most basic lifecycle issues with any app.

我花了一个月读developer.android.com包括开发指南,教程,样本项目,和研究Refernece节的每一页,以及谷歌,荷兰国际集团这个话题5小时。

I spent a month reading every page of developer.android.com including the Dev Guide, the tutorials, sample projects, and researching the Refernece section, as well as google-ing this topic for 5 hours.

我也是不明白为什么在6或7 SO与确切同一个问题的线程,拥有的不可以得到回答,在市场上每一个可下载的应用程序将停止播放其乐(除非它的背景音乐播放器),当你preSS HOME或BACK或任何退出的应用程序去做些别的事情您的手机上。

I also dont understand why the 6 or 7 SO threads with the exact same issue, have not been answered, every downloadable app on the market stops playing its music (unless its a background music player) when you press HOME or BACK or whatever to "exit" the app to go do something else on your phone.

我在想什么?

推荐答案

我今天很开心,而且还有头发:) 我测试过这一点,它的工作原理!

I'm very happy today, and still have hair :) I've tested this and it works!!!

首先,添加到您的清单:

First, add this to your Manifest:

<uses-permission android:name="android.permission.GET_TASKS"/>

二,添加到您的首页/主'活动/类:

Second, add this to your 'Home/Main' Activity/Class:

  @Override
  protected void onPause() {
    if (this.isFinishing()){ //basically BACK was pressed from this activity
      player.stop();
      Toast.makeText(xYourClassNamex.this, "YOU PRESSED BACK FROM YOUR 'HOME/MAIN' ACTIVITY", Toast.LENGTH_SHORT).show();
    }
    Context context = getApplicationContext();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    if (!taskInfo.isEmpty()) {
      ComponentName topActivity = taskInfo.get(0).topActivity; 
      if (!topActivity.getPackageName().equals(context.getPackageName())) {
        player.stop();
        Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
      }
      else {
        Toast.makeText(xYourClassNamex.this, "YOU SWITCHED ACTIVITIES WITHIN YOUR APP", Toast.LENGTH_SHORT).show();
      }
    }
    super.onPause();
  }

和使用,那么,你的类的名称显然替换xYourClassNamex:)

And obviously replace xYourClassNamex with, well, Your Class Name :)

现在显然你并不需要的干杯,但它会告诉你是怎么回事。 非常野趣thind是,当你回来从你的'家/主要活动preSS,你显然得到2敬酒,你preSSED回程从你的'HOME /主要活动,而第二个面包是您切换活动范围内您的应用程序。我相信我知道为什么会这样,但是它不,无所谓,因为我所说的player.stop();从2方案是不是意味着不再被使用我的应用程序。显然做得比更多的工作player.stop();如果你需要:)而且也很明显你不需要的东西为您切换活动范围内您的应用程序,因为没有理由停止/暂停的背景音乐,这正是我需要的,但如果你确实需要做一些事情时,新的活动开始了,还有在这里你去:)

Now obviously you do not need the "Toasts" but it will tell you what is going on. The very intersting thind is when you press BACK from your 'Home/Main' activity, you obviously get 2 Toasts, "YOU PRESSED BACK FROM YOUR 'HOME/MAIN' ACTIVITY", and the 2nd Toast is "YOU SWITCHED ACTIVITIES WITHIN YOUR APP". I believe I know why this happens, but it doesn;t matter because I call "player.stop();" from the 2 scenarios that mean my app is no longer being 'used'. Obviously do more work than "player.stop();" if you need to :) And also obvious you dont need the "else" for "YOU SWITCHED ACTIVITIES WITHIN YOUR APP", because there is no reason to "stop/pause" the background music, which is what i needed, but if you DO need to do something when new activities are started, well here you go :)

希望这有助于任何人都希望知道,当用户的应用程序叶/退出/用了:)

Hope this helps anyone looking to know when the user "leaves/exits/is done with" the app :)

感谢所有的意见邮电大家有所帮助!耶!

THANKS FOR ALL OF THE COMMENTS POSTS AND HELP EVERYONE!!! YAY!

编辑 -

这部分需要在每一个活动的的onPause:

This part has to be in EVERY activity's onPause:

Context context = getApplicationContext();
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        if (!taskInfo.isEmpty()) {
          ComponentName topActivity = taskInfo.get(0).topActivity; 
          if (!topActivity.getPackageName().equals(context.getPackageName())) {
            player.stop();
            Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
          }
        }

这样你就会知道,如果用户离开从任何活动,您的应用程序。这是很好的了解:)

so you'll know if the user left your app from ANY of the activities. this is good to know :)

这篇关于安卓停止背景音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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