flutter - 如何在 dart/flutter 中收听流值

2021-08-31 10:45:23 浏览数 (1)

任何人帮助追踪音频的位置(即)

代码语言:javascript复制
   if(durationtoOne(position==5)){
FlutterToast.Showtoast(msg:"I am At 5 sec");
}

如果在 initstate 中添加出现错误,我会被困在何处添加此代码,

我想通过音频平台收听位置

代码从这里开始

代码语言:javascript复制
  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;
}
}

代码到此结束

总结:在特定位置需要在位置变化时触发一些函数(即)

代码语言:javascript复制
if(durationtoOne(position==5)){
FlutterToast.Showtoast(msg:"I am At 5 sec");
}

通过音频播放或应用程序在前台

最佳答案

如果流不是广播流,则您只能收听一次。 请参阅此 Medium post 以了解有关 Streams 的更多信息。 收听 stream 时,您需要在 _assetsAudioPlayer.currentPosition 函数中添加您的代码。

代码语言:javascript复制
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;
    }
}

0 人点赞