florent37 / Flutter-AssetsAudioPlayer

Play simultaneously music/audio from assets/network/file directly from Flutter, compatible with android / ios / web / macos, displays notifications
https://pub.dartlang.org/packages/assets_audio_player
Apache License 2.0
755 stars 361 forks source link

[iOS] after stop() called or track finished, open() + play() does not work. #419

Open sudachi808 opened 3 years ago

sudachi808 commented 3 years ago
  1. Call open()
  2. Call play()
  3. Track starts to play.
  4. Call stop()
  5. Track stops.
  6. Call open()
  7. Call play()
  8. The track does not start to play.

My codes has something wrong❓🤔

import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {

  AssetsAudioPlayer get _player => AssetsAudioPlayer.withId('music');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                  child: Text('Open'),
                  onPressed: () {
                    print('_/_/ OPEN');
                    _player.open(
                      Audio('assets/sample.mp3'),
                      autoStart: false,
                    );
                  },
                ),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                  child: Text('Play'),
                  onPressed: () {
                    print('_/_/ PLAY');
                    _player.play();
                  },
                ),
                Padding(padding: EdgeInsets.all(4.0)),
                RaisedButton(
                  child: Text('Pause'),
                  onPressed: () {
                    print('_/_/ PAUSE');
                    _player.pause();
                  },
                ),
                Padding(padding: EdgeInsets.all(4.0)),
                RaisedButton(
                  child: Text('Stop'),
                  onPressed: () {
                    print('_/_/ STOP');
                    _player.stop();
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
ghost commented 3 years ago

I'm having the same problem. My code works fine on my Android phone but on my iPhone I'm seeing the same behavior.

ghost commented 3 years ago

I'm looking at the Swift code for the iOS player and noticed that when stop is called it's setting the media player to nil which seems suspect.

https://github.com/florent37/Flutter-AssetsAudioPlayer/blob/master/darwin/Classes/Music.swift#L815

Note that when you pause() in the swift code the player doesn't get set to nil. I don't know the Swift API at all for this functionality so I may be off track here.

siyukok commented 3 years ago

same problem on iPhone

ReversedHourglass commented 3 years ago

Same problem here, only on iPhone, on Android everything seems to work as expected.

The problem is still relevant, to avoid it, i create a new AudioPlayer after stopping the one that was used just before.