嗨,我想实现一个隔离来播放资产中的停止音频铃声 我已经创建了隔离,但不知道为什么它不起作用,有人知道我缺少什么,下面是我的示例代码
void audioIsolateEntry() async {
final ReceivePort receivePort = ReceivePort();
IsolateNameServer.registerPortWithName(receivePort.sendPort, 'audio_isolate');
final audioPlayer = AudioPlayer();
receivePort.listen((message) async {
if (message == 'play') {
debugPrint('Playing audio...');
await audioPlayer.setSource(AssetSource('ringtone.mp3'));
await audioPlayer.resume();
} else if (message == 'pause') {
debugPrint('Pausing audio...');
await audioPlayer.pause();
} else if (message == 'stop') {
debugPrint('Stopping audio...');
await audioPlayer.stop();
}
});
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
void playAudio() {
final isolate = IsolateNameServer.lookupPortByName('audio_isolate');
debugPrint('play isolate $isolate');
if (isolate != null) {
debugPrint('playing $isolate');
isolate.send('play');
} else {
debugPrint('Isolate not found!');
}
}
void pauseAudio() async {
final isolate = await IsolateNameServer.lookupPortByName('audio_isolate');
if (isolate != null) {
isolate.send('pause');
} else {
debugPrint('Isolate not found!');
}
}
void stopAudio() async {
final isolate = await IsolateNameServer.lookupPortByName('audio_isolate');
if (isolate != null) {
isolate.send('stop');
} else {
debugPrint('Isolate not found!');
}
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
AudioPlayer? audioPlayer;
@override
void initState() {
super.initState();
register();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Audio Player'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children:[
ElevatedButton(
onPressed: () async {
playAudio();
},
child: const Text('Play'),
),
ElevatedButton(
onPressed: () {
stopAudio();
},
child: const Text('Stop'),
),
],
),
),
),
);
}
void register() {
audioIsolateEntry();
}
}
隔离看起来不错。问题可能出在声音上。 您应该首先播放音频而不是继续播放。
尝试一下:
if (message == 'play') {
debugPrint('Playing audio...');
if (audioPlayer.state == PlayerState.paused ||
audioPlayer.state == PlayerState.stopped) {
await audioPlayer.resume();
} else {
await audioPlayer.play(AssetSource('ringtone.mp3'));
}
}
注意:
pubspec.yaml
文件中正确设置好的,所以我创建了一个工作正常的方法
String portName = 'audio_isolate';
fcmBackgroundHandler() async {
ReceivePort receiver = ReceivePort();
IsolateNameServer.registerPortWithName(receiver.sendPort, portName);
receiver.listen((message) async {
debugPrint('oyeah play $message');
if (message == "play") {
await FlutterRingtonePlayer().play(
looping: true,
fromAsset: 'assets/ringtone.mp3',
);
}
if (message == "stop") {
await FlutterRingtonePlayer().stop();
}
});
}
只需播放即可停止使用
IsolateNameServer.lookupPortByName(portName)?.send("play");
和
IsolateNameServer.lookupPortByName(portName)?.send("stop");
但我认为错误的是 当我在收到通知时使用它 inisde FCM 后台处理程序来播放铃声时,是否需要更多时间来播放 所以在backgroundhandler中使用要小心