flutter - 如何在 dart/flutter 中收听流值
【摘要】
flutter - 如何在 dart/flutter 中收听流值
任何人帮助追踪音频的位置(即)
if(durationtoOne(position==5)){
FlutterToast.Sho...
flutter - 如何在 dart/flutter 中收听流值
任何人帮助追踪音频的位置(即)
if(durationtoOne(position==5)){
FlutterToast.Showtoast(msg:"I am At 5 sec");
}
- 1
- 2
- 3
如果在 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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
代码到此结束
总结:在特定位置需要在位置变化时触发一些函数(即)
if(durationtoOne(position==5)){
FlutterToast.Showtoast(msg:"I am At 5 sec");
}
- 1
- 2
- 3
通过音频播放或应用程序在前台
最佳答案
如果流不是广播流,则您只能收听一次。
请参阅此 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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
文章来源: jianguo.blog.csdn.net,作者:坚果前端の博客,版权归原作者所有,如需转载,请联系作者。
原文链接:jianguo.blog.csdn.net/article/details/120006657
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)