I need to catch errors thrown by exoplayer when there is no input or bad stream. If there is no input activity opening showing player with controls nothing is displaying. when there is no input hls source I need to show the alert dialogue that Streaming is offline. How to achieve this. Please help.
class playlive : AppCompatActivity() {
private var player: SimpleExoPlayer? = null
private var playerView: PlayerView? = null
private var playWhenReady = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_playlive)
playerView = findViewById(R.id.player_view)
play()
}
private fun play(){
val trackSelector = DefaultTrackSelector()
trackSelector.setParameters(trackSelector.buildUponParameters().setMaxVideoSizeSd())
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector)
playerView!!.player = player
val dataSourceFactory = DefaultHttpDataSourceFactory(Util.getUserAgent(this, "app-name"))
val uri = Uri.parse("http://localhost:1935/live/mystream/index.m3u8")
val hlsMediaSource = HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri)
player!!.playWhenReady = playWhenReady
player!!.prepare(hlsMediaSource)
}
override fun onStop() {
super.onStop()
releasePlayer()
}
override fun onResume() {
super.onResume()
releasePlayer()
resumePlayer()
}
override fun onDestroy() {
super.onDestroy()
releasePlayer()
}
private fun releasePlayer(){
player?.release()
}
private fun resumePlayer(){
play()
}
}```
Related
I am using Exoplayer in my dialog. I want the video to play automatically when dialog opens. When
simpleExoPlayer.prepare() snippet is active I am able to do autoplay but when I close the dialog audio keeps playing. Before activating simpleExoPlayer.prepare() audio stops when I dismiss dialog. Is there another method to autoplay exoplayer or stop the audio when dialog dismiss?
class VideoViewDialog (context: Context) : BaseDialog<LayoutDialogVideoViewBinding>(context) {
private var videoUrl : String = ""
private lateinit var simpleExoPlayer: ExoPlayer
override fun populateUi() {
setCanceledOnTouchOutside(true)
mBinding?.apply {
initializePlayer()
}
}
private fun initializePlayer() {
val mediaDataSourceFactory: DataSource.Factory = DefaultDataSource.Factory(context)
val mediaSource = ProgressiveMediaSource.Factory(mediaDataSourceFactory).createMediaSource(
MediaItem.fromUri(videoUrl))
val mediaSourceFactory: MediaSource.Factory = DefaultMediaSourceFactory(mediaDataSourceFactory)
simpleExoPlayer = ExoPlayer.Builder(context)
.setMediaSourceFactory(mediaSourceFactory)
.build()
simpleExoPlayer.addMediaSource(mediaSource)
simpleExoPlayer.playWhenReady = true
simpleExoPlayer.prepare()
mBinding?.apply {
playerView.player = simpleExoPlayer
playerView.requestFocus()
}
simpleExoPlayer.play()
}
private fun releasePlayer() {
simpleExoPlayer.release()
}
public override fun onStart() {
super.onStart()
if (Util.SDK_INT > 23) initializePlayer()
}
public override fun onStop() {
super.onStop()
if (Util.SDK_INT > 23) releasePlayer()
}
override fun getLayoutRes(): Int {
return R.layout.layout_dialog_video_view
}
companion object{
fun newInstance(
context: Context,
videoUrl : String,
) : VideoViewDialog{
val dialog = VideoViewDialog(context)
dialog.also {
it.videoUrl = videoUrl
}
return dialog
}
}
}
I tried .stop, clearVideoSurface(), playerView.player = null before .release(). Didn't work
Seems like you called initializePlayer() twice. resulting in two Exoplayer instances playing; you're only able to release the one the simpleExoPlayer variable holds a reference to.
I am building a video player app for Android TV. I am using Exoplayer leanback dependency as explained in https://developer.android.com/training/tv/playback/transport-controls.
So far I've been able to display the video title, which is static, but I need to display a subtitle that is dynamic, it changes whenever the video playing changes. How can I do it?
The image below shows how the video player looks like. I've used a subtitle phrase as a placeholder on where it should appear.
I was able to solve the problem. I added a listener in the VideoSupportFragment class, Player.EventListener.
This way:
class VideoFragment(mediaItems: Map<String, Any>) : VideoSupportFragment(), Player.EventListener {
private var playerAdapter: ExoPlayerAdapter? = null
private var _mediaItems: Map<String, Any>? = null
private lateinit var mMediaPlayerGlue: VideoMediaPlayerGlue<ExoPlayerAdapter>
private var mItems: List<*>? = null
init {
_mediaItems = mediaItems
}
private val mHost: VideoSupportFragmentGlueHost = VideoSupportFragmentGlueHost(this)
private fun playWhenReady(glue: PlaybackGlue) {
if (glue.isPrepared) {
glue.play()
} else {
glue.addPlayerCallback(object : PlaybackGlue.PlayerCallback() {
override fun onPreparedStateChanged(glue: PlaybackGlue) {
if (glue.isPrepared) {
glue.removePlayerCallback(this);
glue.play()
}
}
})
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mediaSource = _mediaItems?.get("media_source") as Map<*, *>
playerAdapter = ExoPlayerAdapter(activity!!, _mediaItems!!)
mMediaPlayerGlue =
VideoMediaPlayerGlue(activity, playerAdapter!!)
mMediaPlayerGlue.host = mHost;
mMediaPlayerGlue.isControlsOverlayAutoHideEnabled = true
mItems = mediaSource["media_items"] as List<*>
mMediaPlayerGlue.title = mediaSource["title"] as CharSequence?
mMediaPlayerGlue.playerAdapter.setDataSource()
mMediaPlayerGlue.isSeekEnabled = true
playerAdapter?.player?.addListener(this);
playWhenReady(mMediaPlayerGlue)
}
override fun onPause() {
super.onPause()
playerAdapter?.player?.pause()
}
override fun onResume() {
super.onResume()
playerAdapter?.player?.play()
}
override fun onDestroy() {
super.onDestroy()
playerAdapter?.player?.removeListener(this);
}
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
if (mItems?.size!! > 1){
val item : Map<*, *> = mItems!![playerAdapter?.player?.currentWindowIndex!!] as Map<*, *>
mMediaPlayerGlue.subtitle = item["subtitle"] as String
}
}
}
Hello I'm try to play my hls stream http://81.25.234.43:8083/tv/1plus1HD/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9Mi8xNi8yMDIxIDI6MTE6MjcgUE0maGFzaF92YWx1ZT1GYjhMdlVJdUh1OHVsQ05hSVFTWjBBPT0mdmFsaWRtaW51dGVzPTYwNDgwMCZpZD0zMQ==
via LibVLC android
api "org.videolan.android:libvlc-all:3.3.0-eap17"
but video is always freezing while playing and i got in logs
libvlc video output: picture is too late to be displayed (missing 53 ms)
My codes
class VlcMediaPlayer(context: Context) : IVLCVout.Callback {
private val libVLCFactory = FactoryManager.getFactory(ILibVLCFactory.factoryId) as ILibVLCFactory
private val factory = FactoryManager.getFactory(IMediaFactory.factoryId) as IMediaFactory
private val options = ArrayList<String>().apply {
add("-vvv");
add("--avcodec-dr");
add("--clock-jitter=1500");
add("--live-caching=1500");
add("--network-caching=1500");
add("--file-caching=3000");
add("--no-drop-late-frames");
add("--no-skip-frames");
add("--no-sout-smem-time-sync");
add("--sout-mp4-faststart");
add("--sout-x264-partitions=fast");
add("--adaptive-logic=nearoptimal");
add("--adaptive-use-access");
add("--avcodec-threads=10");
add("--sout-x264-psy");
add("--aout=opensles");
add("--demuxdump-append");
add("--avcodec-hw=d3d11va");
}
private val sLibVLC = libVLCFactory.getFromOptions(context, VLCOptions.libOptions)
private val mediaPlayer = MediaPlayer(sLibVLC).apply {
vlcVout.addCallback(this#VlcMediaPlayer)
}
override fun onSurfacesCreated(vlcVout: IVLCVout?) {
}
override fun onSurfacesDestroyed(vlcVout: IVLCVout?) {
}
#MainThread
fun setVideoScale(scale: Float) {
mediaPlayer.scale = scale
}
fun setVideoAspectRatio(aspect: String?) {
mediaPlayer.aspectRatio = aspect
}
#MainThread
fun setSurface(surface: Surface, holder: SurfaceHolder?) {
// mediaPlayer.stop()
//mediaPlayer.release()
mediaPlayer.vlcVout.apply {
setVideoSurface(surface, holder)
attachViews(null)
}
//release()
}
fun setWindowsSize(width: Int, height: Int) {
mediaPlayer.vlcVout.setWindowSize(width, height)
}
fun setMedia(uri: Uri) {
val media = factory.getFromUri(sLibVLC, uri)
options.forEach {
media.addOption(it)
}
mediaPlayer.media = media
}
fun play() = mediaPlayer.play()
fun stop() = mediaPlayer.stop()
fun release() {
mediaPlayer.release()
}
}
I'm on creating a simple music player with Exoplayer in Android Kotlin. (Playing local MP3 in storage)
The problem is that the playback music is stopped outside the app and if the mobile turns on sleep mode.
So, I tried to implement the Foreground service, but it didn't work.
Below is my code without the part I tried to implement the Foreground service.
Please, let me know how to resolve this issue or how to correctly implement foreground service.
class AudioviewActivity : AppCompatActivity() {
private var player: SimpleExoPlayer? = null
private var playbackPosition = 0L
private var currentWindow = 0
private var playWhenReady = false
override fun onBackPressed() {
super.onBackPressed()
player!!.stop()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_audioview)
}
private fun initializePlayer() {
if (player == null) {
player = ExoPlayerFactory.newSimpleInstance(this)
val userAgent = Util.getUserAgent(this, "Mymusicplayer")
val mediaSource = ExtractorMediaSource(
Uri.parse("asset:///trackone.mp3"),
DefaultDataSourceFactory(this, userAgent), DefaultExtractorsFactory(),
null, null)
player!!.prepare(mediaSource)
player!!.seekTo(currentWindow, playbackPosition)
player!!.playWhenReady = playWhenReady
player!!.repeatMode = SimpleExoPlayer.REPEAT_MODE_ONE
}
}
private fun releasePlayer() {
player?.let {
playbackPosition = it.currentPosition
currentWindow = it.currentWindowIndex
playWhenReady = it.playWhenReady
it.release()
player = null
}
}
override fun onResume() {
super.onResume()
initializePlayer()
}
override fun onRestart() {
super.onRestart()
initializePlayer()
}
override fun onStop() {
super.onStop()
releasePlayer()
}
}
Since you are releasing player in Onstop , So when your app goes in background then Onstop is being called and you are releasing player , So dont release player in OnStop(), remove that part of the code.
I am stuck to play mp4 video on exoplayer yet exoplayer only run mpd
videos only please help me to resolve this issue.
when i pass .mpd format videos player will play smoothly and in case of mp4 player stuck to play
lateinit var mDailyWorkOutShowCatResponseList: ArrayList<DailyWorkOutShowCatResponse>
private lateinit var simpleExoplayer: SimpleExoPlayer
private var playbackPosition = 0L
private val dashUrl = "http://rdmedia.bbc.co.uk/dash/ondemand/bbb/2/client_manifest-separate_init.mpd"
private val bandwidthMeter by lazy {
DefaultBandwidthMeter()
}
private val adaptiveTrackSelectionFactory by lazy {
AdaptiveTrackSelection.Factory(bandwidthMeter)
}
var mUrlStr:String = "http://rdmedia.bbc.co.uk/dash/ondemand/bbb/2/client_manifest-separate_init.mpd"
fun videoPrepareImplementation(urlStr:String) {
initializeExoplayer(urlStr)
}
override fun getDailyWorkByCategoryOutResponse(dailyWorkOutShowCatResponselist: ArrayList<DailyWorkOutShowCatResponse>) {
videoPrepareImplementation(mUrlStr)
}
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
if (playbackState == Player.STATE_BUFFERING)
progressBar.visibility = View.VISIBLE
else if (playbackState == Player.STATE_READY)
progressBar.visibility = View.INVISIBLE
}
private fun initializeExoplayer(urlStr: String) {
simpleExoplayer = ExoPlayerFactory.newSimpleInstance(
DefaultRenderersFactory(this),
DefaultTrackSelector(adaptiveTrackSelectionFactory),
DefaultLoadControl()
)
prepareExoplayer(urlStr)
simpleExoPlayerView.player = simpleExoplayer
simpleExoplayer.seekTo(playbackPosition)
simpleExoplayer.playWhenReady = true
simpleExoplayer.addListener(this)
}
private fun releaseExoplayer() {
playbackPosition = simpleExoplayer.currentPosition
simpleExoplayer.release()
}
private fun buildMediaSource(uri: Uri): MediaSource {
val dataSourceFactory = DefaultHttpDataSourceFactory("ua", bandwidthMeter)
val dashChunkSourceFactory = DefaultDashChunkSource.Factory(dataSourceFactory)
return DashMediaSource(uri, dataSourceFactory, dashChunkSourceFactory, null, null)
}
private fun prepareExoplayer(urlStr: String) {
val uri = Uri.parse(urlStr)
val mediaSource = buildMediaSource(uri)
simpleExoplayer.prepare(mediaSource)
}
kinldy help me to solve this issue
just use ProgressiveMediaSource and buildMediaSource function will be like
private fun buildMediaSource(uri: Uri): MediaSource {
val dataSourceFactory = DefaultHttpDataSourceFactory("ua", bandwidthMeter)
return ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri)
}
and it will play
According to the code lab and according to my experience it should work. Just check this code lab:- https://codelabs.developers.google.com/codelabs/exoplayer-intro/index.html?index=..%2F..index#0
In this code lab mp3,mp4, and dash URL is working fine