diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bffad41..63ff417 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ xmlns:tools="http://schemas.android.com/tools" package="com.frank.ffmpeg"> + diff --git a/app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.kt index 81da76e..8631d0b 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.kt @@ -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 } } diff --git a/app/src/main/java/com/frank/ffmpeg/handler/ConnectionReceiver.kt b/app/src/main/java/com/frank/ffmpeg/handler/ConnectionReceiver.kt new file mode 100644 index 0000000..55f7c68 --- /dev/null +++ b/app/src/main/java/com/frank/ffmpeg/handler/ConnectionReceiver.kt @@ -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() + } + + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/frank/ffmpeg/listener/OnNetworkChangeListener.kt b/app/src/main/java/com/frank/ffmpeg/listener/OnNetworkChangeListener.kt new file mode 100644 index 0000000..2d3a6d8 --- /dev/null +++ b/app/src/main/java/com/frank/ffmpeg/listener/OnNetworkChangeListener.kt @@ -0,0 +1,7 @@ +package com.frank.ffmpeg.listener + +interface OnNetworkChangeListener { + + fun onNetworkChange() + +} \ No newline at end of file