Flutter audio plugins are not able to work with my music links

Viewed 317

hopefully someone is facing my issue or can provide a solution. Im using firebase for saving and playing my music. The following streaming links from my database project doesnt work well with the AudioManager (https://pub.dev/packages/audio_manager) or audioplayers plugin (https://pub.dev/packages/audioplayers) and I didnt find any other plugin where its working in a correct way. To be honest it would be great getting it work with the AudioManager plugin. I also tried to make an issue in both plugin provider projects, without any success for now.

The problem im facing: The functions stop function doesnt recognize stopping. Previous and next aswell as PlayPause does only work with delay. In the provided example im using AudioManager plugin.

In the provided example we have a horizontal list and im trying to stop the music when you press the button on the top right. Using the streaming links its not recognizing anything. It ignores stopping the song. Also there is a slight delay in going to the next song.

If you change the audio links to the one with file ending, stopping the songs works perfectly fine, also there is less delay. More important in my case is getting the link source without file endings stopped!

var audioFiles = [
    "https://docs.google.com/uc?export=open&id=1SaJWqfQuHnFtL7uqrzfYG31hzOnqDM3r",
    "https://docs.google.com/uc?export=open&id=1FZkFMjQyWguAl0RMAsYDEZ07c_Qf7gjz",
    "https://docs.google.com/uc?export=open&id=1GqrwQ3eRuiil0p-Na_R1tMAvggp9YrbH",
  ];

If youre using links with file type endings like the following example everything works fine and without delay:

var audioFiles = [
    "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
    "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3",
    "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3"
  ];

code to reproduce:

import 'package:audio_manager/audio_manager.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    // Page selector for tab list
    void _selectPage(int index) {
      print('page index: $index');
      setState(() {
        currentIndex = index;
      });
    }

    // Routes list for tab navigation Android
    final List<Widget> _pages = [
      ScreenA(),
      ScreenB(func: _selectPage),
    ];

    return Scaffold(
      appBar: AppBar(),
      body: _pages[currentIndex],
      bottomNavigationBar: SafeArea(
        child: BottomNavigationBar(
          onTap: _selectPage,
          iconSize: 22,
          currentIndex: currentIndex,
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
              backgroundColor: Theme.of(context).primaryColor,
              icon: Icon(Icons.description),
              label: 'ScreenA',
            ),
            BottomNavigationBarItem(
                backgroundColor: Theme.of(context).primaryColor,
                icon: Icon(Icons.ac_unit_outlined),
                label: 'ScreenB'),
          ],
        ),
      ),
    );
  }
}

class ScreenA extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('HOME'),
    );
  }
}

class ScreenB extends StatefulWidget {
  Function func;
  ScreenB({Key key, @required this.func}) : super(key: key);
  @override
  _ScreenBState createState() => _ScreenBState();
}

var audioFiles = [
    "https://docs.google.com/uc?export=open&id=1SaJWqfQuHnFtL7uqrzfYG31hzOnqDM3r",
    "https://docs.google.com/uc?export=open&id=1FZkFMjQyWguAl0RMAsYDEZ07c_Qf7gjz",
    "https://docs.google.com/uc?export=open&id=1GqrwQ3eRuiil0p-Na_R1tMAvggp9YrbH",
  ];
var audioIndex = 0;

class _ScreenBState extends State<ScreenB> {

  //var _controller = PageController();
  PlayMode playMode = AudioManager.instance.playMode;

  var globalIndex =0;

  @override
  void initState() {
    // TODO: implement initState
    
    List<AudioInfo> _list = [];
    for (var i = 0; i < audioFiles.length; i++) {
      _list.add(AudioInfo(audioFiles[i], coverUrl: '', desc: '', title: ''));
    }

    print(_list);

    AudioManager.instance.audioList = _list;
    //AudioManager.instance.intercepter = true;
    AudioManager.instance.play(auto: true);

    super.initState();
  }


@override
void dispose() {
    // TODO: implement dispose
   // AudioManager.instance.release();
    super.dispose();
  }

  var lastPage =0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(
            icon: Icon(Icons.close),
            onPressed: () {
              AudioManager.instance.stop();
              widget.func(0);
            },
          ),
        ],
      ),
      body: PageView.custom(
          onPageChanged: (page){
            if(page != lastPage){
              if(lastPage < page){
                print('swipe right');
                lastPage = page;
                setState(() {
                //audioIndex = page;
                AudioManager.instance.next();
                });
              } else{
                print('swipe left');
                lastPage = page;
              }
            }
          },
          //controller: _controller,
          physics: PageScrollPhysics(),
          scrollDirection: Axis.horizontal,
          childrenDelegate: SliverChildBuilderDelegate((ctx, pageIndex) =>
              // GestureDetector(
              //     onPanUpdate: (details) {
              //       if (details.delta.dx < 0) {
              //         _controller.nextPage(
              //             duration: Duration(milliseconds: 200),
              //             curve: Curves.easeInOut);

              //         setState(() {
              //           //audioIndex = pageIndex;
              //           AudioManager.instance.next();
              //         });
                      
              //       }
              //     },
                  //child: 
                  Center(
                      child: Container(
                          width: 200,
                          height: 200,
                          color: Colors.red,
                          child: Text(audioFiles[audioIndex]))))),
    );
  }
}
0 Answers
Related