Performance Optimization

Performance optimization recommendations for IAppPlayer.

Playlist Optimization

Preload Strategy

dart
// Preload next video
final playlistController = result.playlistController;
if (playlistController != null) {
  final nextIndex = playlistController.currentDataSourceIndex + 1;
  if (nextIndex < playlistController.dataSourceList.length) {
    final nextDataSource = playlistController.dataSourceList[nextIndex];
    
    // Use playSource's preload feature
    await IAppPlayerConfig.playSource(
      controller: result.activeController!,
      source: nextDataSource.uri!,
      liveStream: nextDataSource.liveStream,
      preloadOnly: true,  // Preload only
    );
  }
}

Video Quality Adaptation

dart
// Auto-switch quality based on network conditions
void adaptVideoQuality(double bandwidth) {
  String quality;
  if (bandwidth > 5.0) {
    quality = "1080p";
  } else if (bandwidth > 2.5) {
    quality = "720p";
  } else {
    quality = "360p";
  }
  
  // Switch to appropriate quality
  final controller = result.activeController;
  final resolutions = controller?.iappPlayerDataSource?.resolutions;
  if (resolutions != null && resolutions.containsKey(quality)) {
    final url = resolutions[quality]!;
    controller?.setupDataSource(
      IAppPlayerConfig.createDataSource(
        url: url,
        liveStream: false,
        resolutions: resolutions,
      ),
    );
  }
}

Cache Strategy

dart
// Set different cache strategies based on video type
IAppPlayerCacheConfiguration getCacheConfig(String videoType) {
  switch (videoType) {
    case 'short':  // Short videos
      return IAppPlayerCacheConfiguration(
        useCache: true,
        maxCacheSize: 52428800,  // 50MB
        maxCacheFileSize: 10485760,  // 10MB
      );
    case 'long':  // Long videos
      return IAppPlayerCacheConfiguration(
        useCache: true,
        maxCacheSize: 209715200,  // 200MB
        maxCacheFileSize: 52428800,  // 50MB
      );
    case 'live':  // Live streams
      return IAppPlayerCacheConfiguration(
        useCache: false,  // No caching for live streams
      );
    default:
      return IAppPlayerCacheConfiguration(useCache: true);
  }
}

// Clear cache
void cleanupCache() {
  result.activeController?.clearCache();
  IAppPlayerConfig.clearAllCaches();  // Clear URL format detection cache
}

Memory Management Recommendations

dart
// 1. Release resources promptly
@override
void dispose() {
  playerResult?.activeController?.dispose();
  playerResult?.playlistController?.dispose();
  super.dispose();
}

// 2. Manual resource lifecycle management (complex UI scenarios)
final result = await IAppPlayerConfig.createPlayer(
  url: 'video.mp4',
  autoDispose: false,  // Disable auto-release
  eventListener: (event) {},
);

// Manual release at appropriate time
void manualDispose() {
  result.activeController?.dispose();
}

// 3. Large playlists - use pagination loading
void loadMoreVideos(List newUrls) {
  final dataSources = newUrls.map((url) => 
    IAppPlayerConfig.createDataSource(
      url: url,
      liveStream: false,
    )
  ).toList();
  
  result.playlistController?.setupDataSourceList([
    ...result.playlistController!.dataSourceList,
    ...dataSources,
  ]);
}