问题描述
我想在我的Android应用程序播放背景声音。我使用一个单独的类作为SoundManager类。而在我的第一个活动,我开始的声音,我想它通过其他2 activitys玩。而在我的第三类我希望它停下来,另一个声音应该发挥。
但是,拥有即时停止声音问题,不知道为什么。我猜它是与用我SoundManager类类的类上下文,即时通讯。
I'm trying to play a background sound in my Android application. I'm using a seperate class as a Soundmanager. And in my first activity, i start the sound and i want it to play through 2 other activitys. And in my third class i want it to stop and another sound should play.But im having a problem stopping that sound and don't know why. I guess it has something to do with that im using the class Context in my Soundmanager class.
有谁知道如何使声音停止了吗?我将我的链接类SoundManager类监守,它是唯一一个是相关的我猜。林pretty新的节目,所以请温柔。
Does anyone know how to make the sound stop? I'll link my Soundmanager class becuase that is the only one that is relevant i guess. Im pretty new to programming, so please be gentle.
SoundManager类中的类
Soundmanager classhttp://codepad.org/KJi6DZN6
推荐答案
请所有的方法和类变量 SoundManager类
静态的(并通过在上下文
为需要一个方法)。
Make all of your methods and class variables in SoundManager
static (and pass in a Context
for the methods that require one).
我会怀疑你也许在您的每一个活动,这将创建 SoundManager类的多个实例的创建
SoundManager类
的新实例code>。使其静态访问将意味着所有的活动都修改相同的变量。
I would suspect that you are maybe creating a new instance of SoundManager
in each of your Activities, which would create multiple instances of SoundManager
. Making it statically accessible will mean all Activities are modifying the same variables.
另外要注意的,我不认为你必须有一个活动上下文
,所以尝试通过通过在应用程序的上下文context.getApplicationContext( )
。这将避免追平了的MediaPlayer
对象的任何具体活动。
Another note, I don't think you have to have an Activity Context
, so try passing in your application's Context via context.getApplicationContext()
. This will avoid tying the MediaPlayer
object to any specific Activity.
这篇关于不能停止在Android的播放声音的其他行为的MediaPlayer的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!