Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In IOS audio is not getting Played #1807

Open
AnkitKh095 opened this issue May 27, 2024 · 2 comments
Open

In IOS audio is not getting Played #1807

AnkitKh095 opened this issue May 27, 2024 · 2 comments

Comments

@AnkitKh095
Copy link

AnkitKh095 commented May 27, 2024

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.

@AnkitKh095 AnkitKh095 changed the title IOS I am not audio is not getting Played In IOS audio is not getting Played May 27, 2024
@Swaakk
Copy link

Swaakk commented May 28, 2024

//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
Copy link

Harishwarrior commented Jun 27, 2024

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('''))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants