privatefuninitPlayerView(playerView: StyledPlayerView) { Log.d(TAG, "initPlayerView: ") player = ExoPlayer.Builder(requireContext()).build() player?.addListener(listener) // Bind the player to the view. playerView.player = player
// back playerView.findViewById<View>(R.id.back).setOnClickListener { fragmentManager?.popBackStack() val ft = fragmentManager?.beginTransaction() ft?.remove(this@ExoPlayerFragment) ft?.commitAllowingStateLoss() } }
privatevar prepareTime: Long = 0L privatefunprintPlayerTimeLine(method: String) { val c = System.currentTimeMillis() val duration = if (prepareTime == 0L) 0else c - prepareTime prepareTime = c Log.i(TAG, "printPlayerTimeLine: method=$method, duration=$duration, uri=${videoUri}") }
privatefunstartPlay() { Log.d(TAG, "startPlay: ") videoUri?.let { // Build the media item. val mediaItem: MediaItem = MediaItem.fromUri(it)
// Set the media item to be played. player?.setMediaItem(mediaItem)
// Prepare the player. player?.prepare() printPlayerTimeLine("player->prepare:")
// Start the playback. player?.play() // calPlayerTimeLine("player->play:") } }
// 该方法需要在View完全被绘制出来之后调用 @JvmStatic privatefunisNavigationBarVisible(activity: Activity): Boolean { val vp = activity.window.decorView as ViewGroup? if (vp != null) { for (i in0 until vp.childCount) { vp.getChildAt(i).context.packageName if (vp.getChildAt(i).id !== View.NO_ID && NAVIGATION == activity.resources.getResourceEntryName(vp.getChildAt(i).id) ) { returntrue } } } returnfalse }
/** * 获取屏幕的物理大小 px */ @JvmStatic fungetDeviceScreenSize(context: Context): Point { val appContext = context.applicationContext val wm = appContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager val point = Point(0, 0) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { wm.defaultDisplay.getRealSize(point) } else { wm.defaultDisplay.getSize(point) } return point }
/** * 获取显示屏幕的宽高 px */ @JvmStatic fungetDisplaySize(context: Context): Point { val point = Point(0, 0) val dm = context.applicationContext.resources.displayMetrics point.x = dm.widthPixels point.y = dm.heightPixels return point } }
val cm = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val currentNetwork = cm.activeNetwork if (currentNetwork != null) { val caps = cm.getNetworkCapabilities(currentNetwork) val linkProperties = cm.getLinkProperties(currentNetwork) }
val cm = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager cm.registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback() { overridefunonAvailable(network: Network) { Log.e(TAG, "The default network is now: " + network) }
overridefunonLost(network: Network) { Log.e(TAG, "The application no longer has a default network. The last default network was " + network) handle(null) }