Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何停止本地音乐的音频播放器插件在颤振?_Flutter_Dart - Fatal编程技术网

Flutter 如何停止本地音乐的音频播放器插件在颤振?

Flutter 如何停止本地音乐的音频播放器插件在颤振?,flutter,dart,Flutter,Dart,我已经为打电话实施了本地音乐 所以我用了插件。 首先,我在我的项目中找到了资产文件夹中的音乐,并在pubspec.yaml中定义了路径。 现在无法在按下结束呼叫按钮时停止音乐 一段代码: const ringingAudioPath = "phone_ringing_sound.mp3"; AudioCache player = new AudioCache(); //On Calling: RawMaterialButton(

我已经为打电话实施了本地音乐 所以我用了插件。 首先,我在我的项目中找到了资产文件夹中的音乐,并在pubspec.yaml中定义了路径。 现在无法在按下结束呼叫按钮时停止音乐

一段代码:

const ringingAudioPath = "phone_ringing_sound.mp3";
AudioCache player = new AudioCache();

//On Calling:


 RawMaterialButton(
            
            onPressed: () async{
              //player.play(ringingAudioPath);
              player.loop(ringingAudioPath);
            } ,
            child: Icon(
              Icons.call,
              color: Colors.blue,
              size: 35.0,
            ),
            shape: CircleBorder(),
            elevation: 2.0,
            fillColor: Colors.white,
            padding: const EdgeInsets.all(15.0),
          ),

//call end or cancel

 RawMaterialButton(
            onPressed: () async{
              // player.clear(ringingAudioPath);
              
            } ,
            child: Icon(
              Icons.call_end,
              color: Colors.white,
              size: 35.0,
            ),
            shape: CircleBorder(),
            elevation: 2.0,
            fillColor: Colors.redAccent,
            padding: const EdgeInsets.all(15.0),
          ),


AudioCache.clear
仅从缓存中清除文件,只需使用wait on
loop()
获取实例,通过使用它,您可以调用
stop()

一个简单的例子是

var ringingAudioPath = "images/ok_scan.mp3";
AudioCache playerCache = new AudioCache(); // you already initialized this

AudioPlayer player = new AudioPlayer();   // now initialize player to stop the audio with player

void _playAudio() async {
  player = await playerCache.loop(ringingAudioPath); // asign your player here
}

void _stopAudio() {
  player?.stop(); // and now u can stop it like
}

玩家可以进行更多的控制。暂停();player.stop();player.resume();不工作,音乐仍然不停,继续演奏。@shrutiramandansharma如果这有帮助,你能把它标记为答案吗?