任何人都可以帮助跟踪音频的位置(即)

   if(durationtoOne(position==5)){
FlutterToast.Showtoast(msg:"I am At 5 sec");
}

如果在initstate中添加错误,我被卡在哪里添加此代码,
我想在音频平台中收听位置

代码从这里开始
  import 'dart:async';
    import 'package:assets_audio_player/assets_audio_player.dart';
    import 'package:flutter/material.dart';
    import 'package:fluttertoast/fluttertoast.dart';

    class SmartMantra extends StatefulWidget {
    @override
  _SmartMantraState createState() => _SmartMantraState();
}

class _SmartMantraState extends State<SmartMantra> {
  StreamSubscription _positionSubscription;
  Duration position;
  AssetsAudioPlayer _assetsAudioPlayer;


  stream() {
    _positionSubscription = _assetsAudioPlayer.currentPosition
        .listen((p) => setState(() => position = p),);
  }

  @override
  void initState() {
    _assetsAudioPlayer.open("assets/shivamantra.mp3");
    stream();
    _assetsAudioPlayer.finished.listen((finished) {
      print(finished);
//      print(count);
    });
    super.initState();
  }

  @override
  void dispose() {
    _positionSubscription.cancel();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
          children: <Widget>[
            SizedBox(
              height: 70,
            ),
            Center(
              child: Text(
               durationToone(position).toString(),
                style: TextStyle(color: Colors.black, fontSize: 12),
              ),
            ),
            //getTextContainer()
          ],
        ));
  }
int durationToone(Duration duration) {
  int twoDigits(int n) {
    if (n >= 10) return n;
    return n;
  }

  int twoDigitSeconds =
      twoDigits(duration.inSeconds.remainder(Duration.secondsPerMinute));
  return twoDigitSeconds;
}
}

代码到此结束

摘要:在特定的位置需要在位置更改时触发某些功能(即)
if(durationtoOne(position==5)){
FlutterToast.Showtoast(msg:"I am At 5 sec");
}

整个音频播放或应用处于前台

最佳答案

如果流不是广播流,则只能收听一次。

请引用此Medium post以了解有关Streams的更多信息。

收听stream时,需要在_assetsAudioPlayer.currentPosition函数中添加代码。

import 'dart:async';
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class SmartMantra extends StatefulWidget {
    @override
    _SmartMantraState createState() => _SmartMantraState();
}

class _SmartMantraState extends State<SmartMantra> {
    StreamSubscription _positionSubscription;
    Duration position;
    AssetsAudioPlayer _assetsAudioPlayer;

    stream() {
        _positionSubscription = _assetsAudioPlayer.currentPosition
            .listen((p) {
                setState(() => position = p));
                // You should add your code here
                if(durationtoOne(position==5)){
                    FlutterToast.Showtoast(msg:"I am At 5 sec");
                }
            }
    }

    @override
    void initState() {
        _assetsAudioPlayer.open("assets/shivamantra.mp3");
        stream();
        _assetsAudioPlayer.finished.listen((finished) {
            print(finished);
//          print(count);
        });
        super.initState();
    }

    @override
    void dispose() {
        _positionSubscription.cancel();
        super.dispose();
    }

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            body: Column(
                children: <Widget>[
                SizedBox(
                    height: 70,
                ),
                Center(
                    child: Text(
                        durationToone(position).toString(),
                        style: TextStyle(color: Colors.black, fontSize: 12),
                    ),
                ),
                //getTextContainer()
            ],
        ));
    }
    int durationToone(Duration duration) {
        int twoDigits(int n) {
            if (n >= 10) return n;
            return n;
        }

        int twoDigitSeconds =
            twoDigits(duration.inSeconds.remainder(Duration.secondsPerMinute));
        return twoDigitSeconds;
    }
}

关于flutter - 如何在Dart/Flutter中收听流值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60648783/

10-09 15:53