I am using the following sounds (all 32K AAC as mentioned in Apple document but I use mono instead of stereo: https://developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/AudioandVideo.html) for my watchOS 3 games.
let hitSound = SKAction.playSoundFileNamed("hit.m4a", waitForCompletion:false)
let explosionSound = SKAction.playSoundFileNamed("explosion.m4a", waitForCompletion:false)
let music = SKAudioNode(fileNamed: "delta.m4a")
I use hitSound and explosionSound for short sound effect, and play music on the background, they works fine until the game is quit and resume from dock.
The music is resume and the sound effects are no longer heard. Is there any problem with using SKAction.playSoundFileNamed to play sound?
Related
I have an iOS application that can play music and video. You can cast music or video from the app using the chromecast sdk. I've been reading the documentation about playing in background. When setting this code:
let criteria = GCKDiscoveryCriteria(applicationID: chromeCastReceiverAppID)
let options = GCKCastOptions(discoveryCriteria: criteria)
options.physicalVolumeButtonsWillControlDeviceVolume = true
options.suspendSessionsWhenBackgrounded = false
GCKCastContext.setSharedInstanceWith(options)
I can keep controlling my music while casting and the app is in background this is great. When casting a video the session is lost while in background and when the app resume it doesn't resume automatically as stated in the documentation. it does work if I set the suspendSessionsWhenBackgrounded = true but then I get suspended when casting music and can't switch to the next song.
Is there a way to switch that option based on what type of content I'm casting?
I am trying to detect which music from another app is playing in background.
I know there is a way to detect if a music is played in background but I would like to detect which song and the time if I can. How do I do that? In Swift or Objective-c
You can use the MediaPlayer framework to get the current Music playing in the Music app:
import MediaPlayer
let player = MPMusicPlayerController.systemMusicPlayer()
let currentSongTitle = player.nowPlayingItem?.title
let currentPlaybackTime = player.currentPlaybackTime
Note that you need to have the NSAppleMusicUsageDescription (aka Privacy - Media Library Usage Description) set to the String you want to present to the user when asking for permission.
I am developing a tvml/tvjs application and I have added the following code to allow audio to play in the background (applicationDidBecomeActive):
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.setActive(true)
} catch {
print("no audio to assign")
}
I also set the first item in Required Background Modes in info.plist to App plays audio or streams audio/video using AirPlay.
My problem now is that although I can play audio in the background and pause the audio from anywhere in tvOS, I cannot play the audio after pausing. I am looking for the functionality like the Podcast app where I can freely play and pause the current streaming audio.
I cannot seem to find anything in the documentation for TVML/TVJS that points me in the right direction. Thanks!
I am playing sounds in loop in my app. So it should continue playing through out the app. but sometimes it stops after playing sound for 3/4 times.I don't understand whats happening.
I am using audio-toolbox framework for playing sound. creating audio queue and then playing sounds in loop. I am also playing sound from ipod library using mediaplayer. Same thing happening with song from ipod.
I have set [musicPlayer setRepeatMode: MPMusicRepeatModeOne]; but still it stops after 3/4 times.
How exactly are you playing the sound? Show us the code. Do you use System Sound Services? They are not meant to play longer sounds, quote Audio Toolbox Reference:
You can use System Sound Services to
play short (30 seconds or less)
sounds.
This could be the source of your problem. But until you show us the code we can’t but guess.
I need to use AVAudioPlayer and MPMoviePlayerController simultaneously i.e play a movie while the background loop is playing is it possible.
My bg music loop stops working when a movie starts playing , so I tried to stop the bg music loop and start the movie and when movie stops, start playing the bg loop again but this is also is not working.
In fact it is achievable from 3.x onwards. I had once done this in one of my applications and its got nothing to do with any backgrounding or any other new iOS features. You just need to configure the audio sessions properly. If you make sure that both your audio player as well as the video player are using the ambient audio session it is possible.
Prior to starting your AVAudioPlayer to play, set the category of audio session to AVAudioSessionCategoryAmbient. This is the system wide common audio session. This allows other sounds to play along such as the sound from the iPod application.
In case you are using the MPMoviePlayerController from iOS 3.2 or above make sure you set the useApplicationAudioSession to NO. This ensures other sounds to play along. It is the default in earlier versions of iOS.
There is a limitation in the current iPhone SDK (2.x and 3.x) that does not allow any background audio playing while a movie is playing in the MPMoviePlayerController API. Unfortunately, it is not possible to do what you are asking at this time within the iPhone SDK.
I would suggest logging an enhancement request to Apple to offer this in a future version of the SDK.