英文:
Show Media Player Notification on Lock Screen
问题
你好,我正在学习如何在媒体播放器中显示进度通知,并且很好奇是否可以在锁屏界面上显示媒体播放器的进度通知。为此,我有以下的活动类代码:
@Suppress("DEPRECATION")
class MainActivity : Activity() {
private var mediaPlayer: MediaPlayer? = null
override fun onPause() {
super.onPause()
killMediaPlayer()
}//onPause ends
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showNotification(0, 0)
playButton.setOnClickListener {
if (mediaPlayer != null &&
mediaPlayer!!.isPlaying) {
Toast.makeText(applicationContext, "Already Playing!", Toast.LENGTH_SHORT).show()
} else {
initMediaPlayer()
}
}//playButton.setOnClickListener ends
}//onCreate ends
private fun initMediaPlayer() {
mediaPlayer = MediaPlayer().apply {
setAudioStreamType(AudioManager.STREAM_MUSIC)
setDataSource("https://www.radiantmediaplayer.com/media/bbb-360p.mp4")
prepare() // might take long! (for buffering, etc)
start()
}//apply ends
initMediaPlayerListenerEvents()
}//initMediaPlayer ends
private fun initMediaPlayerListenerEvents() {
if (mediaPlayer != null) {
mediaPlayer!!.setOnInfoListener { mp, what, extra ->
Log.e("streaX", "mediaPlayerInfo:::what:::$what".plus(" extra:::$extra"))
false
}
mediaPlayer!!.setOnBufferingUpdateListener { mp, percent ->
// Log.e("streaX", "mediaPlayerPercent:::$percent")
// showOreoBasedNotification(applicationContext, "Singing", "Percent:::" + percent)
}
mediaPlayer!!.setOnErrorListener { mp, what, extra ->
Log.e("streaX", "mediaPlayerError:::what:::$what".plus(" extra:::$extra"))
true
}
Log.e("streaX", "mediaPlayerDuration:::${mediaPlayer!!.duration}")
val mHandler = Handler()
runOnUiThread(object : Runnable {
override fun run() {
if (mediaPlayer != null) {
val mCurrentPosition: Int = mediaPlayer!!.currentPosition / 1000
Log.e("streaX", "mediaPlayerPosition:::$mCurrentPosition")
notification!!.setProgress((mediaPlayer!!.duration) / 1000, mCurrentPosition, false)
notificationManager.notify(1289, notification!!.build())
}
mHandler.postDelayed(this, 1000)
}
})
}//media player not null
}//initMediaPlayerListenerEvents ends
private var notification: NotificationCompat.Builder? = null
private val notificationId = "AudioStreamer12891226-3"
private val notificationName = "AudioStreamer-Test-3"
private lateinit var notificationManager: NotificationManager
private fun showNotification(durationTotal: Int, mCurrentPosition: Int) {
createNotificationChannel()
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notification = NotificationCompat.Builder(applicationContext, notificationId)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Playing BG Music")
.setContentText("Media Player Playing...")
.setSound(null)
.setOnlyAlertOnce(true)
.setProgress(durationTotal, mCurrentPosition, false)
.setPriority(NotificationCompat.PRIORITY_LOW)
notificationManager.notify(1289, notification!!.build())
}//showNotification ends
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val serviceChannel = NotificationChannel(
notificationId,
notificationName,
NotificationManager.IMPORTANCE_DEFAULT
)
val manager = getSystemService(NotificationManager::class.java)
manager!!.createNotificationChannel(serviceChannel)
}
}//createNotificationChannel ends
private fun killMediaPlayer() {
if (mediaPlayer != null &&
mediaPlayer!!.isPlaying) {
mediaPlayer!!.stop()
mediaPlayer!!.release()
mediaPlayer = null
}//media player null check ends
}//killMediaPlayer ends
}//class ends
一切都正常工作,通知在应用运行时出现,当我点击按钮时,进度根据媒体播放器流更新。但问题是,当我锁定设备时,通知根本不会出现。我正在使用三星 J5 来测试我的应用代码。
请问有人能告诉我我在做错了什么,以及三星 J5 设备(搭载 Oreo 安卓系统)是否能在锁屏界面上显示通知?
(我想展示这种类型的通知)
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/d1Zaw.png
英文:
Hello there I am learning the Media Play to my own and curious about if I can show the Media Player Progress Notification on Lock Screen. For this I have this Activity Class:
@Suppress("DEPRECATION")
class MainActivity : Activity() {
private var mediaPlayer: MediaPlayer? = null
override fun onPause() {
super.onPause()
killMediaPlayer()
}//onPause ends
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showNotification(0, 0)
playButton.setOnClickListener {
if (mediaPlayer != null &&
mediaPlayer!!.isPlaying) {
Toast.makeText(applicationContext, "Already Playing!", Toast.LENGTH_SHORT).show()
} else {
initMediaPlayer()
}
}//playButton.setOnClickListener ends
}//onCreate ends
private fun initMediaPlayer() {
mediaPlayer = MediaPlayer().apply {
setAudioStreamType(AudioManager.STREAM_MUSIC)
setDataSource("https://www.radiantmediaplayer.com/media/bbb-360p.mp4")
prepare() // might take long! (for buffering, etc)
start()
}//apply ends
initMediaPlayerListenerEvents()
}//initMediaPlayer ends
private fun initMediaPlayerListenerEvents() {
if (mediaPlayer != null) {
mediaPlayer!!.setOnInfoListener { mp, what, extra ->
Log.e("streaX", "mediaPlayerInfo:::what:::$what".plus(" extra:::$extra"))
false
}
mediaPlayer!!.setOnBufferingUpdateListener { mp, percent ->
// Log.e("streaX", "mediaPlayerPercent:::$percent")
// showOreoBasedNotification(applicationContext, "Singing", "Percent:::" + percent)
}
mediaPlayer!!.setOnErrorListener { mp, what, extra ->
Log.e("streaX", "mediaPlayerError:::what:::$what".plus(" extra:::$extra"))
true
}
Log.e("streaX", "mediaPlayerDuration:::${mediaPlayer!!.duration}")
val mHandler = Handler()
runOnUiThread(object : Runnable {
override fun run() {
if (mediaPlayer != null) {
val mCurrentPosition: Int = mediaPlayer!!.currentPosition / 1000
Log.e("streaX", "mediaPlayerPosition:::$mCurrentPosition")
notification!!.setProgress((mediaPlayer!!.duration) / 1000, mCurrentPosition, false)
notificationManager.notify(1289, notification!!.build())
}
mHandler.postDelayed(this, 1000)
}
})
}//media player not null
}//initMediaPlayerListenerEvents ends
private var notification: NotificationCompat.Builder? = null
private val notificationId = "AudioStreamer12891226-3"
private val notificationName = "AudioStreamer-Test-3"
private lateinit var notificationManager: NotificationManager
private fun showNotification(durationTotal: Int, mCurrentPosition: Int) {
createNotificationChannel()
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notification = NotificationCompat.Builder(applicationContext, notificationId)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Playing BG Music")
.setContentText("Media Player Playing...")
.setSound(null)
.setOnlyAlertOnce(true)
.setProgress(durationTotal, mCurrentPosition, false)
.setPriority(NotificationCompat.PRIORITY_LOW)
notificationManager.notify(1289, notification!!.build())
}//showNotification ends
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val serviceChannel = NotificationChannel(
notificationId,
notificationName,
NotificationManager.IMPORTANCE_DEFAULT
)
val manager = getSystemService(NotificationManager::class.java)
manager!!.createNotificationChannel(serviceChannel)
}
}//createNotificationChannel ends
private fun killMediaPlayer() {
if (mediaPlayer != null &&
mediaPlayer!!.isPlaying) {
mediaPlayer!!.stop()
mediaPlayer!!.release()
mediaPlayer = null
}//media player null check ends
}//killMediaPlayer ends
}//class ends
Everything working fine and the notification appears when app runs and when I click the button the progress updates as per Media Player Streams. But the problem is when I lock the device the notification won't appear at all. I am using Samsung J5 for testing my app code.
Can somebody please tell me what I am doing wrong and if it is possible that the Samsung J5 device having Oreo Android will show the notification on lock screen?
(I want to show this type of notification)
专注分享java语言的经验与见解,让所有开发者获益!
评论