Basic Examples

Simple examples to help you get started with IAppPlayer quickly.

🎬 1. Simplest Video Playback

dart
// createPlayer returns PlayerResult object
final result = await IAppPlayerConfig.createPlayer(
  url: 'https://example.com/video.mp4',
  eventListener: (event) => print('Event: ${event.iappPlayerEventType}'),
);

// Check if player was created successfully
if (result.activeController == null) {
  print('Player creation failed, please check if URL is valid');
  return;
}

// Use the controller
final controller = result.activeController;

📑 2. Video with Subtitles

dart
final result = await IAppPlayerConfig.createPlayer(
  url: 'https://example.com/video.mp4',
  title: 'Video Title',
  subtitleUrl: 'https://example.com/subtitles.srt',
  eventListener: (event) {
    if (event.iappPlayerEventType == IAppPlayerEventType.initialized) {
      print('Player initialized');
    }
  },
);

🎵 3. Music Player (with LRC Lyrics)

dart
// Single audio file playback - Poster Mode
final squareMusicPlayer = await IAppPlayerConfig.createPlayer(
  url: 'https://example.com/song.mp3',
  title: 'Song Name',
  audioOnly: true,
  aspectRatio: 1.0,  // Poster Mode
  subtitleContent:     '''[00:02.05]愿得一人心
[00:08.64]词:胡小健 曲:罗俊霖
[00:27.48]曾在我背包小小夹层里的那个人''',  // LRC lyrics content
  eventListener: (event) {
    if (event.iappPlayerEventType == IAppPlayerEventType.progress) {
      // Get current lyrics
      final subtitle = result.activeController?.renderedSubtitle;
      if (subtitle != null && subtitle.texts != null) {
        final currentLyric = subtitle.texts!.join(' ');
        print('Current lyrics: $currentLyric');
      }
    }
  },
);

// Compact mode music player
final compactMusicPlayer = await IAppPlayerConfig.createPlayer(
  url: 'https://example.com/song.mp3',
  title: 'Song Name',
  audioOnly: true,
  aspectRatio: 2.0,  // Compact mode (2:1 ratio)
  subtitleContent: lrcContent,
);

// Music playlist
final musicPlaylist = await IAppPlayerConfig.createPlayer(
  urls: [
    'https://example.com/song1.mp3',
    'https://example.com/song2.mp3',
    'https://example.com/song3.mp3',
  ],
  titles: ['Song 1', 'Song 2', 'Song 3'],
  imageUrls: [  // Album covers
    'https://example.com/album1.jpg',
    'https://example.com/album2.jpg',
    'https://example.com/album3.jpg',
  ],
  subtitleContents: [  // LRC lyrics
    '''[00:02.05]May I have your heart
[00:08.64]Lyrics: Hu Xiaojian Music: Luo Junlin''',
    '''[00:00.00]About Love
[00:05.00]Beautiful melody''',
    '''[00:01.00]Third Song
[00:06.00]This is sample lyrics''',
  ],
  audioOnly: true,
  shuffleMode: true,  // Shuffle play
  autoPlay: true,
  eventListener: (event) {
    if (event.iappPlayerEventType == IAppPlayerEventType.changedPlaylistItem) {
      final index = event.parameters?['index'] as int?;
      print('Switched to song ${index! + 1}');
    }
  },
);

📺 4. Live Stream (HLS)

dart
final result = await IAppPlayerConfig.createPlayer(
  url: 'https://example.com/live.m3u8',
  title: 'Live Channel',
  autoPlay: true,
  looping: false,     // No looping for live streams
  liveStream: true,   // Explicitly specify as live stream (usually auto-detected)
  eventListener: (event) {
    if (event.iappPlayerEventType == IAppPlayerEventType.bufferingStart) {
      print('Buffering started');
    } else if (event.iappPlayerEventType == IAppPlayerEventType.bufferingEnd) {
      print('Buffering ended');
    }
  },
);

🔐 5. Authenticated Video

dart
final result = await IAppPlayerConfig.createPlayer(
  url: 'https://example.com/protected.mp4',
  headers: {
    'Authorization': 'Bearer your-token-here',
    'User-Agent': 'MyApp/1.0',
  },
  eventListener: (event) {
    if (event.iappPlayerEventType == IAppPlayerEventType.exception) {
      final error = event.parameters?['exception'];
      print('Playback error: $error');
    }
  },
);

📺 6. TV Mode (Notifications Disabled)

dart
final result = await IAppPlayerConfig.createPlayer(
  url: 'https://example.com/video.mp4',
  isTV: true,  // TV mode, automatically disables notifications
  autoPlay: true,
  eventListener: (event) {},
);

🌟 16. Background Image Usage Examples

dart
// Using network background image
final result1 = await IAppPlayerConfig.createPlayer(
  url: 'https://example.com/video.mp4',
  backgroundImage: 'https://example.com/background.jpg',
  eventListener: (event) {},
);

// Using local asset background image
final result2 = await IAppPlayerConfig.createPlayer(
  url: 'https://example.com/video.mp4',
  backgroundImage: 'assets/images/video_bg.png',
  eventListener: (event) {},
);

Tips

  • Always check if result.activeController is null before use
  • Use preferredDecoderType to handle compatibility issues
  • Live streams automatically detect format based on URL extension
  • TV mode is optimized for large screen devices
  • For URL format detection details, see API Reference

Ready for more complex examples?

View Advanced Examples