我有一个带有音频播放器的应用程序,我触发了 25% 、 50% 、 ... 的进度以将 http 请求 POST 发送到后端以继续跟踪用户的进度,当用户在音频播放器页面中时它工作正常但是当屏幕被锁定时不起作用。
我使用Assets_audio_player播放音频和显示通知。
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class AudioPlayerPage extends StatefulWidget {
@override
_AudioPlayerPageState createState() => _AudioPlayerPageState();
}
class _AudioPlayerPageState extends State<AudioPlayerPage>{
final assetsAudioPlayer = AssetsAudioPlayer.withId("PLAUYER_198973");
@override
void initState() {
assetsAudioPlayer.currentPosition.listen((p) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
appProvider.setPosition(p);
});
switch ((appProvider.getProgression * 100).round()) {
case 0:
if (!analytics0 && mounted) {
analytics.playContenuAnalytics(context, "0");
setState(() {
analytics0 = true;
});
}
break;
case 25:
if (!analytics25 && mounted ) {
analytics.playContenuAnalytics(context, "25");
setState(() {
analytics25 = true;
});
}
break;
case 50:
if (!analytics50 && mounted) {
analytics.playContenuAnalytics(context, "50");
setState(() {
analytics50 = true;
});
}
break;
case 75:
if (!analytics75 && mounted) {
analytics.playContenuAnalytics(context, "75");
setState(() {
analytics75 = true;
});
}
break;
case 90:
if (!analytics100 && mounted) {
analytics.playContenuAnalytics(context, "100");
if(audioType =="seance" || audioType == "story") {
game.updateUserProgress(context, 'AUDIO_PLAYED');
}
setState(() {
analytics100 = true;
});
}
break;
}
});
}
@override
Widget build(BuildContext context) {
// PAGE UI
}
}
我需要在屏幕锁定时继续听音频位置并向后端发送Http请求,谢谢你的帮助。