Last active
September 7, 2021 08:30
-
-
Save Zulqurnain/4287e85b2a8b9fa33f485f1abfb4264e to your computer and use it in GitHub Desktop.
Simple music service using native music player and play any kind of online stream url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Service | |
import android.content.Intent | |
import android.media.AudioManager | |
import android.media.MediaPlayer | |
import android.media.MediaPlayer.OnPreparedListener | |
import android.os.IBinder | |
import androidx.annotation.Nullable | |
import java.io.IOException | |
/** | |
* Created by Zulqurnain on 7 Sept. 2021 | |
*/ | |
class StreamMusicPlayerService : Service(), OnPreparedListener { | |
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | |
val urlAddress = intent.getStringExtra(URL_ADDRESS_EXTRA) | |
if (urlAddress != null && urlAddress.isNotEmpty()) { | |
URL_ADDRESS = urlAddress | |
} | |
if (intent.action.equals(ACTION_START, ignoreCase = true)) { | |
mMediaPlayer = MediaPlayer() | |
mMediaPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC) | |
try { | |
mMediaPlayer!!.setDataSource(URL_ADDRESS) | |
mMediaPlayer!!.prepare() // might take long! (for buffering, etc) | |
IS_PLAYING = false | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
} else if (intent.action.equals(ACTION_START_PLAY, ignoreCase = true)) { | |
mMediaPlayer = MediaPlayer() | |
mMediaPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC) | |
try { | |
mMediaPlayer!!.setDataSource(URL_ADDRESS) | |
mMediaPlayer!!.prepare() // might take long! (for buffering, etc) | |
mMediaPlayer!!.start() | |
IS_PLAYING = true | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
} | |
return START_NOT_STICKY | |
} | |
@Nullable | |
override fun onBind(intent: Intent): IBinder? { | |
return null | |
} | |
/** | |
* Called when MediaPlayer is ready | |
*/ | |
override fun onPrepared(player: MediaPlayer) { | |
player.start() | |
} | |
override fun onDestroy() { | |
releasePlayer() | |
super.onDestroy() | |
} | |
override fun onLowMemory() { | |
releasePlayer() | |
super.onLowMemory() | |
} | |
companion object { | |
const val ACTION_START = "com.STREAM.PLAYER.START" | |
const val ACTION_START_PLAY = "com.STREAM.PLAYER.START.PLAY" | |
const val URL_ADDRESS_EXTRA = "com.STREAM.PLAYER.URL" | |
private var URL_ADDRESS: String? = null | |
var mMediaPlayer: MediaPlayer? = null | |
private var IS_PLAYING = false | |
fun stopMusic() { | |
if (mMediaPlayer != null && mMediaPlayer!!.isPlaying) { | |
mMediaPlayer!!.pause() | |
IS_PLAYING = false | |
} | |
} | |
fun startMusic() { | |
if (mMediaPlayer != null && !mMediaPlayer!!.isPlaying) { | |
mMediaPlayer!!.start() | |
IS_PLAYING = true | |
} | |
} | |
fun changeTrack(url: String?) { | |
if (url != null && !url.isEmpty()) { | |
URL_ADDRESS = url | |
} else { | |
return | |
} | |
try { | |
mMediaPlayer!!.reset() | |
mMediaPlayer!!.setDataSource(URL_ADDRESS) | |
mMediaPlayer!!.prepare() // might take long! (for buffering, etc) | |
IS_PLAYING = false | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
} | |
private fun releasePlayer() { | |
try { | |
if (mMediaPlayer != null) { | |
mMediaPlayer!!.stop() | |
mMediaPlayer!!.release() | |
} | |
mMediaPlayer = null | |
} catch (e: IllegalStateException) { | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment