0
import 'dart:html';

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

void main() => runApp(MyApp());

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

class MyHomePage extends StatefulWidget {
  const MyHomePage();

 

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
 
    return Scaffold(
      appBar: AppBar(
          title: Text("widget.title"),
      ),
      body: Center(
         child: Column(
           
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[PlaybackButton()],
        ),
      ),
      
    );
  }
}

class PlaybackButton extends StatefulWidget {
  @override
  _PlaybackButton createState() => _PlaybackButton();
}

class _PlaybackButton extends State<PlaybackButton> {
  final audioPlayer = AudioPlayer();
  bool _isPlaying = false;
  @override
  void initState() {
    super.initState();
    setAudio();
  }

  Future setAudio() async {
    final player = AudioCache(prefix: 'assets/');
    final url = await player.load('beerpour-91033.mp3');
    audioPlayer.setSourceUrl(url.path);
  }

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

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: _isPlaying ? Icon(Icons.stop) : Icon(Icons.play_arrow),
      onPressed: () async {
        if (_isPlaying) {
          await audioPlayer.pause();
        } else {
          await audioPlayer.resume();
        }
        setState(() {
          _isPlaying = !_isPlaying;
        });
      },
    );
  }
}

//why is this not working ?, I am loading sound from assets and only playing and stopping the audio on tap! This is showing no problems while writing in editor but is showing multiple in the time of compilation. ❌ Build failed. Check the logs below

Compile failed

../.pub-cache/hosted/pub.dartlang.org/assets_audio_player-3.0.5/lib/src/assets_audio_player.dart:523:31: Error: Method 'removeObserver' cannot be called on 'WidgetsBinding?' because it is potentially null.

  • 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('packages/flutter/lib/src/widgets/binding.dart').

Try calling using ?. instead.

  WidgetsBinding.instance.removeObserver(_lifecycleObserver!);

                          ^^^^^^^^^^^^^^

../.pub-cache/hosted/pub.dartlang.org/assets_audio_player-3.0.5/lib/src/assets_audio_player.dart:693:31: Error: Method 'addObserver' cannot be called on 'WidgetsBinding?' because it is potentially null.

  • 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('packages/flutter/lib/src/widgets/binding.dart').

Try calling using ?. instead.

  WidgetsBinding.instance.addObserver(_lifecycleObserver!);

                          ^^^^^^^^^^^
bird
  • 1
  • 2

1 Answers1

0

Few things i noticed is the widget binding being null. If you are using flutter v3+ then update the audio player package. In flutter 3+ the widget binding cannot be null. Hence the error. Also for asset audio use audio

_audioCache = AudioCache();
_audioPlayer = await _audioCache.play(widget.url);
Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30