npaw_forge 9.5.3
npaw_forge: ^9.5.3 copied to clipboard
NPAW Forge analytics for Flutter: the shared native Forge Rust core over Dart FFI on Android/iOS, the Forge Web runtime on Flutter Web, and typed video, ad, and session reporting.
example/lib/main.dart
// Copyright (C) NPAW - All Rights Reserved
//
// This source code is protected under international copyright law. All rights
// reserved and protected by the copyright holders. This file is confidential and
// only available to authorized individuals with the permission of the copyright
// holders. If you encounter this file and do not have permission, please contact
// the copyright holders and delete this file.
//
//
// @author: Victor Soto <victor.soto@nicepeopleatwork.com>
// @description: NPAW Forge Flutter example app playing a sample stream with automatic analytics.
import 'package:flutter/material.dart';
import 'package:npaw_forge/npaw_forge.dart';
import 'package:npaw_forge_video_player/npaw_forge_video_player.dart';
import 'package:video_player/video_player.dart';
/// Demo account; override with --dart-define=FORGE_ACCOUNT_CODE=yourAccount.
const String accountCode = String.fromEnvironment(
'FORGE_ACCOUNT_CODE',
defaultValue: 'npawdemodev',
);
const String sampleStream = String.fromEnvironment(
'FORGE_SAMPLE_STREAM',
defaultValue:
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
);
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final forge = await NpawForge.start(
const ForgeConfig(
accountCode: accountCode,
appName: 'npaw-forge-flutter-example',
logLevel: 'info',
),
);
WidgetsBinding.instance.addObserver(ForgeLifecycleObserver(forge));
runApp(ForgeExampleApp(forge: forge));
}
class ForgeExampleApp extends StatelessWidget {
const ForgeExampleApp({required this.forge, super.key});
final NpawForge forge;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'NPAW Forge Example',
navigatorObservers: [ForgeRouteObserver(forge)],
theme: ThemeData(colorSchemeSeed: const Color(0xFF2E5BFF)),
home: PlayerScreen(forge: forge),
);
}
}
class PlayerScreen extends StatefulWidget {
const PlayerScreen({required this.forge, super.key});
final NpawForge forge;
@override
State<PlayerScreen> createState() => _PlayerScreenState();
}
class _PlayerScreenState extends State<PlayerScreen> {
late final VideoPlayerController _controller;
ForgeVideoPlayerAttachment? _attachment;
String _status = 'initializing';
@override
void initState() {
super.initState();
_controller = VideoPlayerController.networkUrl(Uri.parse(sampleStream));
_setUp();
}
Future<void> _setUp() async {
_attachment = await attachVideoPlayer(
widget.forge,
_controller,
metadata: const ContentMetadata(
title: 'Big Buck Bunny',
contentId: 'bbb-mp4',
isLive: false,
),
);
await _controller.initialize();
await _controller.play();
if (mounted) {
setState(() {
_status = 'playing (forge ${widget.forge.nativeVersion ?? 'web'})';
});
}
}
@override
void dispose() {
// Detach analytics before the controller goes away.
_attachment?.dispose();
_controller.dispose();
super.dispose();
}
Future<void> _seekBy(Duration delta) async {
final position = await _controller.position;
if (position != null) {
await _controller.seekTo(position + delta);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('NPAW Forge Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AspectRatio(
aspectRatio: _controller.value.isInitialized
? _controller.value.aspectRatio
: 16 / 9,
child: VideoPlayer(_controller),
),
const SizedBox(height: 12),
Text(_status),
const SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: const Icon(Icons.play_arrow),
onPressed: () => _controller.play(),
),
IconButton(
icon: const Icon(Icons.pause),
onPressed: () => _controller.pause(),
),
IconButton(
icon: const Icon(Icons.replay_10),
onPressed: () => _seekBy(const Duration(seconds: -10)),
),
IconButton(
icon: const Icon(Icons.forward_10),
onPressed: () => _seekBy(const Duration(seconds: 10)),
),
],
),
],
),
),
);
}
}