播放背景音乐在Android应用程序的所有活动 [英] Play background music in all activities of Android app
问题描述
我花了约20小时,直至现在我的问题仍然是。 我创建的Android应用程序有几个活动(MAINMENU,公司简介,设置)。 我跟着最好的回答下面的链接,那就是确定。 <一href="http://stackoverflow.com/questions/22541751/music-played-using-asynctask-isnt-stopping-by-using-cancel">Music使用AsyncTask的停不使用撤销发挥
当我运行我的应用程序,(我的code在mainActivity),音乐开始,并没有切换不同的活动时停止。这是件好事。 但我把切换按钮在我setting_activity的活动,我希望这个按钮启动和停止这样的音乐。现在的问题是如何阻止和/或setting_activity重新开始音乐?
在另一种解决方案: 我创建一个类MusicManager我要求它`启动和停止。 但是,这几个问题太多:
- 音乐开始在mainMenu_activity但只玩了约15秒,然后停了下来。
- 在我无法从另一停止音乐activities.At这段时间我在mainMenua_ctivity播放音乐,因为这行codeS:
MusicManager毫米=新MusicManager(这一点,R.raw.background_sound); mm.play();
我怎样才能停止播放吗? 3.当我浏览到其他活动,音乐停止了。
公共类MusicManager实现在preparedListener {上下文 背景;
私人诠释mySoundId;静态MediaPlayer的MPLAYER;
公共MusicManager(上下文CTX,INT MusicID提供支持){背景= CTX; mySoundId = MusicID功能; MPLAYER = MediaPlayer.create(背景下, mySoundId); mPlayer.setOn preparedListener(本); }
公共无效播放(){MPLAYER = MediaPlayer.create(背景下, mySoundId);
}公共无效停止(){mPlayer.stop(); mPlayer.release(); }
@覆盖于prepared(MediaPlayer的播放器)公共无效{ player.start(); mPlayer.setLooping(真正的); mPlayer.setVolume(25, 25);
}
}
最后,我想在所有活动中播放背景音乐没有停止/启动音乐。 我怎样才能做到这一点?
您可以把音乐播放器中的服务。这将使它独立于活动,你仍然能够控制通过意图播放。
下面是一些关于它的code例如: http://stackoverflow.com/a/8209975/2804473 在code以下是这里的StackOverflow写Synxmax,覆盖在上面的链接:
公共类BackgroundSoundService延伸服务{
私有静态最后字符串变量= NULL;
MediaPlayer的播放器;
公众的IBinder onBind(意向为arg0){
返回null;
}
@覆盖
公共无效的onCreate(){
super.onCreate();
球员= MediaPlayer.create(这一点,R.raw.idil);
player.setLooping(真正的); //设置循环
player.setVolume(100,100);
}
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
player.start();
返回1;
}
公共无效ONSTART(意向意图,诠释startId){
// 去做
}
公众的IBinder onUnBind(意向为arg0){
//做自动生成的方法
返回null;
}
公共无效的onStop(){
}
公共无效的onPause(){
}
@覆盖
公共无效的onDestroy(){
player.stop();
player.release();
}
@覆盖
公共无效onLowMemory(){
}
}
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
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 ?
in another solution : I create a class MusicManager and i call it`s start and stop . But this was several problems too :
- Music started in mainMenu_activity but only play for about 15 seconds and then stopped.
- 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();
How can i stop playing that ? 3. The music stopped when i navigate to other activities .
public class MusicManager implements OnPreparedListener { Context context;
private int mySoundId; static MediaPlayer mPlayer;
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.
Here are some code example about it: http://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屋!