Skip to main content

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

Simple video player built on top of the official Flutter package 'video_player' that implements some capabilities to play a video from a raw byte stream using an HTTP server.

example/lib/main.dart

import 'package:dartssh2/dartssh2.dart';
import 'package:flutter/material.dart';
import 'package:sftp_video_player/sftp_video_player.dart';
import 'test_conf.dart' as test_config;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  try {
    final file = await test_config.getTestFile();
    runApp(MyApp(file: file));
  } catch (e) {
    runApp(ErrorApp(error: e.toString()));
  }
}

/// Error screen displayed when SFTP connection fails.
class ErrorApp extends StatelessWidget {
  final String error;

  const ErrorApp({super.key, required this.error});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('SFTP Video Player - Error')),
        body: Center(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Icon(Icons.error_outline, size: 64, color: Colors.red),
                const SizedBox(height: 16),
                const Text(
                  'Failed to connect to SFTP server',
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 8),
                const Text(
                  'Please configure your SFTP credentials in example/lib/test_conf.dart',
                  textAlign: TextAlign.center,
                ),
                const SizedBox(height: 16),
                Text(
                  error,
                  style: const TextStyle(color: Colors.red),
                  textAlign: TextAlign.center,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

/// Main application showing the video player.
class MyApp extends StatefulWidget {
  final SftpFile file;
  const MyApp({super.key, required this.file});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  SftpVideoPlayerController? _controller;
  bool _initialized = false;
  String? _errorMessage;

  @override
  void initState() {
    super.initState();
    debugPrint('Started playing: ${widget.file}');
    _initController();
  }

  Future<void> _initController() async {
    try {
      final fileStat = await widget.file.stat();
      debugPrint(fileStat.toString());

      _controller = SftpVideoPlayerController(
        widget.file,
        videoFormat: test_config.videoFormat,
        serverConfig: test_config.serverConfig,
      );

      _controller!.addListener(() {
        setState(() {});
      });

      await _controller!.initialize();
      if (mounted) {
        setState(() {
          _initialized = true;
        });
        _controller!.play();
      }
    } catch (e) {
      if (mounted) {
        setState(() {
          _errorMessage = e.toString();
        });
      }
    }
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (_errorMessage != null) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(title: const Text('SFTP Video Player - Error')),
          body: Center(
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Icon(Icons.error_outline, size: 64, color: Colors.red),
                  const SizedBox(height: 16),
                  const Text(
                    'Failed to initialize video player',
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                  ),
                  const SizedBox(height: 8),
                  Text(
                    _errorMessage!,
                    style: const TextStyle(color: Colors.red),
                    textAlign: TextAlign.center,
                  ),
                ],
              ),
            ),
          ),
        ),
      );
    }

    if (_initialized) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(title: const Text('SFTP Video Player')),
          backgroundColor: Colors.black,
          body: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                AspectRatio(
                  aspectRatio: _controller!.value.aspectRatio,
                  child: VideoPlayer(_controller!),
                ),
                VideoProgressIndicator(
                  _controller!,
                  allowScrubbing: true,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    IconButton(
                      onPressed: () {
                        if (_controller!.value.isPlaying) {
                          _controller!.pause();
                        } else {
                          _controller!.play();
                        }
                        setState(() {});
                      },
                      color: Colors.white,
                      icon: Icon(_controller!.value.isPlaying
                          ? Icons.pause
                          : Icons.play_arrow),
                    ),
                    IconButton(
                      color: Colors.white,
                      onPressed: () {
                        _controller!.seekTo(Duration.zero);
                        setState(() {});
                      },
                      icon: const Icon(Icons.refresh),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      );
    } else {
      return const MaterialApp(
        home: Scaffold(
          backgroundColor: Colors.black,
          body: Center(
            child: CircularProgressIndicator(),
          ),
        ),
      );
    }
  }
}
4
likes
140
points
328
downloads

Documentation

API reference

Publisher

verified publisherfloriansabate.top

Weekly Downloads

Simple video player built on top of the official Flutter package 'video_player' that implements some capabilities to play a video from a raw byte stream using an HTTP server.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

dartssh2, flutter, shelf, video_player

More

Packages that depend on sftp_video_player