如何解决出现错误MediaPlayerNative7313:无法创建媒体播放器-Flutter
我正在建立带闹钟的倒计时,我使用audioplayers库作为声音,我尝试在不同时刻播放提示音,所以我尝试了以下代码:
if(timer ==“ 00:50”)player.play(“ bip.wav”)
并且它无法正常工作,当我重新启动应用程序时,声音停止工作,并且出现此错误:
E/MediaPlayerNative( 6964): Unable to create media player
E/AudioplayersPlugin( 6964): Unexpected error!
E/AudioplayersPlugin( 6964): java.lang.RuntimeException: Unable to access resource
E/MediaPlayerNative( 7313): pause called in state 0,mPlayer(0xe8091b60)
E/AudioplayersPlugin( 7313): java.lang.RuntimeException: Unable to access resource
E/AudioplayersPlugin( 7313): at xyz.luan.audioplayers.WrappedMediaPlayer.setSource(WrappedMediaPlayer.java:423)
E/AudioplayersPlugin( 7313): at xyz.luan.audioplayers.WrappedMediaPlayer.setUrl(WrappedMediaPlayer.java:61)
E/AudioplayersPlugin( 7313): at xyz.luan.audioplayers.AudioplayersPlugin.handleMethodCall(AudioplayersPlugin.java:82)
E/AudioplayersPlugin( 7313): at xyz.luan.audioplayers.AudioplayersPlugin.onMethodCall(AudioplayersPlugin.java:60)
E/AudioplayersPlugin( 7313): at io.Flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/AudioplayersPlugin( 7313): at io.Flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/AudioplayersPlugin( 7313): at io.Flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/AudioplayersPlugin( 7313): at android.os.MessageQueue.nativePollOnce(Native Method)
E/AudioplayersPlugin( 7313): at android.os.MessageQueue.next(MessageQueue.java:326)
E/AudioplayersPlugin( 7313): at android.os.Looper.loop(Looper.java:160)
这是我的代码:
import 'package:Flutter/cupertino.dart';
import 'package:audioplayers/audio_cache.dart';
import 'package:Flutter/material.dart';
var player = AudioCache();
class Alarm extends StatefulWidget {
@override
_AlarmState createState() => _AlarmState();
}
class _AlarmState extends State<Alarm> with TickerProviderStateMixin {
AnimationController animationController;
String get timer {
Duration duration =
animationController.duration * animationController.value;
return "${(duration.inMinutes).toString().padLeft(2,'0')}:${(duration.inSeconds % 60).toString().padLeft(2,'0')}";
}
@override
void initState() {
super.initState();
animationController =
AnimationController(
vsync: this,duration: Duration(minutes: 1)
);
}
void Countdown() {
if (animationController.isAnimating) {
animationController.stop();
} else {
animationController.reverse(
from: animationController.value == 0.0
? 1.0
: animationController.value);
};
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: [
Align(
alignment: FractionalOffset.center,child: Column(
mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[
Container(
height: 90,),AnimatedBuilder(
animation: animationController,builder: (_,Widget child) {
**if (timer == "00:50") player.play("bip.wav");**
return Text(timer,style: TextStyle(
fontWeight: FontWeight.w200,color: Colors.white,fontSize: 100.0,)
);
}
),Container(
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,children: <Widget>[
FloatingActionButton(
elevation: 0.0,child: AnimatedBuilder(
animation: animationController,Widget child) {
return Icon(
animationController
.isAnimating
? Icons.pause
: Icons.play_arrow,size: 50,);
}),backgroundColor: Colors.transparent,splashColor: Colors.white,onpressed: () {
Countdown();
setState(() {
Icon(animationController
.isAnimating
? Icons.pause
: Icons.play_arrow);
});
},)
],)
],],);
}
}
谢谢!
解决方法
您应该使用
Timer.periodic( ...)
因此您可以每10分钟播放一次声音。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。