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

Audio for IOS is coming out of ear speaker and not main speakers. #52

Open
JimAdamsdev opened this issue Jul 2, 2020 · 6 comments
Open

Comments

@JimAdamsdev
Copy link

When I play audio the sound just comes out of the ear speaker for IOS. Can I do something to change this to come out of all the speakers or switch to the main speaker? Thanks in advance for your help.

Jim

@fliot4u
Copy link

fliot4u commented Jul 21, 2020

did you find a solution for this. I am having the same exact problem. It changes all the audio output of my app to the ear speaker. I wonder if you found a work around to this issue. Thanks.

@JimAdamsdev
Copy link
Author

JimAdamsdev commented Jul 21, 2020 via email

@fliot4u
Copy link

fliot4u commented Jul 21, 2020

I did found a solution or workaround.
Create an interface on your share project like this:
namespace .IOS
{
public interface IAudioService
{
}
}

and create a class AudioService.cs that implements it just like this:
using System;
using AVFoundation;
using Foundation;

namespace .iOS
{
public class AudioService : IAudioService
{
public AudioService()
{
}

    public void PlaySoundThroughSpeaker()
    {
        var session = AVAudioSession.SharedInstance();
        session.SetCategory(AVAudioSessionCategory.Playback);
        session.SetActive(true);

    }
    public void PlaySoundThroughEarPiece()
    {
        var session = AVAudioSession.SharedInstance();
        session.SetCategory(AVAudioSessionCategory.PlayAndRecord);
        session.SetActive(true);
        

    }
}

}

then in your event clicked do something like this:
void RecordClicked(System.Object sender, System.EventArgs e)
{
var audioService = new AudioService();
audioService.PlaySoundThroughEarPiece();
RecordAudio();
}
because before recording we have to allow the phone to use the microphone and ear speaker.
and then in wherever you handle the audio file created by the recorder add this code at the end:

private void Recorder_AudioInputReceived(object sender, string audioFile)
{

		DisplayAlert("Notification", "Audio Message Sent Succesfully!", "Close");

		
		var audioService = new AudioService();
		audioService.PlaySoundThroughSpeaker();

	}

basically we are changing from the main speakers to the ear speakers. but as I notice if we are going to record we need to set it as ear speakers for that but we can easily change it back when we are done recording. Hope it helps! Good Luck!

@JimAdamsdev
Copy link
Author

JimAdamsdev commented Jul 21, 2020 via email

@fliot4u
Copy link

fliot4u commented Jul 27, 2020

At least for me the android side doesn't give any issues.
But in order to run that code in the shared code you can either add a try{}catch{} or put the code inside a:
#if IOS
var audioService = new AudioService();
audioService.PlaySoundThroughSpeaker();
#endif
because android will give you an exception when you try to initialize an audio service.

@grockland
Copy link

#if IOS and implementing a AudioService in Xamarin iOS for a DependencyInjection with much the same code as above (fliot4u) worked for me. Android ok out of the box.

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