Playing BG Music Across Activities in Android

后端 未结 6 900
南方客
南方客 2020-11-27 06:27

Hello! First time to ask a question here at stackoverflow. Exciting! Haha.

We\'re developing an Android game and we play some background music for our intro

6条回答
  •  没有蜡笔的小新
    2020-11-27 07:02

    You can also create a service which play music using mediaplayer as below.

    Intent svc=new Intent(this, BackgroundSoundService.class);
    startService(svc); //OR stopService(svc); 
    

    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) {
            // TODO
    
    
    
        }
        public IBinder onUnBind(Intent arg0) {
            // TODO Auto-generated method stub
    
            return null;
        }
    
        public void onStop() {
    
        }
        public void onPause() {
    
        }
        @Override
        public void onDestroy() {
    
            player.stop();
            player.release();
        }
    
        @Override
        public void onLowMemory() {
    
        }
    }
    

提交回复
热议问题