I am currently building a game with swift and spritekit. I want to play a sound in a certain part of the code. Currently I am using AVAudioPlayer. I am currently using this method.
player = try AVAudioPlayer(contentsOfURL: url)
player.prepareToPlay()
player.play()
The sound plays correctly, but if I were to be listening to music through itunes while the sound is played my itunes music is stopped and the sound is played. I have played other people's games in the past where ingame sound effects play and do not effect music that was already playing from another app. Can somone explain how this is achieved.Thanks a bunch.
You'll need to set your audio session to use a category that allows mixing. By default it uses AVAudioSessionCategorySoloAmbient which is non mixable.
I don't know which category will fit your needs but AVAudioSessionCategoryAmbient allows mixing.
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
Related
I am currently using the following statement to detect music:
if MPMusicPlayerController.systemMusicPlayer().playbackState == .Playing {
print("There is music playing")
}
Great, however this will only work for iTunes player, and not music that might be coming from a different app, specifically talking about Spotify.
I don't need to know the song being played, simply whether there is anything playing at all, so I can decide whether I provide my own background music for my game or not.
Edit: ideally the solution should cover any 3rd party music program, not just Spotify.
Given iOS: How do I detect if music is playing in any background music app?
the Swift version would be:
let isOtherAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying()
However, the developer docs suggest that starting with iOS 8.0 you should use secondaryAudioShouldBeSilencedHint instead:
if (AVAudioSession.sharedInstance().secondaryAudioShouldBeSilencedHint()) {
print("another application with a non-mixable audio session is playing audio")
}
I have an app that plays silent Movies using AVFoundation. Is there a way to allow the music to play and your iPod (if you're playing music) to continue playing? If I play the movie will my iPod is on, the iPod pauses so the movie can play, and vice versa - if I try to replay the music while the movie is playing, the movie pauses. Basically, I want them to play continue playing seamlessly....
Let me know if you need applicable code to answer.
In my experience, for standard AVAudioPlayer based sound in an app, managing the AVAudioSession is required to allow background music:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
But I can't tell you if that's the same as attempting to play encoded/compressed Video and encoded/compressed audio at the same time.
I am creating a radio streaming app with play, pause and volume slider.
I have implemented volume slider using MPVolumeView but unfortunately it is not working.
Can any one please let me know the correct code so that the volume slider will work in my app. I have used MPMoviePlayerController, AVPlayer, AVAudioPlayer.
Sounds as if you missed an important part of Apple's documentation;
Working with Movies and iPod Music
I am suspecting that you are using both, AVAudioPlayer and MPMoviePlayerController together and that you have setup some audio session attributes to get that working properly. Now when doing so, you may want to tell MPMoviePlayerController to use that session / or not.
Using the Media Player Framework Exclusively
If your application is using a movie player only, or a music player
only—and you are not playing your own sounds—then you should not
configure an audio session.
If you are using a movie player exclusively, you must tell it to use
its own audio session, as follows:
myMoviePlayer.useApplicationAudioSession = NO
If you are using a movie
player and a music player, then you probably want to configure how the
two interact; for this, you must configure an audio session, even
though you are not playing application audio per se. Use the guidance
in Table 6-1.
In my application I have made [audioSession setCategory:AVAudioSessionCategoryPlayback] to play audio in back ground and also to mute other audio play backs while playing song in my application. I am playing the audio using AVPlayer and also I have UIWebView for playing video in same Application. The issue arises when I play both video in web view and audio using AVPlayer. The music mixes in this case. Prior to iOS 6 when I rech same situation one audio pauses automatically and the mixing up does not occur. So kindly suggest some solutions.
Try to set
Required background modes -> App plays audio
in Info.plist.
As you can suggest, I have a trouble =)
In the game I use CocosDenshion for playing both background music and sound effects. As far as I know and can see in the code, it uses AVAudioPlayer to play background music. The trouble is appeared when I try to launch my app with music from iPod library.
I set audio category to AmbientSound (tried setting both with AVAudioSession and C interface). If I then try to play game background track directly using AVAudioPlayer, it plays with iPod music in background without problems. But if try to play background music with SimpleAudioEngine, only iPod music is playing.
Can someone give an advice how to make CocosDenshion work in this case?