Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

java - Cant stop playing sound in other Activity with MediaPlayer in Android

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.

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 class http://codepad.org/KJi6DZN6

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make all of your methods and class variables in SoundManager static (and pass in a Context for the methods that require one).

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...