add network change listener

dev
xufuji456 3 years ago
parent 9c27d982f7
commit 442d35414e
  1. 1
      app/src/main/AndroidManifest.xml
  2. 36
      app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.kt
  3. 24
      app/src/main/java/com/frank/ffmpeg/handler/ConnectionReceiver.kt
  4. 7
      app/src/main/java/com/frank/ffmpeg/listener/OnNetworkChangeListener.kt

@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.frank.ffmpeg">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

@ -1,7 +1,9 @@
package com.frank.ffmpeg.activity
import android.annotation.SuppressLint
import android.content.IntentFilter
import android.media.AudioFormat
import android.net.ConnectivityManager
import android.os.Bundle
import android.os.Handler
import android.os.Message
@ -14,6 +16,8 @@ import android.widget.Toast
import android.widget.ToggleButton
import com.frank.ffmpeg.R
import com.frank.ffmpeg.handler.ConnectionReceiver
import com.frank.ffmpeg.listener.OnNetworkChangeListener
import com.frank.live.camera2.Camera2Helper
import com.frank.live.listener.LiveStateChangeListener
import com.frank.live.param.AudioParam
@ -25,9 +29,12 @@ import com.frank.live.LivePusherNew
* Created by frank on 2018/1/28.
*/
class LiveActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener, LiveStateChangeListener {
open class LiveActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener, LiveStateChangeListener, OnNetworkChangeListener {
private var textureView: SurfaceView? = null
private var livePusher: LivePusherNew? = null
private var isPushing = false
private var connectionReceiver: ConnectionReceiver? = null
@SuppressLint("HandlerLeak")
private val mHandler = object : Handler() {
override fun handleMessage(msg: Message) {
@ -50,6 +57,7 @@ class LiveActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener, Liv
hideActionBar()
initView()
initPusher()
registerBroadcast(this)
}
private fun initView() {
@ -75,13 +83,22 @@ class LiveActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener, Liv
livePusher!!.setPreviewDisplay(textureView!!.holder)
}
private fun registerBroadcast(networkChangeListener: OnNetworkChangeListener) {
connectionReceiver = ConnectionReceiver(networkChangeListener)
val intentFilter = IntentFilter()
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
registerReceiver(connectionReceiver, intentFilter)
}
override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
when (buttonView.id) {
R.id.btn_live//start or stop living
-> if (isChecked) {
livePusher!!.startPush(LIVE_URL, this)
isPushing = true
} else {
livePusher!!.stopPush()
isPushing = false
}
R.id.btn_mute//mute or not
-> {
@ -101,8 +118,15 @@ class LiveActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener, Liv
override fun onDestroy() {
super.onDestroy()
if (livePusher != null) {
if (isPushing) {
isPushing = false
livePusher?.stopPush()
}
livePusher!!.release()
}
if (connectionReceiver != null) {
unregisterReceiver(connectionReceiver)
}
}
override fun onViewClick(view: View) {
@ -115,10 +139,18 @@ class LiveActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener, Liv
}
override fun onNetworkChange() {
Toast.makeText(this, "network is not available", Toast.LENGTH_SHORT).show()
if (livePusher != null && isPushing) {
livePusher?.stopPush()
isPushing = false
}
}
companion object {
private val TAG = LiveActivity::class.java.simpleName
private const val LIVE_URL = "rtmp://192.168.1.3/live/stream"
private const val LIVE_URL = "rtmp://192.168.31.212/live/stream"
private const val MSG_ERROR = 100
}
}

@ -0,0 +1,24 @@
package com.frank.ffmpeg.handler
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import com.frank.ffmpeg.listener.OnNetworkChangeListener
class ConnectionReceiver(networkChangeListener: OnNetworkChangeListener) : BroadcastReceiver() {
var networkChangeListener :OnNetworkChangeListener ?= networkChangeListener
override fun onReceive(context: Context?, intent: Intent?) {
if (ConnectivityManager.CONNECTIVITY_ACTION == intent?.action) {
val connectivityManager : ConnectivityManager = context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetworkInfo = connectivityManager.activeNetworkInfo
if (activeNetworkInfo == null || !activeNetworkInfo.isAvailable) {
networkChangeListener?.onNetworkChange()
}
}
}
}

@ -0,0 +1,7 @@
package com.frank.ffmpeg.listener
interface OnNetworkChangeListener {
fun onNetworkChange()
}
Loading…
Cancel
Save