@@ -116,7 +116,23 @@ class MainActivity : ComponentActivity() {
116116 coreConfigJson = CORE_CONFIG_JSON ,
117117 serverPort = SERVER_PORT ,
118118 )
119+
120+ // Event listeners for P2P Media Loader
121+ p2pml.addEventListener(CoreEventMap .OnPeerConnect ) { params ->
122+ // Implement logic to handle peer connection
123+ Log .d(" P2PML" , " Peer connected: ${params.peerId} - ${params.streamType} " )
124+ }
125+
126+ p2pml.addEventListener(CoreEventMap .OnSegmentLoaded ) { params ->
127+ // Implement logic to handle loaded segment
128+ Log .d(" P2PML" , " Segment loaded: ${params.segmentUrl} - ${params.bytesLength} - ${params.downloadSource} " )
129+ }
119130
131+ p2pml.addEventListener(CoreEventMap .OnChunkDownloaded ) { params ->
132+ // Implement logic to handle downloaded chunk
133+ Log .d(" P2PML" , " Chunk downloaded: ${params.bytesLength} - ${params.downloadSource} - ${params.downloadSource} " )
134+ }
135+
120136 // Start P2P Media Loader with the activity context and ExoPlayer instance
121137 p2pml.start(this , exoPlayer)
122138
@@ -190,21 +206,21 @@ public class MainActivity extends AppCompatActivity {
190206 // URL to the media manifest
191207 private static final String MANIFEST_URL = " https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8" ;
192208
193- // JSON configuration for P2P Media Loader
209+ // Port on which the P2P server will run
194210 private static final int SERVER_PORT = 8081 ;
195211
196- // Port on which the P2P server will run
212+ // JSON configuration for P2P Media Loader
197213 private static final String CORE_CONFIG_JSON = " {\" swarmId\" :\" TEST_KOTLIN\" }" ;
198214
199215 private ExoPlayer exoPlayer;
200- private P2PMediaLoader p2pMediaLoader ;
216+ private P2PMediaLoader p2pml ;
201217
202218 private PlayerView playerView;
203219 private ProgressBar loadingIndicator;
204220 private TextView videoTitle;
205221
206222 private void initializePlayback () {
207- String manifestUrl = p2pMediaLoader . getManifestUrl(MANIFEST_URL );
223+ String manifestUrl = p2pml . getManifestUrl(MANIFEST_URL );
208224
209225 DefaultHttpDataSource . Factory dataSourceFactory = new DefaultHttpDataSource .Factory ()
210226 .setConnectTimeoutMs(15000 ) // Set the connection timeout
@@ -254,15 +270,31 @@ public class MainActivity extends AppCompatActivity {
254270 exoPlayer = new ExoPlayer .Builder (this ). build();
255271
256272 // Initialize P2P Media Loader with callbacks
257- p2pMediaLoader = new P2PMediaLoader (
273+ p2pml = new P2PMediaLoader (
258274 this :: initializePlayback,
259275 this :: onP2PReadyErrorCallback,
260276 SERVER_PORT ,
261277 CORE_CONFIG_JSON
262278 );
263279
280+ // Event listeners for P2P Media Loader
281+ p2pml. addEventListener(CoreEventMap . OnPeerConnect , (params) - > {
282+ // Implement logic to handle peer connection
283+ System . out. println(" Peer connected: " + params. getPeerId() + " - " + params. getStreamType());
284+ });
285+
286+ p2pml. addEventListener(CoreEventMap . OnSegmentLoaded , (params) - > {
287+ // Implement logic to handle loaded segment
288+ System . out. println(" Segment loaded: " + params. getSegmentUrl() + " - " + params. getBytesLength() + " - " + params. getDownloadSource());
289+ });
290+
291+ p2pml. addEventListener(CoreEventMap . OnChunkDownloaded , (params) - > {
292+ // Implement logic to handle downloaded chunk
293+ System . out. println(" Chunk downloaded: " + params. getBytesLength() + " - " + params. getDownloadSource() + " - " + params. getDownloadSource());
294+ });
295+
264296 // Start P2P Media Loader with the activity context and ExoPlayer instance
265- p2pMediaLoader . start(this , exoPlayer);
297+ p2pml . start(this , exoPlayer);
266298
267299 // Listener to update UI based on playback state
268300 exoPlayer. addListener(new Player .Listener () {
@@ -284,26 +316,26 @@ public class MainActivity extends AppCompatActivity {
284316 protected void onRestart () {
285317 super . onRestart();
286318 // Disable P2P features when the activity stops
287- if (p2pMediaLoader != null ) {
288- p2pMediaLoader . applyDynamicConfig(" { \" isP2PDisabled\" : false }" );
319+ if (p2pml != null ) {
320+ p2pml . applyDynamicConfig(" { \" isP2PDisabled\" : false }" );
289321 }
290322 }
291323
292324 @Override
293325 protected void onStop () {
294326 super . onStop();
295327 // Re-enable P2P features when the activity restarts
296- if (p2pMediaLoader != null ) {
297- p2pMediaLoader . applyDynamicConfig(" { \" isP2PDisabled\" : true }" );
328+ if (p2pml != null ) {
329+ p2pml . applyDynamicConfig(" { \" isP2PDisabled\" : true }" );
298330 }
299331 }
300332
301333 @Override
302334 protected void onDestroy () {
303335 super . onDestroy();
304336 // Release ExoPlayer resources and stop P2P Media Loader
305- if (p2pMediaLoader != null ) {
306- p2pMediaLoader . stop();
337+ if (p2pml != null ) {
338+ p2pml . stop();
307339 }
308340 if (exoPlayer != null ) {
309341 exoPlayer. release();
0 commit comments