‫Hello, my friends, and welcome back.
‫In this video, we'll create a new application that uses Android services to play music in the background.
‫Open Android studio, create a new project, select Empty Views activity, and I'll name it as music
‫Player app, Click finish.
‫I'll copy this background image and paste it in the drawable folder.
‫Make sure that it starts with small letter and no numbers.
‫And this is my background for the application.
‫Let's move to the main activity and I'll start creating the layout background drawable back.
‫Remove this text view and I'll create two buttons.
‫The first one to play and to play the music, which is starting the service and the second button is
‫to stop the service and stop the music playing in turn.
‫Wrap Content.
‫Wrap Content ID.
‫Infer the constraints.
‫And here we go.
‫Drag and drop it in further constraints.
‫And I'll create a new text view just for making a title for this app.
‫And this is our layout for this application.
‫One text view and two buttons.
‫Let's move to the main activity and initialize these widgets.
‫Let's create our services.
‫The first step in creating the service is to create a class, a custom service class, and the custom
‫service class will be created in the same directory where the main activity class resides, and this
‫class will extend the service class and I'll name it as my custom service.
‫It will extend service class from Android app.
‫The callback methods are used to initiate and destroy the services so alt+ enter to implement the callback
‫method, which is the unbind method in this case to play music.
‫The media player object is used, so I'll create an object of media player.
‫Private media player.
‫I name it as player alt plus enter to import the media player class.
‫The media player class in Android is used to play audio and video files.
‫It provides a high level interface to control playback, pose, stop, seek and manage other aspects
‫of multimedia playback.
‫It supports a variety of audio and video formats and can be used to play media from various sources,
‫including local files, network streams and more.
‫In the previous lesson we said that the execution of service will start on calling the on start command.
‫So on start command method is implemented so I can create Oncreate method.
‫But here I'll use the on start command directly.
‫That will start on calling this method so the execution of the service will start on calling this method.
‫Here is the logic that I need to execute in the background.
‫So on start command I'll start creating a media player which will play the audio of default ringtone
‫in Android device.
‫So player equal to media player.
‫I'm initializing it media player dot create and here I need to pass two parameters the context for this
‫and the source of the media file that I need to play media or settings dot system dot or notification
‫or alarm alert, screen brightness mode and other things.
‫I need to use the default ringtone URL again, I'm creating a media player to play the default ringtone
‫in the device.
‫Then I need to loop the audio for that.
‫I'll use player dot set loop to true, and in order to start the process of playing I need to use player
‫dot start method.
‫This will start or resumes the playback of the music.
‫And the last thing here I need to return missing return statement and what type of return I need to
‫return a variable of type int.
‫But in this case I'm not using integer variables can use not sticky, sticky, sticky compatibility,
‫flag retry and others start sticky returns the status of the program.
‫So the on start command method of the service is used to handle the incoming start requests.
‫It returns an integer that specifies how the system should handle the service after it has been started.
‫There are several return types you can use, including start sticky if you return a start sticky from
‫on start command method.
‫It indicates that the service should be restarted if it's terminated by the system after it has been
‫started, the system will recreate the service with a null intent and you will need to handle reinitialization
‫of any necessary resources.
‫Use this return type If the service performs periodic tasks such as background data synchronization
‫where you want to ensure that the service resumes operation after being temporarily killed.