I have 2 Player instnace. One need to play background sound, second to play other sound.
Background sound play in loop
bgPlayer.setLoopCount(-1);
try {
bgPlayer.start();
} catch (MediaException e) {
e.printStackTrace();
}
When I try to play background sound and then play other play another sound, the background sound is stopped (9860) and in some simulator or device (9800 torch) both sound are playing but sound quality is deteriorating.
How to play two sounds simultaneously?
Related
I have tried this code before calling Play function of video player
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch {
// report for an error
}
In this way video player play sound even ringer switch is down without raise volume , But what i want to do is
Allow voice for videos when clicking the raise volume even in mute
I'm working on an app with a piano keyboard. I want that the sound after key-pressing is only played, when "Ring/Silent" switch is on ring. Then it should stop background music from other apps . If the "Ring/Silent" switch is on silent, it shouldn't do anything and let the background music from other apps play (like Spotify).
What I tried (with Spotify as background music):
Option 1 [AVAudioSessionCategoryAmbient]:
"Ring/Silent" switch is on ring: background music doesn't stop and it plays in-app sound together with background music (-> What I don't want)
"Ring/Silent" switch is on silent: background music doesn't stop and no in-app sounds are played (-> What I want)
Option 2 [AVAudioSessionCategorySoloAmbient]:
"Ring/Silent" switch is on ring: background music stops and it plays in-app sound (-> What I want)
"Ring/Silent" switch is on silent: background music stops, but no in-app sounds are played (-> What I want, it shouldn't stop background music)
I think that Option 2 is the closest option I can get. The iPhone shouldn't stop background music when "Ring/Silent" switch is on silent and I'd be happy.
Option 3 [AVAudioSessionCategoryPlayback]:
"Ring/Silent" switch is on ring: background music stops and it plays in-app sound (-> What I want)
"Ring/Silent" switch is on silent: background music stops and it plays in-app sound (-> What I don't want)
My Code:
let NoteSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: currentNote.sound, ofType: "m4a")!)
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(contentsOf: NoteSound as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print("Problem in getting File")
}
In the code example I only changed .setCategory() .
There is no native API to check if the phone is on Silent or not, and you are discouraged from basing your app's functionality on such user choices (On low battery mode, Charging or not, ...).
There is a hack to check whether the device is muted or not: Play a short sound of 0.5s minimum duration, and detect the time it took to play. For more details on this workaround, have a look here.
In the game I'm building there are sound effects and music. Players are able to turn off the in-game music and sound effects. I assume that when a player turns off the in-game music it's probably because they want to listen to their own music. However, when I run a Sound.play() in order to play a sound effect the background music being played from the music app is stopped. Is there a way around this?
Thanks in advance.
I am recording audio with AVAudioRecorder. I want to however play a short sound before the recording starts. I am doing this with AudioServicesCreatSystemSoundId. I notice that I need to pause the thread after calling the sound effect or else it will start playing, then get interrupted by the AVAudioRecord record call and then finish playing after the recording has been stoped again.
So I have something like this:
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
[NSThread sleepForTimeInterval:0.7];
[_audioRecorder record];
The problem is that one notices the delay. I know there are apps out there (like i.e. Voxer) that make the sound without delaying the actual recording. I tried splitting of the first two lines in a block, but that didn't help. How do they do it?
Thanks
For low latency (no noticeable start delay) audio recording, use a pre-started RemoteIO Audio Unit instead of the AVAudioRecorder API.
I have a video with sounds, and I want to play other sounds on the background. So I want to stop the video music to keep playing just the other music. I have to code for all except to stop the music for the video. Is it possible to build?