Skip to content

Instantly share code, notes, and snippets.

View IMoHaMeDHaMdYI's full-sized avatar
💻

Mohamed Hamdy IMoHaMeDHaMdYI

💻
  • N26
View GitHub Profile
private fun extractMediaSourceFromUri(uri: Uri): MediaSource {
val userAgent = Util.getUserAgent(this, "Exo")
return ExtractorMediaSource.Factory(DefaultDataSourceFactory(this, userAgent))
.setExtractorsFactory(DefaultExtractorsFactory()).createMediaSource(uri)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_player)
val mediaSource = extractMediaSourceFromUri(Uri.parse("asset:///heart_attack.mp3"))
val exoPlayer = ExoPlayerFactory.newSimpleInstance(
baseContext, DefaultRenderersFactory(baseContext)
, DefaultTrackSelector(),
DefaultLoadControl()
)
exoPlayer.apply {
class MusicService : MediaBrowserServiceCompat() {
override fun onLoadChildren(parentId: String, result: Result<MutableList<MediaBrowserCompat.MediaItem>>) {
}
override fun onGetRoot(clientPackageName: String, clientUid: Int, rootHints: Bundle?): BrowserRoot? {
return BrowserRoot("", null)
}
}
<service android:name=".MusicService">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService"/>
</intent-filter>
</service>
class MusicService : MediaBrowserServiceCompat() {
private var mMediaSession: MediaSessionCompat? = null
private lateinit var mStateBuilder: PlaybackStateCompat.Builder
private var mExoPlayer: SimpleExoPlayer? = null
private var oldUri: Uri? = null
private val mMediaSessionCallback = object : MediaSessionCompat.Callback() {
override fun onPlayFromUri(uri: Uri?, extras: Bundle?) {
super.onPlayFromUri(uri, extras)
uri?.let {
val mediaSource = extractMediaSourceFromUri(uri)
class PlayerActivity : AppCompatActivity() {
private lateinit var mMediaBrowserCompat: MediaBrowserCompat
private val connectionCallback: MediaBrowserCompat.ConnectionCallback = object : MediaBrowserCompat.ConnectionCallback() {
override fun onConnected() {
// The browser connected to the session successfully, use the token to create the controller
super.onConnected()
mMediaBrowserCompat.sessionToken.also { token ->
@IMoHaMeDHaMdYI
IMoHaMeDHaMdYI / BaseAdapter.kt
Last active March 13, 2019 14:51
BaseAdapter is a RecyclerView adapter that contains the main operations happen within the adapter,
interface Displayable {
fun getType(): Int
}
// ----------------------------------
abstract class ViewRenderer<in D : Displayable, VH : RecyclerView.ViewHolder>(val type: Int) {
abstract fun bindView(model: D, holder: VH)
abstract fun createViewHolder(parent: ViewGroup): VH
}
// Those are two tasks.
// TASK 1
open class SingletonHolder<out T, in A>(creator: (A) -> T) {
private var creator: ((A) -> T)? = creator
@Volatile
private var instance: T? = null
@IMoHaMeDHaMdYI
IMoHaMeDHaMdYI / ShadowExample.kt
Last active August 22, 2019 19:49
Discovering RadialGradient
// With the assumption, this code is written inside a view
// choose a reasonable range of colors, some ranges won't be drawn and I don't know why actually.
private val shadowStartColor = 0x99ffffff.toInt()
private val shadowEndColor = 0x03ffffff
private val shadowSize = 50f
val radialGradientPaint =RadialGradient(centerX
, centerY,
radius,
intArrayOf(shadowStartColor, shadowEndColor),
floatArrayOf(0f, 1f),
@IMoHaMeDHaMdYI
IMoHaMeDHaMdYI / Path.kt
Created August 25, 2019 15:24
Using Path
val path = Path()
val paint = Paint()
paint.apply {
strokeWidth = 20f
style = Paint.Style.STROKE
}
path.apply {
moveTo(xCenter, yCenter)