I am working on a Flutter project where I need to play audio through the earpiece (not the speaker) on both iOS and Android devices. Below is a sample of my current implementation:
import 'dart:async';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:legalis/provider/communication/communication_provider.dart';
import 'package:legalis/provider/navigation/navigation_provider.dart';
import 'package:legalis/widgets/call/CallCountDown.dart';
import 'package:legalis/widgets/call_close_review_bottom_sheet.dart';
class CallPage extends StatefulWidget {
CallPage({super.key, required this.callToken});
final String? callToken;
@override
State<CallPage> createState() => _CallPageState();
}
class _CallPageState extends State<CallPage> with TickerProviderStateMixin {
late AnimationController _controller;
final int levelClock = 28;
late AudioPlayer _player;
@override
void initState() {
super.initState();
// Start the countdown timer when the widget is first displayed
final communicationProvider =
Provider.of<CommunicationProvider>(context, listen: false);
communicationProvider.startTimer(Duration(seconds: 70));
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: levelClock),
);
_controller.forward();
_player = AudioPlayer();
_player.setAudioContext(
AudioContext(
android: AudioContextAndroid(isSpeakerphoneOn: false), // Set to false for earpiece
iOS: AudioContextIOS(category: AVAudioSessionCategory.playAndRecord)),
);
}
@override
Widget build(BuildContext context) {
final navigationProvider = Provider.of<NavigationProvider>(context);
return Scaffold(
backgroundColor: const Color(0xFF000E2B),
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
margin: const EdgeInsets.only(top: 30),
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 72,
height: 72,
decoration: ShapeDecoration(
image: DecorationImage(
image: NetworkImage(
'https://media.licdn.com/dms/image/D4E03AQHWd3HiWA1I7g/profile-displayphoto-shrink_800_800/0/1693132754168?e=1723680000&v=beta&t=oyhL1JDIwlEP2yKtVEAvlQ-Z1k-EYM0A0whwep-TWXc',
),
fit: BoxFit.fill,
),
shape: const OvalBorder(),
),
),
const SizedBox(height: 13),
const Text(
'Məhəmməd Məhəmmədov',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 32,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 13),
Countdown(
warning: (p0) {
if (p0) _player.play(AssetSource('voice/call_warning.mp3'));
},
onEnd: (bool isEnd) {
if (isEnd) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
Navigator.pop(context);
navigationProvider.setNavVisible(false);
showModalBottomSheet(
context: context,
backgroundColor: const Color(0xFF173786),
builder: (context) =>
const CallCloseReviewBottomSheet(),
).then(
(value) => navigationProvider.setNavVisible(true),
);
}
});
}
},
key: UniqueKey(),
animation: StepTween(
begin: levelClock, // THIS IS A USER ENTERED NUMBER
end: 0,
).animate(_controller),
),
],
),
),
Container(
width: MediaQuery.of(context).size.width,
height: 93,
decoration: const ShapeDecoration(
color: Color(0xFF333E55),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
GestureDetector(
child: SvgPicture.asset('assets/icons/volume-high.svg'),
),
GestureDetector(
child: SvgPicture.asset('assets/icons/microphone-slash.svg'),
),
Consumer<CommunicationProvider>(
builder: (context, provider, child) {
if (!provider.isConnected) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
Navigator.pop(context);
navigationProvider.setNavVisible(false);
showModalBottomSheet(
context: context,
backgroundColor: const Color(0xFF173786),
builder: (context) =>
const CallCloseReviewBottomSheet(),
).then(
(value) => navigationProvider.setNavVisible(true),
);
}
});
}
return GestureDetector(
onTap: () {
Navigator.pop(context);
navigationProvider.setNavVisible(false);
showModalBottomSheet(
context: context,
backgroundColor: const Color(0xFF173786),
builder: (context) =>
const CallCloseReviewBottomSheet(),
).then(
(value) => navigationProvider.setNavVisible(true),
);
},
child: SvgPicture.asset('assets/icons/Frame 230.svg'),
);
},
),
],
),
),
],
),
),
);
}
@override
void dispose() {
_controller.dispose();
_player.dispose();
super.dispose();
}
}
have set the isSpeakerphoneOn
property to false
for Android and the category
to AVAudioSessionCategory.playAndRecord
for iOS, but the audio still plays through the speaker. How can I ensure the audio plays through the earpiece on both platforms?