0

I'm developing an app that plays a background music. The music is managed by a service that runs in background. In the Main activity, in the onCreate() method, the service starts with:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Background music service.
    Intent musicServiceIntent = new Intent(this, BackgroundSoundService.class);
    startService(musicServiceIntent);

    final BackgroundSoundService bss = new BackgroundSoundService();
    buttonStart = (Button) v.findViewById(R.id.button_start);
    buttonStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bss.onStart();
        }
    });
    buttonPause = (Button) v.findViewById(R.id.button_pause);
    buttonPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bss.onPause();
        }
    });
    buttonStop = (Button) v.findViewById(R.id.button_stop);
    buttonStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bss.onStop();
            }
        });
    }
}
}

The service is the following code: BackgroundSoundService.java

    public class BackgroundSoundService extends Service {
    private static final String TAG = null;
    static MediaPlayer mMediaPlayer;

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

       @Override
        public void onCreate() {
            super.onCreate();

            mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.background_music);
            mMediaPlayer.setVolume(1.0f, 1.0f);
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer arg0) {
                    playMusic();
                }
            });

            playMusic();
        }

        public void playMusic() {
            mMediaPlayer.start();
        }

        /**
         *
         * @param arg0 -
         * @return -
         */
        public IBinder onUnBind(Intent arg0) {
            // TO DO Auto-generated method
            return null;
        }

        /**
         *
         */
        public void onStart() {
            Log.d("A", "onStart()");
            if(!mMediaPlayer.isPlaying()) {
                Log.d("A", "mMediaPlayer is not playing");
                playMusic();

            } else {
                Log.d("A", "mMediaPlayer is playing");
            }
        }

        /**
         *
         */
        public void onPause() {
            Log.d("A", "onPause()");
            if(mMediaPlayer.isPlaying()) {
                Log.d("A", "mMediaPlayer is playing");
                mMediaPlayer.pause();
            } else {
                Log.d("A", "mMediaPlayer is not playing");
            }
        }

        /**
         *
         */
        public void onStop() {
            Log.d("A", "onStop()");
            if(mMediaPlayer.isPlaying()) {
                Log.d("A", "mMediaPlayer is playing");
                mMediaPlayer.stop();
            } else {
                Log.d("A", "mMediaPlayer is not playing");
            }
        }

        /**
         *
         */
        @Override
        public void onDestroy() {
            Log.d("A", "onDestroy()");
            mMediaPlayer.stop();
            mMediaPlayer.release();
        }

        /**
         *
         */
        @Override
        public void onLowMemory() {

        }

    }

Now, MainActivity has three buttons to control the music. SART Button, starts music, PAUSE Button, put the sound in pause and STOP Button, stops music. Each one executes the methos onStart(), on Pause() and onStop() in the BackgroundSoundService. The problem is if I press the START Button after the STOP Button, the log show me this error:

E/MediaPlayer: start called in state 0
E/MediaPlayer: error (-38, 0)

I try solve this implementing:

    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });

But did not work. Any suggestions? Thank you.

flagg327
  • 967
  • 1
  • 10
  • 21

3 Answers3

0

Posting MainActivity code helps us to give an exact solution without deviating you to random answers.

Jaccs
  • 1,052
  • 3
  • 12
  • 33
0

After .stop() , if you want it to start again you need to reset it.

     public void onStop() {
                Log.d("A", "onStop()");
                if(mMediaPlayer.isPlaying()) {
                    Log.d("A", "mMediaPlayer is playing");
            mMediaPlayer.stop();
            mMediaPlayer.reset();
            mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.background_music);

                } else {
                    Log.d("A", "mMediaPlayer is not playing");
                }
            }

EDIT

boolean isPrepared = false;

mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            isPrepared = true;
        }
    });

 public void playMusic() {
   if(isPrepared){
            mMediaPlayer.start();
            isPrepared = false;
     }
    else{
    Toast.makeText(getApplicationContext(),"Not prepared",LENGTH.SHORT)
    .show();
    }
  }
mariuss
  • 1,177
  • 2
  • 14
  • 30
  • Hmm maybe your file is taking too much to prepare, try implementing onPrepareListener() and set up a flag. See my edited answer. – mariuss Sep 19 '16 at 16:59
0
  1. Instead of mediaPlayer.create(), call new mediaplayer
  2. Call prepare() and start() inside playMusic() OR prepareAsync() inside playMusic() and have onPreparedListener() calls start()
g-hos
  • 117
  • 1
  • 7