NateRickard / Plugin.AudioRecorder

Audio Recorder plugin for Xamarin and Windows
MIT License
164 stars 68 forks source link

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

Open JimAdamsdev opened 4 years ago

JimAdamsdev commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Have not found a work around. I did notice that if you close the app and then reopen it then all speakers

work. However, when you record it switches it back. I got pulled in a different direction. Going to get back into

this later this week. Wondered if a way exists to reset speakers after recording finishes. Let me know if you have

any luck. Thanks.

From: fliot4u notifications@github.com Sent: Tuesday, July 21, 2020 12:14 PM To: NateRickard/Plugin.AudioRecorder Plugin.AudioRecorder@noreply.github.com Cc: JimAdamsdev jim@adamstechsols.com; Author author@noreply.github.com Subject: Re: [NateRickard/Plugin.AudioRecorder] Audio for IOS is coming out of ear speaker and not main speakers. (#52)

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.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/NateRickard/Plugin.AudioRecorder/issues/52#issuecomment-661956629 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AQE5BIZONNM3CSIBZNQGL2TR4W5FTANCNFSM4OPAXMNQ .

fliot4u commented 4 years ago

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 commented 4 years ago

Thanks! 😊 My android phone has only one speaker. Have you tested to see if its also occuring on Android?

From: fliot4u notifications@github.com Sent: Tuesday, July 21, 2020 2:14 PM To: NateRickard/Plugin.AudioRecorder Plugin.AudioRecorder@noreply.github.com Cc: JimAdamsdev jim@adamstechsols.com; Author author@noreply.github.com Subject: Re: [NateRickard/Plugin.AudioRecorder] Audio for IOS is coming out of ear speaker and not main speakers. (#52)

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!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/NateRickard/Plugin.AudioRecorder/issues/52#issuecomment-662024028 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AQE5BI5DCAIGPBVUHJ7GXZDR4XLHLANCNFSM4OPAXMNQ .

fliot4u commented 4 years ago

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 commented 4 years ago

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.