EasyPlayer now supports fast switching of multiple video sources. Let's introduce how to achieve this.
This requirement is usually applied when a client needs to view multiple video sources, such as multiple monitoring scene carousels.
Since the playback side of EasyPlayer has been placed in Fragment, this enables the SDK layer to encapsulate the code of many application layers, which brings convenience to developers as much as possible. For example, when switching between two video sources, only two video sources need to be created. PlayFragment, and then switch the display of these two Fragments, as shown in the code:
public void onSwitchPlayer(View view) {
PlayFragment f = (PlayFragment) getSupportFragmentManager().findFragmentByTag("first");
PlayFragment s = (PlayFragment) getSupportFragmentManager().findFragmentByTag("second");
if (!s.isHidden()){
getSupportFragmentManager().beginTransaction().show(f).commit();
getSupportFragmentManager().beginTransaction().hide(s).commit();
mRenderFragment = f;
}else{
getSupportFragmentManager().beginTransaction().show(s).commit();
getSupportFragmentManager().beginTransaction().hide(f).commit();
mRenderFragment = s;
}
}
Isn't it very simple? You only need to toggle the display state of the two Fragments to toggle the playback.
Some students may want to ask, this actually just switches the currently displayed View, and the video will still be played in the background. Right? This is indeed the case. In order to perform video switching as quickly as possible, so as not to cause video delay, black screen, and freeze during the switching process, we reserved a video in the background and did not release it.
In this way, some students may feel bad, and we also feel bad. So don't worry, then look down to see what happened inside PlayFragment during this process:
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (hidden){
// stop
// stopRending();
if (mStreamRender != null) {
mStreamRender.pause();
}
}else{
if (mStreamRender != null) {
mStreamRender.resume();
}
}
}
As you can see, when a Fragment is hidden, the playback state will actually pause. When it is displayed again, playback will continue.
Another student may have questions. When EasyPlayer plays RTSP, it is basically a live broadcast requirement. In the case of live broadcast, how to pause/continue? We do this:
pause:
public void pause(){
mQueue.clear();
if (mClient != null) {
mClient.pause();
}
mQueue.clear();
}
continue:
public void resume(){
if (mClient != null) {
mClient.resume();
}
}
When suspending, the buffer queue is first called, and then the client's suspend is called; when continuing, the client's continuation is called. How is the internal implementation of the client implemented?
pause:
public void pause() {
if (Looper.myLooper() != Looper.getMainLooper()){
throw new IllegalThreadStateException("please call pause in Main thread!");
}
synchronized (_channelPause) {
_channelPause.add(_channel);
}
paused = 1;
Log.i(TAG,"pause:=" + 1);
h.postDelayed(closeTask, 10000);
}
continue:
public void resume() {
if (Looper.myLooper() != Looper.getMainLooper()){
throw new IllegalThreadStateException("call resume in Main thread!");
}
synchronized (_channelPause) {
_channelPause.remove(_channel);
}
h.removeCallbacks(closeTask);
if (paused == 2){
Log.i(TAG,"resume:=" + 0);
openStream();
}
Log.i(TAG,"resume:=" + 0);
paused = 0;
}
It can be seen that during the pause, inside the EasyRTSPClient, we set a pause flag. When this flag is true, the media data we receive is not called back to the upper layer, but is directly discarded. Is this a waste of traffic? Don't Urgent, we post a task, this task will close the stream transmission after 10 seconds.
When continuing to play, we will set the flag bit to false and cancel the task at the same time. In this way, if you quickly pause and resume within 10 seconds, the stream is actually not disconnected, so the user experience is the best when switching. .And when there is no recovery for a long time, the link will also be closed after a 10-second pause, so as not to consume additional traffic.
In summary, fast switching is achieved. Please see the effect:
About EasyPlayer Streaming Media Player
An elegant, simple, fast android RTSP/RTMP/HLS/HTTP Player. EasyPlayer support RTSP(RTP over TCP/UDP) version & Pro version, cover all kinds of streaming media! EasyPlayer is a refined, efficient and stable streaming media player. It is divided into three versions: RTSP version, RTMP version and Pro version. It supports a variety of streaming media audio and video protocols and file playback. It is widely used in many fields such as education, recording and broadcasting, IPTV and so on!
EasyPlayer:https://github.com/EasyDSS/EasyPlayer
Click the link to join the group【EasyPlayer】: 544917793
Get more information
Email: [email protected]
EasyDarwin Open Source Streaming Server: www.EasyDarwin.org
EasyDSS Commercial Streaming Solutions: www.EasyDSS.com
EasyNVR no plug-in live broadcast solution: www.EasyNVR.com
Copyright © EasyDarwin Team 2012-2017