🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / FLUTTER / video player kullanımı

1-) FLUTTER - video player kullanımı

 

kaynak : https://pub.dev/packages/video_player

 

EKRAN GÖRÜNTÜSÜ

 

 

 

flutter pub add video_player

 

1-) videoapp.dart

 

 

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoApp extends StatefulWidget {
  
const VideoApp({Key? key}) : super(key: key);

  
@override
  
_VideoAppState createState() => _VideoAppState();
}

class _VideoAppState extends State<VideoApp> {
  
late VideoPlayerController _controller;

  
@override
  
void initState() {
    
super.initState();
    
_controller = VideoPlayerController.network(
        
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4')
      ..initialize().then((_) {
        
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        
setState(() {});
      });
  }

  
@override
  
Widget build(BuildContext context) {
    
return Scaffold(
      body:
Center(
        child:
_controller.value.isInitialized
            
? AspectRatio(
          aspectRatio:
_controller.value.aspectRatio,
          child:
VideoPlayer(_controller),
        )
            :
Container(),
      ),
      floatingActionButton:
FloatingActionButton(
        onPressed: () {
          setState(() {
            
_controller.value.isPlaying
                
? _controller.pause()
                :
_controller.play();
          });
        },
        child:
Icon(
          
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }

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

 

 

 

2-) KULLANIMI main.dart

 

 

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

  
@override
  
Widget build(BuildContext context) {
    
return MaterialApp(
      title:
'Ders2',
      debugShowCheckedModeBanner:
false,
      theme:
ThemeData(
        primarySwatch:
Colors.blue,
      ),
      home:
VideoApp(),

    );
  }
}

 

 

 

 

 

3-) videoapp.dart KAPSAMLI

 

LİST İÇİN : https://github.com/brownsoo/flutter_video_list_sample

 

kaynak : https://stackoverflow.com/questions/57584726/how-can-i-detect-the-progressinitialize-end-of-video-player-in-flutter

 

 

eventları dinleme : addListener
başa sarma için :
_controller.setLooping(false);

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';


class VideoPlayerScreen extends StatefulWidget {
   
VideoPlayerScreen({super.key});

  
@override
  
State<VideoPlayerScreen> createState() => _VideoPlayerScreenState();
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  
late VideoPlayerController _controller;
  
late Future<void> _initializeVideoPlayerFuture;

  
@override
  
void initState() {
    
super.initState();

    
// Create and store the VideoPlayerController. The VideoPlayerController
    // offers several different ructors to play videos from assets, files,
    // or the internet.
    
_controller = VideoPlayerController.network(
      
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4',
    );

    
// Initialize the controller and store the Future for later use.
    
_initializeVideoPlayerFuture = _controller.initialize();

    
// Use the controller to loop the video.
    
_controller.setLooping(false);

    
_controller.addListener(checkVideo);

  }

  
void checkVideo(){
    
// Implement your calls inside these conditions' bodies :
    
if(_controller.value.position == Duration(seconds: 0, minutes: 0, hours: 0)) {
      print(
'video Started');
    }

    
if(_controller.value.position == _controller.value.duration) {
      print(
'video Ended');
      setState(() {
        
      });
    }

  }

  
@override
  
void dispose() {
    
// Ensure disposing of the VideoPlayerController to free up resources.
    
_controller.dispose();

    
super.dispose();
  }

  
@override
  
Widget build(BuildContext context) {
    
return Scaffold(
      appBar:
AppBar(
        title:  
Text('Butterfly Video'),
      ),
      
// Use a FutureBuilder to display a loading spinner while waiting for the
      // VideoPlayerController to finish initializing.
      
body: FutureBuilder(
        future:
_initializeVideoPlayerFuture,
        builder: (context, snapshot) {
          
if (snapshot.connectionState == ConnectionState.done) {
            
// If the VideoPlayerController has finished initialization, use
            // the data it provides to limit the aspect ratio of the video.
print("baş1");
            
return AspectRatio(
              aspectRatio:
_controller.value.aspectRatio,
              
// Use the VideoPlayer widget to display the video.
              
child: VideoPlayer(_controller),
            );
          }
else {
            
// If the VideoPlayerController is still initializing, show a
            // loading spinner.
            
return  Center(
              child:
CircularProgressIndicator(),
            );
          }
        },
      ),
      floatingActionButton:
FloatingActionButton(
        onPressed: () {
          
// Wrap the play or pause in a call to `setState`. This ensures the
          // correct icon is shown.
          
setState(() {
            
// If the video is playing, pause it.
            
if (_controller.value.isPlaying) {
              
_controller.pause();
            }
else {
              
// If the video is paused, play it.
              
_controller.play();
            }
          });
        },
        
// Display the correct icon depending on the state of the player.
        
child: Icon(
          
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }
}

 

 

 

 2023 Mart 23 Perşembe
 408