This tutorial will help you how to create a simple Audio Player application.Normally,when you make a player application based on Activity, it will stop when you exit the application. So, now you will know about a technique using Service to perform playing sound. You can play music when your application is closed.
1. MediaPlayer class
MediaPlayer class can be used to control playback of audio/video files and streams. And here is some method is used to control a mediaplayer object.
mp.reset():Resets the MediaPlayer to its uninitialized state.
setDataSource(String path): Sets the data source (file-path or http/rtsp URL) to use.
mp.prepare():Prepares the player for playback, synchronously. After setting the datasource and the display surface, you need to either call prepare() or prepareAsync(). For files, it is OK to call prepare(), which blocks until MediaPlayer is ready for playback.
mp.start():Starts or resumes playback. If playback had previously been paused, playback will continue from where it was paused. If playback had been stopped, or never started before, playback will start at the beginning.
mp.getDuration():Gets the duration of the file(Return the duration in milliseconds)
mp.isPlaying():Checks whether the MediaPlayer is playing.
mp.stop():Stops playback after playback has been stopped or paused.
mp.release():Releases resources associated with this MediaPlayer object.
2. Service class
Service class: A Service is an application component representing either an application’s desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding declaration in its package’s AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().
3. SongProvider class
Download Sample Source code