如何停止资产音频播放器(assets_audio_player包),同时导航到下一页



在我的代码中,我使用Asset Audio播放器包来播放背景音乐。但是当我转到下一页后,它仍然继续播放音乐。我该如何在导航到下一页时停止此操作?

------------------ 这是我的代码 ----------------

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:toffeeride_flutterapp/screens/content_screen/content_screen.dart';
import 'package:assets_audio_player/assets_audio_player.dart';
class HomeScreen extends StatefulWidget {
static const routeName = "/home_screen";
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
AssetsAudioPlayer? _assetsAudioPlayer;
@override
void initState() {
super.initState();

_assetsAudioPlayer = AssetsAudioPlayer();
AssetsAudioPlayer.newPlayer().open(
Audio("assets/audio/bgm.mp3"),
autoStart: true,
showNotification: true,
);
_assetsAudioPlayer!.play();
}
@override
void deactivate() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
super.deactivate();
}
@override
void dispose() { 
_assetsAudioPlayer = null;
super.dispose();
}
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]);
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/home/Sky.png'),
fit: BoxFit.cover,
)),
child: Stack(
children: [
Positioned(
left: 300,
child: Image(
height: MediaQuery.of(context).size.height / 5,
image: AssetImage('assets/images/logo/HomeScreenLogo.png')),
),
Positioned(
child: Container(
width: MediaQuery.of(context).size.width,
child: Image(
image: AssetImage('assets/images/home/Hill.png'),
fit: BoxFit.cover,
),
),
),
Positioned(
top: 10,
right: 3,
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Image(
image: AssetImage('assets/images/home/elements.png'),
fit: BoxFit.cover,
),
),
),
Positioned(
top: 20,
left: 20,
child: GestureDetector(
onTap: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => ContentScreen()),
);
},
child: Image(
height: 70,
image: AssetImage(
'assets/images/icons/newicons/Menu_150.png')),
),
),
Positioned(
child: Align(
alignment: Alignment.centerRight,
child: GestureDetector(
onTap: () {
// Navigator.pushReplacement(
//   context,
//   MaterialPageRoute(builder: (context) => ModuleListScreen()),
// );
},
child: Image(
height: 100,
image: AssetImage(
'assets/images/icons/newicons/Play_150.png'),
),
),
),
),
],
)),
);
}
}

在我的代码中,我使用资产音频播放器播放背景音乐。但是当我转到下一页后,它仍然继续播放音乐。我该如何在导航到下一页时停止此操作?

------------------ 这是我的代码 ----------------

首先你应该像这样改变你的initState:

@override
void initState() {
super.initState();
_assetsAudioPlayer = 
AssetsAudioPlayer.newPlayer();
_assetsAudioPlayer!.open(
Audio("assets/audio/bgm.mp3"),
autoStart: false,
showNotification: true,
);
_assetsAudioPlayer!.play();
}

然后在导航代码前添加:

_assetsAudioPlayer!.stop();
onTap: () {
_assetsAudioPlayer!.stop();
Navigator.pushReplacement(context,MaterialPageRoute(builder: 
(context) => ContentScreen()),);
},

不可空的旧颤振版本的工作代码。像这样创建实例:

final assetsAudioPlayer = AssetsAudioPlayer.newPlayer();

播放:

assetsAudioPlayer.open(
Audio("assets/audios/youraudiofile.aac"),
autoStart: true,
showNotification: false,
);
assetsAudioPlayer.play();

停止:

assetsAudioPlayer.stop();

final assetsAudioPlayer = AssetsAudioPlayer.newPlayer();

声明对象时不要使用final,这样它就可以工作了:

assetsAudioPlayer.stop();

相关内容

  • 没有找到相关文章

最新更新