bmdrm_plugin 0.0.2 copy "bmdrm_plugin: ^0.0.2" to clipboard
bmdrm_plugin: ^0.0.2 copied to clipboard

A Flutter plugin for DRM-protected video playback with watermark support.

example/lib/main.dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:bmdrm_plugin/bmdrm_video_player.dart';
import 'package:bmdrm_plugin/dart_drm_service.dart' as drm_service;

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'BMDRM Player',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: const MyHomePage(),
    );
  }
}

enum DrmMethod {
  drmMinimal,
  drmWithSession,
  drmFromBackendSession,
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  DrmMethod _selectedMethod = DrmMethod.drmMinimal;
  
  // TODO: Replace these with your actual credentials from BMDRM
  // Get your API key, userId, and videoId from your BMDRM dashboard
  final String _apiKey = "YOUR_API_KEY_HERE";
  final String _userId = "YOUR_USER_ID_HERE";
  final String _videoId = "YOUR_VIDEO_ID_HERE";

  // TODO: Replace with your actual pre-generated backend session JSON
  // This should be obtained from your backend server
  final String _preGeneratedSessionJson = '''
{
  "edge": "https://your-edge-server.com",
  "videoId": "your-video-id",
  "signature": "your-signature",
  "userId": "your-user-id",
  "subscriptionId": "your-subscription-id",
  "sessionId": "your-session-id",
  "nonce": "your-nonce",
  "date": 1234567890
}
''';

  // TODO: Replace with your actual DRM session data
  // This should be obtained from your DRM service
  final String _drmSessionDataJson = '''
{
  "edgeName": "https://your-edge-server.com",
  "token": "your-session-token",
  "ecdsaKey": "your-ecdsa-key",
  "drmServerUrl": "https://your-license-server.com/license"
}
''';

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    final videoHeight = screenWidth * 9 / 16;

    return Scaffold(
      backgroundColor: Colors.red,

      body: SafeArea(
        child: Column(
          children: [
            // Method selector

            // Player
            Expanded(
              child: Center(
                child: _buildPlayer(videoHeight),
              ),
            ),
          ],
        ),
      ),
    );
  }


  Widget _buildPlayer(double videoHeight) {
    try {
      switch (_selectedMethod) {
        case DrmMethod.drmMinimal:
          return _buildDrmMinimalPlayer(videoHeight);
        case DrmMethod.drmWithSession:
          return _buildDrmWithSessionPlayer(videoHeight);
        case DrmMethod.drmFromBackendSession:
          return _buildDrmFromBackendSessionPlayer(videoHeight);
      }
    } catch (e) {
      return Container(
        width: double.infinity,
        height: videoHeight,
        color: Colors.black,
        child: Center(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Text(
              'Error: $e',
              style: const TextStyle(color: Colors.red),
              textAlign: TextAlign.center,
            ),
          ),
        ),
      );
    }
  }

  Widget _buildDrmMinimalPlayer(double videoHeight) {
    return BmdrmVideoPlayer.drmMinimal(
      apiKey: _apiKey,
      userId: _userId,
      videoId: _videoId,
      watermarkConfig: null,
      width: double.infinity,
      height: videoHeight,
      autoPlay: true,
      onError: (error) {
        debugPrint('DRM Minimal Error: $error');
      },
      onPlay: () {
        debugPrint('DRM Minimal: Video started playing');
      },
      onPause: () {
        debugPrint('DRM Minimal: Video paused');
      },
    );
  }

  Widget _buildDrmWithSessionPlayer(double videoHeight) {
    try {
      final sessionMap = jsonDecode(_drmSessionDataJson);
      final drmSessionData = drm_service.DrmSessionData.fromJson(sessionMap);
      
      return BmdrmVideoPlayer.drmWithSession(
        drmSessionData: drmSessionData,
        watermarkConfig: null,
        width: double.infinity,
        height: videoHeight,
        autoPlay: true,
        onError: (error) {
          debugPrint('DRM With Session Error: $error');
        },
        onPlay: () {
          debugPrint('DRM With Session: Video started playing');
        },
        onPause: () {
          debugPrint('DRM With Session: Video paused');
        },
      );
    } catch (e) {
      return Container(
        width: double.infinity,
        height: videoHeight,
        color: Colors.black,
        child: Center(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Text(
              'Error parsing session data: $e\n\nPlease update _drmSessionDataJson with valid session data.',
              style: const TextStyle(color: Colors.red),
              textAlign: TextAlign.center,
            ),
          ),
        ),
      );
    }
  }

  Widget _buildDrmFromBackendSessionPlayer(double videoHeight) {
    try {
      final sessionMap = jsonDecode(_preGeneratedSessionJson);
      return BmdrmVideoPlayer.drmFromBackendSession(
        backendSession: sessionMap,
        watermarkConfig: null,
        width: double.infinity,
        height: videoHeight,
        autoPlay: true,
        onError: (error) {
          debugPrint('DRM From Backend Session Error: $error');
        },
        onPlay: () {
          debugPrint('DRM From Backend Session: Video started playing');
        },
        onPause: () {
          debugPrint('DRM From Backend Session: Video paused');
        },
      );
    } catch (e) {
      return Container(
        width: double.infinity,
        height: videoHeight,
        color: Colors.black,
        child: Center(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Text(
              'Error parsing backend session: $e',
              style: const TextStyle(color: Colors.red),
              textAlign: TextAlign.center,
            ),
          ),
        ),
      );
    }
  }
}
0
likes
115
points
150
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for DRM-protected video playback with watermark support.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

crypto, flutter, http, plugin_platform_interface, pointycastle

More

Packages that depend on bmdrm_plugin

Packages that implement bmdrm_plugin