Search in sources :

Example 1 with P2pEngine

use of com.cdnbye.sdk.P2pEngine in project android-p2p-engine by cdnbye.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // bugly
    // CrashReport.initCrashReport(getApplicationContext(), "e40b652a35", true);
    setContentView(R.layout.activity_main);
    offloadV = findViewById(R.id.offload);
    uploadV = findViewById(R.id.upload);
    peersV = findViewById(R.id.peers);
    connectedV = findViewById(R.id.connected);
    peerIdV = findViewById(R.id.peerId);
    ratioV = findViewById(R.id.ratio);
    playerView = findViewById(R.id.player_view);
    TextView versionV = findViewById(R.id.version);
    versionV.setText("Version: " + P2pEngine.Version);
    P2pConfig config = new P2pConfig.Builder().logEnabled(true).logLevel(LogLevel.DEBUG).p2pEnabled(true).withTag("exoplayer").channelIdPrefix("cdnbye").build();
    // Instantiate P2pEngine,which is a singleton
    P2pEngine engine = P2pEngine.initEngine(getApplicationContext(), "ZMuO5qHZg", config);
    // Recommended while playing living stream
    engine.setPlayStats(new PlayerStatsCallback() {

        @Override
        public long onBufferedDuration() {
            return player.getBufferedPosition() - player.getCurrentPosition();
        }
    });
    engine.addP2pStatisticsListener(new P2pStatisticsListener() {

        @Override
        public void onHttpDownloaded(long value) {
            totalHttpDownloaded += (double) value;
            refreshRatio();
        }

        @Override
        public void onP2pDownloaded(long value) {
            totalP2pDownloaded += (double) value;
            String text = String.format("Offload: %.2fMB", totalP2pDownloaded / 1024);
            offloadV.setText(text);
            refreshRatio();
        }

        @Override
        public void onP2pUploaded(long value) {
            totalP2pUploaded += (double) value;
            String text = String.format("Upload: %.2fMB", totalP2pUploaded / 1024);
            uploadV.setText(text);
        }

        @Override
        public void onPeers(List<String> peers) {
            String text = String.format("Peers: %d", peers.size());
            peersV.setText(text);
        }

        @Override
        public void onServerConnected(boolean connected) {
            String text = String.format("Connected: %s", connected ? "Yes" : "No");
            connectedV.setText(text);
            String text2 = String.format("Peer ID: %s", P2pEngine.getInstance().getPeerId());
            peerIdV.setText(text2);
        }
    });
    startPlay(currentUrl);
    replayBtn = findViewById(R.id.replay);
    switchBtn = findViewById(R.id.switcher);
    vodBtn = findViewById(R.id.vod);
    liveBtn = findViewById(R.id.live);
    replayBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // 清空数据
            clearData();
            if (player != null && player.isPlaying()) {
                player.stop();
            }
            startPlay(currentUrl);
        }
    });
    switchBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (currentUrl.equals(VOD)) {
                currentUrl = LIVE;
            } else {
                currentUrl = VOD;
            }
            // 清空数据
            clearData();
            startPlay(currentUrl);
        }
    });
    vodBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // 清空数据
            clearData();
            currentUrl = VOD;
            startPlay(currentUrl);
        }
    });
    liveBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // 清空数据
            clearData();
            currentUrl = LIVE;
            startPlay(currentUrl);
        }
    });
}
Also used : P2pStatisticsListener(com.cdnbye.sdk.P2pStatisticsListener) P2pEngine(com.cdnbye.sdk.P2pEngine) PlayerStatsCallback(com.cdnbye.sdk.PlayerStatsCallback) P2pConfig(com.cdnbye.sdk.P2pConfig) PlayerView(com.google.android.exoplayer2.ui.PlayerView) TextView(android.widget.TextView) View(android.view.View) TextView(android.widget.TextView)

Aggregations

View (android.view.View)1 TextView (android.widget.TextView)1 P2pConfig (com.cdnbye.sdk.P2pConfig)1 P2pEngine (com.cdnbye.sdk.P2pEngine)1 P2pStatisticsListener (com.cdnbye.sdk.P2pStatisticsListener)1 PlayerStatsCallback (com.cdnbye.sdk.PlayerStatsCallback)1 PlayerView (com.google.android.exoplayer2.ui.PlayerView)1