Skip to content

Commit

Permalink
[Android]Add media player info display.
Browse files Browse the repository at this point in the history
  • Loading branch information
xuchunzhen committed Jul 17, 2024
1 parent 02cd9a5 commit 33c74cc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -35,7 +36,9 @@
import com.yanzhenjie.permission.runtime.Permission;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.agora.api.example.MainApplication;
import io.agora.api.example.R;
Expand Down Expand Up @@ -85,10 +88,12 @@ public class MediaPlayer extends BaseFragment implements View.OnClickListener, A
private Spinner sp_player_stream, sp_publish_stream;
private List<MediaStreamInfo> mediaStreamInfoList;
private int playerStreamIndex, publishStreamIndex;
private TextView local_video_info;

private boolean joined = false;
private SeekBar progressBar;
private long playerDuration = 0;
private final Map<String, String> videoInfoMap = new LinkedHashMap<>();

private static final String SAMPLE_MOVIE_URL = "https://agora-adc-artifacts.s3.cn-north-1.amazonaws.com.cn/resources/sample.mp4";

Expand Down Expand Up @@ -166,6 +171,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
stop = view.findViewById(R.id.stop);
pause = view.findViewById(R.id.pause);
publish = view.findViewById(R.id.publish);
local_video_info = view.findViewById(R.id.local_video_info);
ll_streams = view.findViewById(R.id.ll_streams);
sp_publish_stream = view.findViewById(R.id.sp_publish_stream);
sp_player_stream = view.findViewById(R.id.sp_player_stream);
Expand Down Expand Up @@ -251,6 +257,7 @@ public void onClick(View v) {
mediaPlayer.stop();
open.setEnabled(false);
setMediaPlayerViewEnable(false);
cleanVideoInfo();
}
} else if (v.getId() == R.id.open) {
String url = et_url.getText().toString();
Expand Down Expand Up @@ -558,6 +565,30 @@ public void onDestroy() {
engine = null;
}

private void updateVideoInfo(String key, String value) {
runOnUIThread(() -> {
videoInfoMap.put(key, value);
StringBuilder sb = new StringBuilder();
int size = videoInfoMap.size();
int index = 0;
for (Map.Entry<String, String> entry : videoInfoMap.entrySet()) {
sb.append(entry.getKey()).append(" : ").append(entry.getValue());
if (index < size - 1) {
sb.append("\n");
}
index++;
}
local_video_info.setText(sb.toString());
});
}

private void cleanVideoInfo() {
runOnUIThread(() -> {
videoInfoMap.clear();
local_video_info.setText("");
});
}

private void setMediaPlayerViewEnable(boolean enable) {
runOnUIThread(() -> {
play.setEnabled(enable);
Expand Down Expand Up @@ -637,7 +668,7 @@ public void onMetaData(io.agora.mediaplayer.Constants.MediaPlayerMetadataType me

@Override
public void onPlayBufferUpdated(long l) {

updateVideoInfo("PlayBuffer", String.valueOf(l));
}

@Override
Expand Down Expand Up @@ -667,7 +698,10 @@ public void onPlayerCacheStats(CacheStatistics stats) {

@Override
public void onPlayerPlaybackStats(PlayerPlaybackStats stats) {

updateVideoInfo("Fps", String.valueOf(stats.getVideoFps()));
updateVideoInfo("videoBitrateInKbps", String.valueOf(stats.getVideoBitrate()));
updateVideoInfo("audioBitrateInKbps", String.valueOf(stats.getAudioBitrate()));
updateVideoInfo("totalBitrateInKbps", String.valueOf(stats.getTotalBitrate()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,23 @@
android:orientation="vertical">

<FrameLayout
android:id="@+id/fl_local"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
android:layout_weight="1.0">

<FrameLayout
android:id="@+id/fl_local"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:id="@+id/local_video_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
tools:text="fps: 15 \nbitrate=100kbps" />

</FrameLayout>

<FrameLayout
android:id="@+id/fl_remote"
Expand Down

0 comments on commit 33c74cc

Please sign in to comment.