ARYAN WATHRE
ARYAN WATHRE

Reputation: 21

flutter: Continuous buffering consuming data even when stream is not set to play [just_audio]

Describe the bug I am taking the example given here https://pub.dev/packages/just_audio#-example-tab- I am having a Live Stream audio, as soon as the UI is loaded, the app keeps buffering the stream in background all the time. Hence consuming data. How can we tell the player to only start buffer when play button is pressed. Here is my code

import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
void main() => runApp(RadioTest());

class RadioTest extends StatefulWidget {
  @override
  _RadioTestState createState() => _RadioTestState();
}

class _RadioTestState extends State<RadioTest> {
  AudioPlayer audioPlayer;


  String url = "http://canada1.reliastream.com:8052/live?type=.mp3";

  @override
  void initState() {
    super.initState();
    audioPlayer = AudioPlayer();
    audioPlayer.setUrl(url).catchError((error){
      print(error);
    });
    print("Radio Activated");
  }

  @override
  void dispose() {
    audioPlayer.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Radio Test"),
        ),
        body: Container(
          child: Row(
            children: <Widget>[
              Text("Hi"),
              StreamBuilder<FullAudioPlaybackState>(
                stream: audioPlayer.fullPlaybackStateStream,
                builder: (context, snapshot) {
                  final fullState = snapshot.data;
                  final state = fullState?.state;
                  final buffering = fullState?.buffering;
                  return Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      if (state == AudioPlaybackState.connecting ||
                          buffering == true)
                        Container(
                          margin: EdgeInsets.all(8.0),
                          width: 64.0,
                          height: 64.0,
                          child: CircularProgressIndicator(),
                        )
                      else if (state == AudioPlaybackState.playing)
                        IconButton(
                          icon: Icon(Icons.pause),
                          iconSize: 64.0,
                          onPressed: audioPlayer.pause,
                        )
                      else
                        IconButton(
                          icon: Icon(Icons.play_arrow),
                          iconSize: 64.0,
                          onPressed: audioPlayer.play,
                        ),
                      IconButton(
                        icon: Icon(Icons.stop),
                        iconSize: 64.0,
                        onPressed: state == AudioPlaybackState.stopped ||
                            state == AudioPlaybackState.none
                            ? null
                            : audioPlayer.stop,
                      ),
                    ],
                  );
                },
              ),

            ],
          ),
        ),
      ),
    );
  }
}

Expected behavior It should not buffer upon app opening until play button is pressed.

Smartphone (please complete the following information):

Device: Iphone 11 pro

OS: iOS 13

flutter doctor

[✓] Flutter (Channel stable, 3.0.5, on macOS 12.5 21G72 darwin-arm, locale en-IN)
    • Flutter version 3.0.5 at /Users/aryanwathre/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f1875d570e (6 weeks ago), 2022-07-13 11:24:16 -0700
    • Engine revision e85ea0e79c
    • Dart version 2.17.6
    • DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0-rc1)
    • Android SDK at /Users/aryanwathre/Library/Android/sdk
    • Platform android-32, build-tools 32.0.0-rc1
    • Java binary at: /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home/bin/java
    • Java version Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] VS Code (version 1.70.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.46.0

[✓] VS Code (version 1.69.1)
    • VS Code at /Users/aryanwathre/Desktop/bhavesh/Visual Studio Code.app/Contents
    • Flutter extension version 3.46.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 12.5 21G72 darwin-arm
    • Chrome (web)    • chrome • web-javascript • Google Chrome 104.0.5112.101
    ! Error: Tirth Patel's  iPhone is not connected. Xcode will continue when Tirth Patel's  iPhone is connected. (code -13)

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Upvotes: 2

Views: 927

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63769

Try using setAudioSource

  @override
  void initState() {
    super.initState();
    audioPlayer = AudioPlayer();
    audioPlayer.setAudioSource(AudioSource.uri(Uri(path: url)));
  }
void main() => runApp(MaterialApp(home: RadioTest()));

class RadioTest extends StatefulWidget {
  @override
  _RadioTestState createState() => _RadioTestState();
}

class _RadioTestState extends State<RadioTest> {
  late AudioPlayer audioPlayer;

  String url = "http://canada1.reliastream.com:8052/live?type=.mp3";

  @override
  void initState() {
    super.initState();
    audioPlayer = AudioPlayer();
    audioPlayer.setUrl(url).catchError((error) {
      print(error);
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      floatingActionButton: FloatingActionButton(onPressed: () {
        audioPlayer.play();
      }),
    );
  }
}

And to play call await audioPlayer.play()

Upvotes: 0

Related Questions