I have a question related to video stream. For example, the video is played online, during playback, is it necessary to check the connection to the network every second? If so, how. Because I only worked with requests to get a JSON string. Or generally how to build a video playback structure?
- Why do you need to check the connection? The decision depends on it. When playing, in general, the connection does not make sense to check - the video itself either comes, or it does not. - tse
- For example, in the YouTube application during playback, if the connection is broken, then the progressBar pops up. So is it checked or not? - DevOma
- Most likely, when a connection is broken, an expulsion is thrown, or some method is called up via a callback, and this is what you do. - temq
- That's the thing. How is disconnection checked? - DevOma
- In each case, we must look separately. Once again, it’s not necessary to check the network status on the phone, but to correctly handle the exceptions or callbacks that this or that library provides. - temq
2 answers
You need a Broadcast receiver, which will notify your application every time the network status changes.
public class NetworkReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) { Log.d("Network", "Соединение есть"); } else if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.DISCONNECTED) { Log.d("Network", "Соединение оборванно"); } } } } And do not forget to register in the manifest
<receiver android:name=".receivers.NetworkReceiver"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> - Thanks, not checked. but thought understood! - DevOma
- It makes no sense. Such a solution will not notice network problems beyond the first hop. - Pavel Mayorov
Perhaps you really do not need information about whether or not the server is available, but information about whether the data player is enough or not enough to work. If the server fell off for a while, but the buffer was enough, then the user does not care. The user will be important if the server is available, but the speed is not enough.
Look in the library for the player kolbek, which returns the status. If you use ExoPlayer (I hope you are using it, not standard?), Then in the ExoPlayer.EventListener callback , the onPlayerStateChanged () method can return STATE_BUFFERING status, upon receipt of which you can show the progress bar to the user.
- thank. The information was very helpful. Yeah, I realized that I need information about the data in the buffer. I didn't know about the library - DevOma
- And what do you think about the Vitamio library, where I read about it, but I didn’t quite understand what it was doing - DevOma
- Vitamio not to use. And, in my opinion, it costs money, not? - tse
- Thank. I don't know - DevOma