iOS not playing sound - ios

So, I am making a game and it is almost done, however, the problem I am having is that when the player comes in contact with the bomb, it just kills the player and does not play any sound effect. It plays the sound effect when collecting items though. The collection for the items are set up the exact same. When I collect an item, it plays the sound effect, when I hit a bomb, it doest play anything. Just silence. Please help!
if firstBody.node?.name == "Player" && secondBody.node?.name == "Bomb" {
if sound == true {
self.run(SKAction.playSoundFileNamed("Bomb.mp3", waitForCompletion: false))
}
firstBody.node?.removeFromParent()
secondBody.node?.removeFromParent()
self.scene?.isPaused = true
playerDied()
}

Related

How to stop currently playing audio automatically when another play button in different UITableView cells are pressed? - Swift

I am a swift beginner currently working on my first iOS app.
I have a TableViewController which has several cells and each cell contains a play button. When a few play buttons are pressed, a few different audios are played at the same time, but I would like to stop a currently playing audio when another playing play button is pressed.
Here are the sample codes I created.
Please give me an advice. Thank you so much!
The problem is that every single cell has its own independent player. You want just one player, so that you can stop it and start a new sound. For example, make the one player a property of the view controller. Every cell can see it but there is just one.
create an extension for AVPlayer because AVPlayer does not own condition for playing. extension given below.
extension AVPlayer {
var isPlaying: Bool {
return rate != 0 && error == nil
}
}
use it while tap on play like this.
if player?.isPlaying == true {
player?.pause()
player?.seek(to: CMTime(seconds: 0, preferredTimescale: 60))
}
//set up you audio file to player
player?.play()

MPMusicPlayerController skips to next song after continuing playback

I am using the MPMusicPlayerController to play songs from Apple Music. I am periodically (every several seconds) changing the song queue using
setQueue(with: MPMusicPlayerStoreQueueDescriptor(storeIDs: ids))
I don't have any problems during music playback, the song queue is updated as requested, I can skip to next song and everything is fine. However, whenever I pause the player in the middle of the song, wait a bit till a new queue is set, and play again - the current song is lost and next song starts to play!
So imagine the following situation:
I have songs A, B, C, D
I set it as a song queue and call play()
player.nowPlayingItem returns A
I set the song queue to E, F, G, H while playing
I call player.skipToNext() - song E starts playing, as expected
I call player.pause() - song E pauses
I call player.play() - song E continues to play. Everything is fine till now
I call player.pause() again - song E pauses
I set the song queue to I, J, K, L.
I call player.play() - I expect the paused song E to continue playing. Instead, song I starts playing
I also did some log output for the above scenario:
func togglePlayPause() {
if player.playbackState == .playing {
player.pause()
} else {
NSLog("NP before \(player.nowPlayingItem)") // prints E at step 10
player.play()
NSLog("NP after \(player.nowPlayingItem)") // prints nil at step 10
}
}
Strangely, pause/play from lock screen works fine, even if I change the queue in between.
Has anyone experienced similar problem, any hints/workarounds how to fix this?
As a workaround, I used
player.currentPlaybackRate = 1
instead of player.play(). Seems like play() does a bit more than just playing from paused location.

What's the difference between playbackLikelyToKeepUp and AVPlayerItemStatusReadyToPlay?

I'm trying to understand how to properly detect when a player item can play again.
See below observer logic:
if (object == playerItem && [keyPath isEqualToString:#"playbackBufferEmpty"])
{
if (playerItem.playbackBufferEmpty)
{
// show loading indicator
}
}
if (object == playerItem && [keyPath isEqualToString:#"playbackLikelyToKeepUp"])
{
if (playerItem.playbackLikelyToKeepUp)
{
// hide loading indicator
if (playerItem.status == AVPlayerItemStatusReadyToPlay) {
// start playing
}
else if (playerItem.status == AVPlayerStatusFailed) {
// handle failed
}
else if (playerItem.status == AVPlayerStatusUnknown) {
// handle unknown
}
}
}
Is checking for AVPlayerItemStatusReadyToPlay underneath playbackLikelyToKeepUp overkill?
Or
Should I only listen to status change on the player item instead of bothering with playbackLikelyToKeepUp?
The two properties inform us of two different pieces of information in regards to the status of an AVPlayerItem. AVPlayerItemStatusReadyToPlay is a constant that will indicate readyToPlay ONLY once an AVPlayer has been given sufficient time to buffer enough of the item's data such that it is able to BEGIN playback of the item. But that is it. Just because an item is ready to play, doesn't mean that it won't stall after the first few seconds.
playBackLikelyToKeepUp returns a bool indicating that playback of the item is "likely" to keep up throughout the duration of the item. This property does NOT only pertain to the beginning of the item, like AVPlayerItemStatusReadyToPlay. It does not "care" if the item is ready for playback, all it "cares" about is wether or not it "thinks" that playback of the item will keep up without stalling. This is a PREDICTION of playability that takes into account various factors which you can read about here -> https://developer.apple.com/documentation/avfoundation/avplayeritemstatus
So in regards to your question, is it overkill to check the value of AVPlayerItemStatusReadyToPlay after you've already checked playbackLikelyToKeepUp... well it is up to you. I personally would check both. I would want to ensure first that the item is ready to play, meaning that sufficient data has been buffered by the AVPlayer in order to begin playback. AND I would then want to ensure that playbackLikeyToKeepUp == true so that I can have some degree of certainty that the user's media experience won't be interrupted. But if all you care about is knowing when an item is ready to begin playback again, then you only need check the status.

How to Stop AVAudioPlayer

I am making a noiseMaker application for iOS in xCode where there are different buttons that play different sounds. Each sound is given a tag of 0-3 along with each button in the storyboard. And the randomize button has a tag of 4, Here is the code so far:
func play (index: Int) {
if !player.isEmpty && index >= 0 && index < player.count {
player[index].play()
}
if index == 4{
let randomNumber = Int(arc4random_uniform (4) + 0)
player[randomNumber].play()
}
However, what I want is for the current song to stop playing when I press on another button or even on the same button again. I believe the stop AVAudioPlayer code would go right when the function "play" begins so that it stops the player before any other song starts playing.
All help is appreciated. Thanks in advance...
You can check that currently player is playing or not.
Check the playing property returns status of player is playing or not.
if (aPlayer.playing) {
aPlayer.stop()
//Then play again by aPlayer.play()
}
else {
aPlayer.play()
}

Playing Sound File while Game is Paused in SpriteKit / Swift

In my game I made a pause menu which pauses the entire game and loads a play button, score label, and a main menu button. When I click the play button, pause button, or main menu button, I want to have it play a sound file. However, it is not letting me do this because everything is paused. I tried manually pausing all nodes, but this is not working. Is there anyway to unpause actions? Also, can you programmatically change a sound file's volume? Below is my code for when the user taps the pause button. I am using xCode 6.4 by the way.
if targetNode.physicsBody?.categoryBitMask == PhysicsCategory.Pause {
switch gameState {
case (.GameRunning):
if paused == false {
runAction(SKAction.playSoundFileNamed("beep1.mp3", waitForCompletion: false))
paused = true
pauseScene()
}
case (.GameOver):
return
}
}

Resources