How to check if any music is playing in XCode - sprite-kit

How can I check if any sound is playing in the background and if true, it stops?
I tried this but didn't succeed:
if audioPlayerMainMusic.isPlaying{
audioPlayerMainMusic.stop()
}
thanks for help :)

Related

Audio file cannot be played on watchOS after background

When playing an audio file in watchOS (using SpriteKit):
run(SKAction.playSoundFileNamed("ready.wav", waitForCompletion:false))
It will works until you background the app (says face the watch outwards) and back, then the audio file cannot be played anymore.
I have tried many workarounds, including setting UIBackgroundModes to audio, but the sound will always stop working after the app is background and back. Any solution?
PS: I think I might have found the answer: use AVAudioPlayer instead of SKAction.
You need to start HKWorkoutSession and in info.plist enable workout background processing. Don't need the audio to play in background, just continue to play once it comes back from background.
Use AVAudioPlayer instead of SKAction. This works.
Only SKAction.playSoundFileNamed has this issue.

How to turn on , off sound for MPMoviePlayerViewController

Hey I need to set volume for MPMoviePlayerViewController in ios.
I have searched a lot on this but fails. I have got answers to set volume for MPMoviePlayerController,
MPmusicPlayer but not MPMoviePlayerViewController.
Please help me. thanks in advance.
you dont need to do it programmatically....the UI of MpMovieplayerViewController do have volume adjustment as default...why are you trying to do it your own?
ok try this
[[MPMusicPlayerController applicationMusicPlayer] setVolume:];
Will change the device's volume, not the media player's so any other audio or video streams are also affected.
try this...
[[MPMusicPlayerController applicationMusicPlayer] setVolume:sliderr.value];
it only work in device,not work in simulator

How to mute a video on iOS while iPod still playing audio?

I'm developing a teaching app playing video animations with no sound.
I would like to prevent the movie player from stopping other audio sessions (such as the iPod) when the video is launched.
Is there any way to indicate the iOS the video has no audio or play in mute mode?
Thanks in advance for your help!
You can always detect if the music is playing and and resume it right way after the movie started playing.
Detecting player state
[MPMusicPlayerController iPodMusicPlayer] playbackState]
Resume
[[MPMusicPlayerController iPodMusicPlayer] play];
I think maybe something like this
UInt32 allowMixing = YES;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
AudioSessionSetActive(YES);
should do the the trick.
Keep in mind, you'll need to add the AudioToolbox library to your application, and add:
#import <AudioToolbox/AudioToolbox.h>
to your classes imports in order to use the above code.
It should allow your app's sounds to mix with the other app's sounds in the background. In this case, since your app is silent, it will just allow background sounds to keep playing.
Hope it helps!

AVCaptureSession cancels background audio

Whenever I start an AVCaptureSession running with the microphone as an input it cancels whatever background music is currently running (iPod music for instance). If I comment out the line adding the audio input, the background audio continues.
Does anyone know of a way to record video clips with the microphone while continuing to allow background audio to play? I've looked around a lot, and can't seem to find any references to this behavior.
Thanks for any help!
Try setting kAudioSessionProperty_OverrideCategoryMixWithOthers as seen in https://stackoverflow.com/a/7426406/16572
Is the background music a looping track? -- if so you could make it a system sound and tell it to re-play when it finishes playing. The reason being you can only have tracks up to 30 seconds as a system sound.
Don't know if it helps -- but thats one approach :)
Cheers,
Michael
I had the same problem and used this in my app and it worked:
UInt32 audioRouteOverride = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,sizeof (audioRouteOverride),&audioRouteOverride);
I was able to record video and audio and at the same time play a sound out of the speakers.

Fade In and Out Audio in iPhone SDK

I am wondering if there is some way to fade in and out audio using AVAudioPlayer in CocoaTouch, with a pause command once the music finishes fading and then a play command once the fading begins. I have heard that one can use NSTimer for this purpose, but I am unsure of the easiest way to accomplish this.
Thanks for any help!
There is a good video tutorial to do this here: Playing a sound, and fading a sound using AVAudioPlayer on iPhone.