How to stream audio from Google Drive using Flutter and just_audio package?

I’m trying to stream an audio file from Google Drive in my Flutter app using the just_audio package. Instead of downloading the file, I pass the URL directly to the player. However, every time I try, I encounter errors.

Below is a revised version of my code that uses different function and variable names:

import 'package:just_audio/just_audio.dart';

class StreamAudioWidget extends StatefulWidget {
  final String driveUrl;
  StreamAudioWidget({required this.driveUrl});

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

class _StreamAudioWidgetState extends State<StreamAudioWidget> {
  late AudioPlayer audioPlayer;

  @override
  void initState() {
    super.initState();
    audioPlayer = AudioPlayer();
    _initAudio();
  }

  Future<void> _initAudio() async {
    try {
      await audioPlayer.setUrl(widget.driveUrl);
    } catch (err) {
      print('Failed to load audio: $err');
    }
  }

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: Icon(Icons.play_arrow),
      onPressed: () {
        audioPlayer.play();
      },
    );
  }

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

The error mentions a SocketTimeoutException and some issues with the network connection. Does anyone have suggestions to help troubleshoot this further?

hey mate, mayb google drive link is bein tricky. try: https://drive.google.com/uc?export=view&id=YOUR_FILE_ID and ensure it’s public. if that no work, pre-cache audio before playin. hope this helps!

I’ve encountered similar issues when streaming audio from Google Drive in Flutter using the just_audio package.

Often, the problem stems from how Google Drive handles direct links. In my experience, using the standard sharing URL won’t work well. Instead, you need to generate a direct download link. For instance, by using the URL format: https://drive.google.com/file/d/YOUR_FILE_ID/uc?export=download&id=YOUR_FILE_ID, you can bypass the redirection issues.

Also, ensure that the file’s sharing settings allow public access, like ‘Anyone with the link can view’. If problems persist, consider using a package like http to fetch the file bytes first, then passing them to the audio player. Lastly, double-check your internet connection for stability, as streaming requires a consistent network.

I’ve dealt with this issue before. The problem likely lies in the Google Drive URL you’re using. Google Drive’s standard sharing links don’t work well for direct streaming.

Instead, try modifying your URL to this format:
https://drive.google.com/uc?export=download&id=YOUR_FILE_ID

Replace YOUR_FILE_ID with the actual ID from your Drive link. This should provide a direct download link that the just_audio package can handle.

Also, ensure your file’s sharing settings are set to ‘Anyone with the link can view’. If you’re still having issues, consider implementing a short delay before playing the audio to allow for proper initialization.

Remember to handle potential errors gracefully in your UI to improve user experience.