3

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

   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");
}

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

4

1 回答 1

1

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

请参阅此Medium 帖子以了解有关 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;
    }
}
于 2020-03-12T08:45:41.720 回答