Hello,
Welcome to our Microsoft Q&A platform!
You could select the Speaker in iPhone without any code modification. When you click the button to play , tap AirPlay and select the speaker , refer to https://support.apple.com/en-us/HT202809#ios
In other way, you could set AVAudioSessionCategory
. I check the iOS sample of Plugin.AudioRecorder, there is a RequestAVAudioSessionCategory
method, and it can change the play mode, we just need to use AVAudioSessionCategory.Playback
.
// this controls whether the library will attempt to set the shared AVAudioSession category, and then reset it after recording completes
AudioRecorderService.RequestAVAudioSessionCategory (AVAudioSessionCategory.PlayAndRecord);
But I add it to Xamarin.iOS project and it doesn't work. So, I check the source code and find this plugin use native AVFoundation
framework. I try to use DependencyService and AVFoundation
, it works, you could refer to the following code:
Interface in Forms
public interface IAVService
{
void setSessionCategory(bool isSpeaker);
}
iOS implementation with the DependencyService
using AutoReorderiOSDemo;
using AutoReorderiOSDemo.iOS;
using AVFoundation;
using Foundation;
using Plugin.AudioRecorder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIKit;
using Xamarin.Forms;
[assembly: Dependency(typeof(AVService_iOS))]
namespace AutoReorderiOSDemo.iOS
{
public class AVService_iOS : IAVService
{
NSError error;
public void setSessionCategory(bool isSpeaker)
{
AVAudioSession.SharedInstance().SetCategory(isSpeaker == true? AVAudioSession.CategoryPlayback: AVAudioSession.CategoryPlayAndRecord, out error);
}
}
}
Invoke the method in Forms
async Task RecordAudio()
{
try
{
if (!recorder.IsRecording) //Record button clicked
{
if (Device.RuntimePlatform == Device.iOS)
{
DependencyService.Get<IAVService>().setSessionCategory(false);//set CategoryPlayAndRecord
}
......
}
else //Stop button clicked
{
......
}
}
catch (Exception ex)
{
//blow up the app!
throw ex;
}
}
When record the audio, set CategoryPlayAndRecord
, when play the audio set CategoryPlayback
void PlayAudio()
{
try
{
var filePath = recorder.GetAudioFilePath();
if (filePath != null)
{
if (Device.RuntimePlatform == Device.iOS)
{
DependencyService.Get<IAVService>().setSessionCategory(true);
}
player.Play(filePath);
}
}
catch (Exception ex)
{
throw ex;
}
}
----------update--------
In similator, it will get a audioQueue.Start() returned non-OK status: GeneralParamError
, it means there is no microphone. You could try to change the similator settings( I/O=>Audio Input=>System/Internal ), if your Mac( such as Mac mini) doesn't have microphone, you have to plug a real physical microphone into your Mac. This is a limitation of the iOS simulator.
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.