Background music Android

后端 未结 9 1418
夕颜
夕颜 2020-12-03 01:57

Okey, this is my problem. I have one service class where Ive managed to create media player to play music in background all time. Here is code:

package com.t         


        
相关标签:
9条回答
  • 2020-12-03 02:47

    yes, u should stop the service when u press the home button . In OnPause(){} override method u should stop the music service. And inside the service in OnDestroy() method u please stop the media player. So now,pressing home calls onPause and onPause() -> stops the service and OnDestroy is executed in service and in odestroy() the mediaplayer is stopped and ur problem is fixed.

    0 讨论(0)
  • 2020-12-03 02:51

    (Kotlin) Sadly Jong's answer didn't work for me, I had a few errors including: MediaPlayer finalized without being released and a delay in stopping and starting the sound. So i'll post the way I did it just in case anyone else has the same problem. My implementation of BackgroundSound still uses AsyncTask and MediaPlayer however it is a non-nested class.

    class BackgroundSound : AsyncTask<Context, Void?, Void?>() {
    
        override fun doInBackground(vararg params: Context): Void? {
            val player = MediaPlayer.create(params[0], R.raw.msc_background)
            player.isLooping = true
            player.start()
    
            while (!isCancelled) {
            }
    
            player.stop()
            player.release()
    
            return null
        }
    }
    

    You can use this class within your Activity like so:

    private var backgroundSound: BackgroundSound? = null
    
    override fun onResume() {
        super.onResume()
        backgroundSound = BackgroundSound()
        backgroundSound!!.execute(this)
    }
    
    override fun onPause() {
        backgroundSound?.cancel(true)
        super.onPause()
    }
    
    0 讨论(0)
  • 2020-12-03 02:53

    If you want to play background music for your app only, then play it in a thread launched from your app/use AsyncTask class to do it for you.

    The concept of services is to run in the background; By background, the meaning is usually when your app UI is NOT VISIBLE. True, it can be used just like you have (If you remember to stop it) but its just not right, and it consumes resources you shouldn't be using.

    If you want to peform tasks on the background of your activity, use AsyncTask.

    By the way, onStart is deprecated. When you do use services, implement onStartCommand.

    UPDATE:

    I think this code will work for you. Add this class (Enclosed in your activity class).

    public class BackgroundSound extends AsyncTask<Void, Void, Void> {
    
        @Override
        protected Void doInBackground(Void... params) {
            MediaPlayer player = MediaPlayer.create(YourActivity.this, R.raw.test_cbr); 
            player.setLooping(true); // Set looping 
            player.setVolume(1.0f, 1.0f); 
            player.start(); 
    
            return null;
        }
    
    }
    

    Now, in order to control the music, save your BackgroundSound object instead of creating it annonymously. Declare it as a field in your activity:

    BackgroundSound mBackgroundSound = new BackgroundSound();
    

    On your activity's onResume method, start it:

    public void onResume() {
        super.onResume();
        mBackgroundSound.execute(null);
    }
    

    And on your activity's onPause method, stop it:

    public void onPause() {
        super.onPause();
        mBackgroundSound.cancel(true);
    }
    

    This will work.

    0 讨论(0)
提交回复
热议问题