Cordova Media Plugin breaks HTML5 Audio tag on iOS

房东的猫 提交于 2019-12-06 15:42:21

Back with a solution.

In file cordova-plugin-media/blob/master/src/ios/CDVSound.m, apply the following patch :

// get the audioSession and set the category to allow recording when device is locked or ring/silent switch engaged
         if ([self hasAudioSession]) {
             if (![self.avSession.category isEqualToString:AVAudioSessionCategoryPlayAndRecord]) {
             -                    [self.avSession setCategory:AVAudioSessionCategoryRecord error:nil];
             +                    [self.avSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
             }

             if (![self.avSession setActive:YES error:&error]) {

Basically while changing our AvAudioSession catagory, we have to put it in category 'Play And Record', not only 'Record' and add option 'to mix with others audio sessions'.

Hope this will help ;)

In addition to your fix, I've found that when you start recording, audio is sent automatically to earphones instead of speakers, making the sound low. To fix this, I also added AVAudioSessionCategoryOptionDefaultToSpeaker mask to options. This will detect automatically if you have earphones connected, if not, it will default to speakers output. The resulting code is:

[self.avSession setCategory:AVAudioSessionCategoryPlayAndRecord
  withOptions:AVAudioSessionCategoryOptionMixWithOthers |
   AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

Just double check you are releasing the underlying operating system's audio resources after recording. Was a simple fix for me:

my_media.release();

For ionic2 or higher

Replace the CDVSound.m file with this one: https://gist.github.com/malinosqui/0df4c570403b29f08f3cf1352f4b56b9 (contains @MasterKitano and @Sn00p answers)

the CDVSound.m file is located in platforms/ios/{AppName}/Plugins/cordova-plugin-media/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!