Basic Usage
Import Package
Import IAppPlayer in your Dart file:
dart
import 'package:iapp_player/iapp_player.dart';
Play Single Video
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;
Playlist
dart
final result = await IAppPlayerConfig.createPlayer(
urls: [
'https://example.com/video1.mp4',
'https://example.com/video2.mp4',
'https://example.com/video3.mp4',
],
eventListener: (event) => print('Event: ${event.iappPlayerEventType}'),
);
Music Player (with Lyrics)
dart
// 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}');
}
},
);
Resource Release
Important: To avoid memory leaks, you must properly release player resources when the page is destroyed. Here is the recommended release method:
dart
Future _releasePlayer() async {
try {
// 1. Clear player cache
IAppPlayerConfig.clearAllCaches();
if (_playerController != null) {
try {
// 2. Remove event listener
_playerController!.removeEventsListener(_videoListener);
// 3. If playing, pause and mute first
if (_playerController!.isPlaying() ?? false) {
await _playerController!.pause();
await _playerController!.setVolume(0);
}
// 4. Release player resources
await Future.microtask(() {
_playerController!.dispose(forceDispose: true);
});
_playerController = null;
} catch (e) {
print('Player cleanup failed: $e');
_playerController = null;
}
}
} catch (e) {
print('Player cleanup failed: $e');
_playerController = null;
}
}
@override
void dispose() {
_releasePlayer();
super.dispose();
}
Advanced Features
💡 For more advanced features and detailed configuration, please see:
🎬 Player usage sample code - Use the player with sample code
📚 Complete API Parameter Documentation - Contains detailed explanations of all parameters
🏆 Advanced Function Documentation - Explore the advanced features of IAppPlayer
Main Advanced Features
- Multi-language Subtitles - Support for multiple subtitle formats and language switching
- DRM Protection - Support for Widevine, FairPlay and other DRM solutions
- Cache Control - Intelligent caching strategies and cache management
- Network Configuration - HTTP header settings and network optimization
- Playback Control - Precise playback control and event listening
- UI Customization - Fully customizable player interface
- Picture-in-Picture Mode - Support for Android and iOS PiP
- Notification Integration - Media notifications and lock screen controls