The application is divided in two files. The first one is the Activity which contains all the UI controls and ask the connection to the Service written in the second file.
In the MusicService class, there is a subclass which extends Binder. The only method in it is getService to access all the sevice's methodes.
The MainActivity is bind to the MusicService with the SerciveBinder class once the service is connected to the Activity.
We bind the MainActivity to the service in the onStart Method. We use the Flags BIND_AUTO_CREATE to be sure that the service is created and only once and BIND_ABOVE_CLIENT to tell the system that the UI should be killed in priority before the service in case of low memory.
To be sure that the service keep going when the UI is closed, we use the setForeground() method.
In the MainActivity a thread is running to keep the SeekBar up to date. It asks the service the progression of the playback and set it to the seekbar.
We had two main problems. The first one was to keep the service running even if lots of other processes are running. This was solved by adding the flag BIND_ABOVE_CLIENT and by starting the service in foreground. With this method, the system doesn't kill the service when it needs resources.
The second problem was that the player variable in the service was null each time we resumed the UI. This was because we called unbind() in the onStop() method. By suppressing this call, the problem was solved. But we don't really know why this variable was suppressed.
We also had a null pointer exception when we clicked on the button stop or pause before starting because the player wasn't initialized yet. So we check if the player != null && player.isPlaying() to avoid this issue.
Finally, when we clicked several times on the start button, each time the music started above the other. So we added the condition if the player was already running before creating it.
To be sure that the service keep going when the UI is closed, we use the setForeground() method.
In the MainActivity a thread is running to keep the SeekBar up to date. It asks the service the progression of the playback and set it to the seekbar.
We had two main problems. The first one was to keep the service running even if lots of other processes are running. This was solved by adding the flag BIND_ABOVE_CLIENT and by starting the service in foreground. With this method, the system doesn't kill the service when it needs resources.
The second problem was that the player variable in the service was null each time we resumed the UI. This was because we called unbind() in the onStop() method. By suppressing this call, the problem was solved. But we don't really know why this variable was suppressed.
We also had a null pointer exception when we clicked on the button stop or pause before starting because the player wasn't initialized yet. So we check if the player != null && player.isPlaying() to avoid this issue.
Finally, when we clicked several times on the start button, each time the music started above the other. So we added the condition if the player was already running before creating it.