更改网址后,如何在exoplayer中获得正确的持续时间?

时间:2018-10-31 09:34:26

标签: android exoplayer

我用chrome cast实现了exo-player,我的问题是当我连接到Cast设备后更改URL时,我得到了不正确的持续时间,而chrome Cast的已连接符号未更改以显示其已连接URL的更改方式。 总之,我正在做的步骤:-

1。打开应用,转到视频部分并打开它。
2.exo-player以URL开头,现在我正在使用演员表图标连接演员表
3.cast已连接,现在杀死该应用程序,在视频部分再次转到
4.选择另一个视频投射屏幕。 在这里,我持续时间不正确,并且投射图标未显示已连接

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_watch_archive_video)

    simpleExoPlayerView = findViewById(R.id.player_view) as SimpleExoPlayerView
    exo_position = findViewById(R.id.exo_position)
    lv_chrome_cast = findViewById(R.id.lv_chrome_cast)
    tv_chrome_text = findViewById(R.id.tv_chrome_text)
    tv_video_title = findViewById(R.id.tv_video_title)
    mMediaRouteButton =  findViewById(R.id.media_route_button)
   // mMediaRouteButton?.setRemoteIndicatorDrawable(R.drawable.about_icon)
    cast_control_view=findViewById(R.id.cast_control_view)

    CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), mMediaRouteButton);
    mCastContext = CastContext.getSharedInstance(this)
    if(mCastContext?.getCastState() != CastState.NO_DEVICES_AVAILABLE) {
        mMediaRouteButton?.setVisibility(View.VISIBLE)
    }
    mCastContext?.addCastStateListener(this)
    mCastContext?.setReceiverApplicationId("123456789")
    castPlayer =  CastPlayer(mCastContext)
    castPlayer?.addListener(this)
    castPlayer?.setSessionAvailabilityListener(this)
    cast_control_view?.player=castPlayer
    Log.e("~~videuorl","-"+url)

    mCastContext?.sessionManager?.addSessionManagerListener(this)
}private fun initializePlayer(urlName: String?) {
    if (player == null) {
        simpleExoPlayerView?.requestFocus()
        simpleExoPlayerView?.setControllerVisibilityListener {
            //  simpleExoPlayerView.hideController();
        }
        val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeter)
        trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)
        player = ExoPlayerFactory.newSimpleInstance(this, trackSelector)
        simpleExoPlayerView?.player = player
        //player?.setPlayWhenReady(shouldAutoPlay)
        val extractorsFactory = DefaultExtractorsFactory()
        val myurl = Uri.parse(urlName)
        val mediaSource = HlsMediaSource(myurl, mediaDataSourceFactory, 1800000, null, null)
        player?.prepare(mediaSource)
        player?.addListener(this)
        if(castPlayer?.isCastSessionAvailable!!){
              //mCastContext?.castOptions?.enableReconnectionService
            //castPlayer?.duration?.let { castPlayer?.seekTo(currentWindow, it) }
            onCastSessionAvailable()
        }
    }else{
        player?.playWhenReady = true

    }
}
override fun onCastStateChanged(state: Int) {
    if (state == CastState.NO_DEVICES_AVAILABLE){
        mMediaRouteButton?.setVisibility(View.GONE)
    }
    else {
       // Toast.makeText(this,  "${state}", Toast.LENGTH_LONG).show()
        //if (mMediaRouteButton?.visibility == View.GONE)
        mMediaRouteButton?.visibility = View.VISIBLE
        if (state == CastState.NOT_CONNECTED){
            mMediaRouteButton?.visibility = View.VISIBLE
        }
        if (state == CastState.CONNECTING){
        }
        if (state == CastState.CONNECTED){

        }
    }
}
override fun onCastSessionAvailable() {
    sessionManager=mCastContext?.sessionManager?.currentCastSession
    deviceId =sessionManager?.castDevice?.friendlyName
    //sessionManager?.addCastListener(castlisten())
    lv_chrome_cast?.visibility=View.VISIBLE
    tv_chrome_text?.text="Playing on $deviceId"
    tv_video_title?.text=name

    val movieMetadata =  MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE)
    movieMetadata.putString(MediaMetadata.KEY_TITLE, name)
    movieMetadata.addImage(WebImage(Uri.parse("https://www.abcdefg.com/images/abcd-logo.png")))

    val mediaInfo =  MediaInfo.Builder(url)
    if (castUrlType.equals("live")){
        mediaInfo.setStreamType(MediaInfo.STREAM_TYPE_LIVE)
    }else{
        mediaInfo.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
    }
    mediaInfo.setContentType(MimeTypes.VIDEO_UNKNOWN)
    mediaInfo.setMetadata(movieMetadata)
    val mediaItems = arrayOf(MediaQueueItem.Builder(mediaInfo.build()).build())
    castPlayer?.loadItems(mediaItems, 0, player?.currentPosition ?: 0, Player.REPEAT_MODE_OFF)

    player?.volume=0f
    player?.playWhenReady=false

}
override fun onCastSessionUnavailable() {
    lv_chrome_cast?.visibility=View.GONE
    if (player!=null){
        player?.volume=1.0f
        player?.seekTo(castPlayer?.currentPosition ?:0)
        player?.playWhenReady=true
    }
    windowFullscreenView()
}

0 个答案:

没有答案