bluefireteam / audioplayers

A Flutter package to play multiple audio files simultaneously (Android/iOS/web/Linux/Windows/macOS)
https://pub.dartlang.org/packages/audioplayers
MIT License
1.95k stars 827 forks source link

In IOS audio is not getting Played #1807

Open AnkitKh095 opened 1 month ago

AnkitKh095 commented 1 month ago

I am currently working on implementing audio playback functionality in a Flutter app using the audioplayers plugin. However, I'm encountering an issue where the audio is not playing at all. Here's a simplified version of my code:

dart Copy code import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:audioplayers/audioplayers.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Audio Player Example'), ), body: AudioPlayerWidget(), ), ); } }

class AudioPlayerWidget extends StatefulWidget { @override _AudioPlayerWidgetState createState() => _AudioPlayerWidgetState(); }

class _AudioPlayerWidgetState extends State { AudioPlayer player = AudioPlayer();

@override void initState() { super.initState(); initPlayer(); }

Future initPlayer() async { try { print("Trying to play audio..."); await player.play(AssetSource('sounds/sample.wav')); print("Audio playback started");

  final String result = await platform.invokeMethod('startRecording');
  print("Recording started: $result");

  player.onPlayerStateChanged.listen((event) {
    if (event == PlayerState.playing) {
      print("Player state: playing");
    } else if (event == PlayerState.stopped || event == PlayerState.completed) {
      print("Player state: stopped or completed");
    }
  });

  player.onPlayerError.listen((error) {
    print("Audio player error: $error");
  });
} on PlatformException catch (e) {
  print("Error starting recording: ${e.message}");
}

}

@override void dispose() { player.dispose(); super.dispose(); }

@override Widget build(BuildContext context) { return Container( child: Center( child: Text('Playing audio...'), ), ); } }

This is the code that I am using but it is audio is not getting Played.

Swaakk commented 1 month ago

//Try this Future initPlayer() async { try { print("Trying to play audio..."); await player.setAudioContext(const AudioContext( iOS: AudioContextIOS( category: AVAudioSessionCategory.ambient, options: [ AVAudioSessionOptions.mixWithOthers, ], ), )); player.play(AssetSource('sounds/sample.wav')); print("Audio playback started");

  final String result = await platform.invokeMethod('startRecording');
  print("Recording started: $result");

  player.onPlayerStateChanged.listen((event) {
    if (event == PlayerState.playing) {
      print("Player state: playing");
    } else if (event == PlayerState.stopped || event == PlayerState.completed) {
      print("Player state: stopped or completed");
    }
  });

  player.onPlayerError.listen((error) {
    print("Audio player error: $error");
  }
  );
} on PlatformException catch (e) {
  print("Error starting recording: ${e.message}");
}

}

Harishwarrior commented 3 weeks ago

Using await while playing the audio is the issue. @haarts @cosmok @ktakayama Should it be the void instead of Future<void> in play method?

use unawaited(audioplayer.play('''))