Advanced Examples

Complex examples showcasing advanced features of IAppPlayer.

Note: For complete API documentation and detailed examples, please refer to the API Reference section.

🎯 7. Advanced Playlist

dart
// Create data source list
final dataSources = [
  IAppPlayerConfig.createDataSource(
    url: 'https://example.com/video1.mp4',
    liveStream: false,
    title: 'Video 1',
    subtitleUrl: 'https://example.com/sub1.srt',
    headers: {'Authorization': 'Bearer token1'},
  ),
  IAppPlayerConfig.createDataSource(
    url: 'https://example.com/live.m3u8',
    liveStream: true,
    title: 'Live Stream',
  ),
  IAppPlayerConfig.createDataSource(
    url: 'https://example.com/drm_video.mpd',
    liveStream: false,
    title: 'DRM Protected Video',
    drmConfiguration: IAppPlayerDrmConfiguration(
      drmType: IAppPlayerDrmType.widevine,
      licenseUrl: 'https://example.com/license',
    ),
  ),
];

// Create advanced playlist
final result = IAppPlayerConfig.createPlaylistPlayer(
  eventListener: (event) {},
  dataSources: dataSources,
  shuffleMode: false,
  loopVideos: true,
  initialStartIndex: 0,
);

🔄 8. Dynamic Source Switching

dart
// Initial player
final result = await IAppPlayerConfig.createPlayer(
  url: 'https://example.com/video1.mp4',
  eventListener: (event) {},
);

// Switch to new video later
await IAppPlayerConfig.playSource(
  controller: result.activeController!,
  source: 'https://example.com/video2.mp4',
  title: 'New Video',
  subtitleUrl: 'https://example.com/new_sub.srt',
);

// Preload next video (without playing)
await IAppPlayerConfig.playSource(
  controller: result.activeController!,
  source: 'https://example.com/next_video.mp4',
  preloadOnly: true,  // Preload only
);

👂 9. Complete Event Handling Example

dart
eventListener: (IAppPlayerEvent event) {
  switch (event.iappPlayerEventType) {
    case IAppPlayerEventType.initialized:
      print('Player initialized');
      final duration = result.activeController?.videoPlayerController?.value.duration;
      print('Total duration: ${duration?.inSeconds} seconds');
      break;
      
    case IAppPlayerEventType.play:
      print('Started playing');
      break;
      
    case IAppPlayerEventType.pause:
      print('Paused');
      break;
      
    case IAppPlayerEventType.progress:
      final progress = event.parameters?['progress'] as Duration?;
      final duration = event.parameters?['duration'] as Duration?;
      if (progress != null && duration != null) {
        final percent = (progress.inSeconds / duration.inSeconds * 100).toStringAsFixed(1);
        print('Progress: $percent%');
      }
      break;
      
    case IAppPlayerEventType.bufferingStart:
      print('Buffering...');
      break;
      
    case IAppPlayerEventType.bufferingEnd:
      print('Buffering complete');
      break;
      
    case IAppPlayerEventType.finished:
      print('Playback finished');
      break;
      
    case IAppPlayerEventType.exception:
      final error = event.parameters?['exception'];
      print('Playback error: $error');
      break;
      
    case IAppPlayerEventType.openFullscreen:
      print('Entered fullscreen');
      break;
      
    case IAppPlayerEventType.hideFullscreen:
      print('Exited fullscreen');
      break;
      
    case IAppPlayerEventType.changedPlaylistItem:
      final index = event.parameters?['index'] as int?;
      print('Switched to playlist item ${index! + 1}');
      break;
      
    case IAppPlayerEventType.changedPlaylistShuffle:
      final shuffleMode = event.parameters?['shuffleMode'] as bool?;
      print('Shuffle mode: ${shuffleMode ? "On" : "Off"}');
      break;
      
    default:
      break;
  }
}
Note: For a complete list of all event types and their parameters, see API Reference → Event System

Advanced Tips

  • Use data sources for complex playlist configurations
  • Implement proper error handling and retry logic
  • Monitor network status for better user experience
  • Custom controls provide complete UI flexibility
  • Always dispose controllers to avoid memory leaks

Related Documentation

Need help troubleshooting issues?

View Troubleshooting Guide