Search code examples
fluttergoogle-drive-apiflutter-webexoplayerflutter-video-player

How to play video from Google Drive in Better Player - Flutter?


In my app, I already integrated with google login and I successfully accessed my google drive folder and video files(mp4). But better player can play public video by modified url like this https://drive.google.com/uc?id=my_video_file_id. I want to develop an app like Nplayer or Kudoplayer.

Can anyone guide me some scenario of Nplayer or Kudoplayer? and guide me how to play private drive's video using better player. Thanks.


Solution

  • Finally I got solution after research for a long time :-)

    Summary answer for this question

    • Don't forget to add headers in BetterPlayerDataSource

    • Don't use this format "https://drive.google.com/uc?export=view&id=${fileId}" and

    • Use this format "https://www.googleapis.com/drive/v3/files/${fileId}?alt=media"


    For guys who face issue like me, please follow these step

    • Enable Drive API
    • Integrate google login in your app with appropriate Scpoe, for me driveReadonlyScope is enough.
    • Retrieve fileId

    Next step that I stuck is

    • Don't forget to add headers in BetterPlayerDataSource
    • Don't use this format "https://drive.google.com/uc?export=view&id=${fileId}" and
    • Use this format "https://www.googleapis.com/drive/v3/files/${fileId}?alt=media"

    Like this

    BetterPlayerDataSource betterPlayerDataSource = BetterPlayerDataSource(
      BetterPlayerDataSourceType.network,
        "https://www.googleapis.com/drive/v3/files/$fileId?alt=media",
        videoExtension: ".mp4",
        headers: getDataFromCache(CacheManagerKey.authHeader),
     );
    

    authHeader can get when you call signWithGoogle function, I save it and retrieve later. In signWithGoogle function, you can get authHeader using this

    Map<String, String> authHeader = await googleSignIn.currentUser!.authHeaders;
    

    Bonus - Queries For You.

    • Get Share With Me Data = > q: "'sharedWithMe = true and (mimeType = 'video/mp4' or mimeType = 'application/vnd.google-apps.folder')"
    • Get Share Drive Data => q: "'shared_drive_id' in parents and trashed=false and (mimeType = 'video/mp4' or mimeType = 'application/vnd.google-apps.folder')",
    • Get My Drive Data => q: "'root' in parents and trashed=false and (mimeType = 'video/mp4' or mimeType = 'application/vnd.google-apps.folder')",