native-audio
native-audio copied to clipboard
On iOS stops the background music
Hello guys! 👋 Whenever I play a sound in my iOS application with the help of the plugin the background music stops immediately. Do you know any solutions to resume the music after the audio played or play the two audio (background music and the audio from my application) at the same time with different volume levels? On android it is working.
Faced the same problem on android and iOS, in my case, the music that was turned on before opening the application turns off after calling the preload function
I created a temporary package until the PR was merged. Who wants to already use this fix, you can install:
npm i capacitor-native-audio
@Sitronik thanks for the package i use it and that perfect, just do you think you can fix all deprecated issue in the plugin that will be awesome :)
@Sitronik thanks for the package i use it and that perfect, just do you think you can fix all deprecated issue in the plugin that will be awesome :)
I am very glad that my package helped you, Unfortunately, I do not have so much free time to do this
@riderx
The update to 3.0.4
resolves this issue. I think it's ready to be closed.
Yep agree
i made a new PR https://github.com/capacitor-community/native-audio/pull/67 to allow on IOS to mix sound without the need to remove silent mode.
How does one install the 3.0.4 version to get the fix for background mixing? I had 3.0.3 installed, I npm uninstall @capacitor-community/native-audio
and remove the package from my package.json, then do npm install @capacitor-community/native-audio
, and my package.json says it has 3.0.4 installed, but when I look in the 'definitions.d.ts' file, (/@capacitor-community/native-audio/dist/esm/), it is missing the 'focus' parameter...
(edit:) FWIW, if I use @Sitronik's npm i capacitor-native-audio
, things work fine, the audio from my app is mixed properly with any audio that is already playing. I must be doing something incorrectly in re-installing the new version, but cannot see what I might be doing wrong.
I'm using the latest version ATM 4.0.0-0 and I still have this issue in android when I start the app and it stops my music player.
Does 4.0.0-0 supports focus: false
in its configuration? Should I try another fork? Is anyone else experiencing this?
I have an Android 12 Samsung GALAXY S20.
When I looked at the definition file of 3.0.4 it doesn't have the focus parameter, not sure if this is a definition problem or that this version doesn't support this.
Any help would be appreciated.
This issue is still active. It seems like the Focus property on the configuration isn't available in 3.0.4.
I've also tried @Sitronik version (which as far as I understand has the "default mix" on, meaning I don't need to configure anything) but it doesn't solve my problem. My test case is as follows:
- Open pod cast addict (a pod cast app I use for this test) and play a pod cast
- Start my app which calls
preload
- The pod cast app stops
I've tested version 4.0.0-0 which has the focus parameter in the config but this didn't work as well. If any one has a solution it will be great. The only workaround I can think of is to preload just before I play the sound in my app which is not a popular use case and reduce the chance this issue will occur in my app, but that's just a hack...
otherwise guys you can use my fork, i did the focus parameter and that work in my app. i think of transferring it to my org capgo where i maintain a lot of fork of plugin. https://www.npmjs.com/package/@forgr/native-audio
@riderx thanks for the info but your fork and npm package doesn't solve the problem above I wrote. My code can be seen here: https://github.com/IsraelHikingMap/Site/blob/a2b76b810853fb2a198ea765b615118c0507f27c/IsraelHiking.Web/src/application/services/audio-player.factory.ts#L23 I hope I'm not doing anything wrong... When I'm using a music player and play a song, when this code is being called the music player pauses the current song... :-( Let me know if there's anything I can do to help debug/test this.
I made this feature because my users need to play music while my app Captime play sound. So that should work ^^ Did you remove the old plugin? Did you sync after install ? Did you build and copy again ? I double-check and that still work in my app, i can play music before preload or play and that don't stop.
I'm testing this on a CI build so basically npx cap sync and all that are always done in the pipeline, but just to make sure I did npx cap sync and committed the code and the problem still exists :-( Can you share a link to the play store where I can download your app and test if I can reproduce the issue on my phone?
If this is the following app then I experience the the issue on my device in this app as well: https://play.google.com/store/apps/details?id=ee.forgr.captime&hl=he&gl=US i.e. I open the app and the music app that I use pauses the music in the background.
FWIW I was never able to make this plugin not stop the music (e.g. Spotify) when my app played a sound (e.g a user interface noise). This has been a month ago that I tried.
FWIW, if I use @Sitronik's 'npm i capacitor-native-audio', as mentioned above, things work fine. I can preload sounds like music is playing, and initialize plugin via:
NativeAudio.configure({focus:false});
And things work fine on iOS (and Android for that matter).
@viking2917 thanks for the input!! I was not able to preload a file without stopping music playing no matter what I tried on my Android device. I understand what you are saying about playing music, which sound somewhat reasonable (but not ideal), but I was not able to reproduce your success with not stopping the music when only preloading. If you have some code you can share or a link to an app I can install to see if this issue reproduces on my phone in any app that can help as well. BTW, I'm using Samsung Galaxy S20 for my tests.
@harelM
I am in an Ionic app, in my own internal audio service, this is the code:
import { Injectable } from '@angular/core';
import { Platform } from '@ionic/angular';
// import { NativeAudio } from '@capacitor-community/native-audio';
import { NativeAudio } from 'capacitor-native-audio';
import { Haptics, ImpactStyle } from '@capacitor/haptics';
import { SettingsService } from './settings.service';
@Injectable({
providedIn: 'root'
})
export class AudioHapticService {
events = {
sample: { sound: 'public/assets/sounds/Notification_Plug_01.mp3', haptic: ImpactStyle.Medium },
startup: { sound: 'public/assets/sounds/Selection_Ukelele_chord_03.mp3', haptic: false },
error: { sound: 'public/assets/sounds/Notification_Max_02.mp3', haptic: ImpactStyle.Medium },
waiting: { sound: false, haptic: false },
doneWaiting: { sound: 'public/assets/sounds/Notification_Message_05.mp3', haptic: ImpactStyle.Light },
login: { sound: 'public/assets/sounds/Action_Login_01.mp3', haptic: ImpactStyle.Light },
logout: { sound: 'public/assets/sounds/Action_Logout.mp3', haptic: ImpactStyle.Light },
confirm: { sound: false, haptic: ImpactStyle.Medium },
done: { sound: 'public/assets/sounds/Selection_Discreet_01.mp3', haptic: ImpactStyle.Medium },
addBook: { sound: 'public/assets/sounds/Selection_Access_02.mp3', haptic: ImpactStyle.Medium },
removeBook: { sound: 'public/assets/sounds/Selection_Deny_02.mp3', haptic: ImpactStyle.Light },
notStarted: { sound: 'public/assets/sounds/Notification_Plug_01.mp3', haptic: ImpactStyle.Light },
reading: { sound: 'public/assets/sounds/Notification_Unplug_01.mp3', haptic: ImpactStyle.Light },
read: { sound: 'public/assets/sounds/Notification_Zen_01.mp3', haptic: ImpactStyle.Light },
expand: { sound: 'public/assets/sounds/Notification_Descreet_02.mp3', haptic: ImpactStyle.Light },
collapse: { sound: 'public/assets/sounds/Selection_Simple_Tone_06.mp3', haptic: ImpactStyle.Light },
reload: { sound: 'public/assets/sounds/Action_Hangpan.mp3', haptic: false }
};
isBrowser = (this.platform.is('desktop') || this.platform.is('mobileweb'));
audioSupported = !(this.platform.is('desktop') || this.platform.is('mobileweb'));
hapticsSupported = !(this.platform.is('desktop') || this.platform.is('mobileweb'));
constructor(private platform: Platform,
private settings: SettingsService) { }
preload = async (key, value) => {
console.log('registering', key, '||', value.sound);
NativeAudio.preload({
assetId: key,
assetPath: value.sound,
audioChannelNum: 1,
isUrl: false
});
};
async initialize() {
if (this.audioSupported) {
NativeAudio.configure({focus:false});
for (const [key, value] of Object.entries(this.events)) {
if (value.sound) {
await this.preload(key, value);
}
}
this.platform.ready().then(() => {
console.log('the platform is ready');
setTimeout(() => {
this.play('startup');
}, 100);
});
}
}
haptics = async (style) => {
await Haptics.impact({ style });
};
play(soundKey: string) {
if (this.settings.sounds && this.audioSupported && this.events[soundKey].sound) {
NativeAudio.play({ assetId: soundKey, time: 0 });
}
if (this.settings.haptics && this.hapticsSupported && this.events[soundKey].haptic) {
Haptics.impact({ style: this.events[soundKey].haptic });
}
}
}
have to run I will try to add more details in a bit!
(in my app component import { AudioHapticService } from './services/audio-haptic.service'; then in constructor do:
this.ha.initialize();
then play() as needed)
If I read the code correctly (and I'm probably not) this will always play the startup sound (this.play('startup');
) - so this means you can't really tell if the sound stops because you are playing a sound or because you are preloading, am I right?
Yes, my app plays a chime on startup. Before, if I was listening to Spotify music and started my app, Spotify would stop playing.
After I use the other repo I mentioned when I start my app, the Spotify sound is blended with the chime, and then the Spotify music continues afterwards. So my sound and Spotify overlap, then Spotify continues on its own afterwards
Maybe I misunderstood your problem? Is this the kind of thing you are trying to fix?
On Mon, Oct 31, 2022 at 4:16 PM Harel M @.***> wrote:
If I read the code correctly (and I'm probably not) this will always play the startup sound (this.play('startup');) - so this means you can't really tell if the sound stops because you are playing a sound or because you are preloading, am I right?
— Reply to this email directly, view it on GitHub https://github.com/capacitor-community/native-audio/issues/40#issuecomment-1297157554, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB2VYVK2MZQVZKLLN5L742TWF7ICZANCNFSM5DZENCHA . You are receiving this because you were mentioned.Message ID: @.***>
-- Mark Watkins Founder, Bookship https://www.bookshipapp.com
@.@. | (508) 395-0474
Shortlisted for The Bookseller's BookTech Company of the Year https://www.thebookseller.com/futurebook/booktech-company-year-2017-final-6-657151 . http://linkedin.com/in/markwatkins1 http://instagram.com/thehawaiiproject http://twitter.com/thehawaiiproj http://facebook.com/mark.watkins.56679
@viking2917 yes, this is what I'm trying to fix. I would like my app sound not to stop the music player/spotify when the sound plays. Currently, the sound doesn't even play on startup and I only call preload
, but the music player (I didn't try spotify, just pod cast addict and a music player I have on my device) stops playing the current music.
I tried all the above suggestions, and nothing helps :-(
Hm. I am not sure the answer, but one thing I did, after installing on the plugins, was to going into my app distribution and make sure the code I needed was actually in the app codebase. I had trouble with sync a few times and the code in the git repo did not show up in my app until I did some cleanups and re-syncing. (especially the api level that contained the 'focus' parameter, I would make sure that is in the code you are actually executing.
Also on Android, maybe you get some interesting log messages from 'adb logcat'. Otherwise I don't think I have any good ideas.
Does it work for you on iOS as desired?
@HarelM ok sorry i didn't check on android and i agree that the case, the good point is i can fix it :) and i will my users need it !
Great to hear I'm not crazy 😜 let me know when you have a fix available, I'll be happy to test it.
Can you try : https://www.npmjs.com/package/@capgo/native-audio i tried in ios and android and both work :)
@riderx the capgo npm package solves my issue, THANKS!!
@riderx did you create a PR with the latest fixes you made? I'm still using the capgp fork but I guess it wouldn't hurt to open a PR to this repo, right?
@HarelM I stopped to try to back port to the original repo, like many others project. Now my focus is to make Capgo big enough that I can pay people doing OSS on plugins