flutter - 如何在 dart/flutter 中收听流值
Posted Lucklyの博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flutter - 如何在 dart/flutter 中收听流值相关的知识,希望对你有一定的参考价值。
flutter - 如何在 dart/flutter 中收听流值
任何人帮助追踪音频的位置(即)
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 中收听流值的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:如何在 Flutter 中为 Firebase (FCM) 注册令牌订阅ToTopic
Flutter:如何在 google_maps_flutter 中为标记图标设置动画?
Flutter - 如何在 Flutter 应用上实现 News Count
flutter - 如何在 dart/flutter 中收听流值