simple audio engine isBackgroundMusicPlaying is still YES after done playing - iphone

I need to check whether the background music is playing every seconds, and if it's not playing, then i will randomly start to play another song.
This method gets called every seconds
- (void)private_checkMusicEnds {
if (NUM_BGMUSIC > 0) {
if ([ModuleHelper audio_isBackgroundMusicEnabled]) {
//ERROR: BACKGROUND MUSIC NOT PLAYING BUT ALWAYS IS PLAYING???
if ([SimpleAudioEngine sharedEngine].isBackgroundMusicPlaying == NO) {
if ([CacheHelper isFirstTimeSetDefaultBGMusic]) {
_currentPlayingMusic = DEFAULT_BGMUSIC_INDEX;
}
else {
//song start at 1
_currentPlayingMusic = [Helper getRandomIntBothInclusiveLow:1 high:NUM_BGMUSIC];
}
NSString *fn = [NSString stringWithFormat:FORMAT_STRING_SONG_FILE_NAME, _currentPlayingMusic];
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:fn loop:NO];
}
}
}
}
When the song is finished playing, [SimpleAudioEngine sharedEngine].isBackgroundMusicPlaying is still YES.
I have also tried [[CDAudioManager sharedManager].backgroundMusic isPlaying] but I get the same.

Related

playing audio in background and recording that background audio with video on iphone

In my app i want to play background audio and video recording both audio and video will be record....
How can i do this functionality???
// Setup cam
-(void)setupDivAvCam
{
self.capturingWindow.delegate = self;
[self.capturingWindow setupWithOptions:#{DIYAVSettingCameraPosition : [NSNumber numberWithInt:AVCaptureDevicePositionBack] }];
[self.capturingWindow setCamMode:DIYAVModeVideo];
}
#pragma mark - Rec video methods Start
-(IBAction)captureVideo:(id)sender
{
[self startCapturingVideo];
}
-(IBAction)stopVideo:(id)sender
{
[self stopCapturingVideo];
}
//To start video recording
-(void)startCapturingVideo
{
{
{
if(![[UIDevice currentDevice].model isEqualToString:#"iPhone Simulator"])
{
//To disable the camera button for some time untill image loads
_cameraButton.userInteractionEnabled = NO;
[self.capturingWindow captureVideoStart];
}
}
}
}
//To stop video recording
-(void)stopCapturingVideo
{
[self.capturingWindow captureVideoStop];
}
The above code is to get the video
I looked in several forums and they all couldn't deliver a satisfying answer.
My wish is to be able to play and record video while playing music at the background. I managed to do that with the help of a snippet I found. here is the code:

how to stop audio when iOS Sleep Timer gets called

I want to stop my audio app when iOS sleep timer gets called.
Just like Pandora app.
http://help.pandora.com/customer/portal/articles/24324-ios-sleep-timer-with-pandora
Tap the Clock app, Tap Timer, Select a time, Tap When Timer Ends, Tap Stop
Playing
This will sleep your Pandora app if it is running.
I can see inInterruptionState == kAudioSessionBeginInterruption gets called when iOS sleep timer ends, but how can I detect if it's sleep timer or just interruptions like phone call?
Here is my codes.
Currently, my app just starts playing again after iOS sleep timer ends.
// Audio Interruption Listener
void MyInterruptionListener(void *inClientData, UInt32 inInterruptionState) {
if (inInterruptionState == kAudioSessionBeginInterruption) {
[[DOSpeechManager sharedInstance] audioSessionBeginInterruption];
}
if (inInterruptionState == kAudioSessionEndInterruption) {
[[DOSpeechManager sharedInstance] audioSessionEndInterruption];
}
}
- (void)audioSessionBeginInterruption {
if ([_MyAcaTTS isSpeaking] && [_MyAcaTTS isPaused] == NO) {
[_MyAcaTTS pauseSpeakingAtBoundary:AcapelaSpeechImmediateBoundary];
[self setAudioSettionStatus:NO];
_audioInterruptedWhileSpeaking = YES;
}
}
- (void)audioSessionEndInterruption {
if (_audioInterruptedWhileSpeaking) {
[self setAudioSettionStatus:YES];
[_MyAcaTTS continueSpeaking];
}
}
- (void)setAudioSettionStatus:(BOOL)status {
AudioSessionSetActive(status);
[_MyAcaTTS setActive:status];
//cancel audio interrupted flag
if (status) {
_audioInterruptedWhileSpeaking = NO;
}
}
The trick is not to detect the source of the interruption, but to know whether your app should resume after the interruption.
The AVAudioSession API will send a notification when the audio session is interrupted. Within this notification, the OS gives a "hint" as to whether the app should resume playback or not.
See below:
//Add notification observer
__weak typeof(self) weakSelf = self;
self.audioSessionInterruptionNotification =
[[NSNotificationCenter defaultCenter] addObserverForName:AVAudioSessionInterruptionNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
NSNumber* interruptionType = note.userInfo[AVAudioSessionInterruptionTypeKey];
NSNumber* interruptionOption = note.userInfo[AVAudioSessionInterruptionOptionKey];
BOOL shouldResume = interruptionOption.integerValue == AVAudioSessionInterruptionOptionShouldResume;
switch (interruptionType.integerValue) {
case AVAudioSessionInterruptionTypeBegan:
[weakSelf beginInterruption];
break;
case AVAudioSessionInterruptionTypeEnded:
[weakSelf endInterruption:shouldResume];
break;
default:
break;
}
}];
}

How to play Next song Automatically when First one is finish?

In my app I am using AudioStreamer to play a song through server,
I am refering this tutorial,
http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html
How to play next song Automatically when using AudioStreamer?
In the tutorial is mentioned callback function which is triggered when a song finish playing. Your goal is to write functionality for your list of songs to move on the next one when this function is called.
void MyAudioQueueIsRunningCallback( void * inClientData,
AudioSessionPropertyID inID,
UInt32 inDataSize,
const void * inData)
{
move to the next song
}
I got answer,
In that tutorial there isplaybackStateChanged:(NSNotification *)aNotification method,
- (void)playbackStateChanged:(NSNotification *)aNotification
{
if ([songStreamer isWaiting])
{
}
if ([songStreamer isPlaying])
{
}
else if ([songStreamer isIdle])
{
[self playnextsong];
}
else if ([songStreamer isPaused])
{
}
}
When song completes through streaming,
It going to idle state.
So in idle state i call a method of playing next song.
And it Runs.

AVAudioPlayer resetting currently playing sound and playing it from beginning

I'm having an issue using AVAudioPlayer where I want to reset a player if it's currently playing and have it play again.
I try the following with no luck:
The sound plays once but then the second time i select the button it stops the sound, the third time starts the sound up again.
//Stop the player and restart it
if (player.playing) {
NSLog(#"Reset sound: %#", selectedSound);
[player stop];
[player play];
} else {
NSLog(#"playSound: %#", selectedSound);
[player play];
}
I've also tried using player.currentTime = 0 which indicates that would reset the player, that didn't work, I also tried resetting currentTime = 0 and then calling play that didn't work.
//Stop the player and restart it
if (player.playing) {
NSLog(#"Reset sound: %#", selectedSound);
player.currentTime = 0;
[player play];
} else {
NSLog(#"playSound: %#", selectedSound);
[player play];
}
For your situation I would try the following
//Pause the player and restart it
if (player.playing) {
NSLog(#"Reset sound: %#", selectedSound);
[player pause];
}
player.currentTime = 0;
[player play];
If you are going to immediately play the sound again, I would suggest pause instead of stop. Calling stop "Stops playback and undoes the setup needed for playback."
As indicated, if you want the sound to play again, you should avoid disabling the AVAudioPlayer instance with a stop but rather, just issue a reset of the position where it is playing. So, don't use stop() or pause() but as Plumenator mentioned, just issue .currentTime=0. So in your case, you would simply do:
if (player.isPlaying) {
player.currentTime = 0
} else {
player.play()
}
Swift example:
player.currentTime = 0.0;

How do I display an UIImageView while a sound is playing?

I'm trying to display an image while a sound is playing. I can get it to appear, but I'm having trouble making it disappear once the sound has finished playing.
My code:
-(IBAction)guitarChord:(id)sender
{
if (theAudio.playing == YES) {
theAudio.stop;
theAnimation.stop;
} else {
theAudio.play;
theAnimation.play;
}
}
I'm using AVAudioPlayer.
Any ideas what I am doing wrong?
How do I detect a sound has stopped?
Briefly, you need to take a look at AVAudioPlayerDelegate and audioPlayerDidFinishPlaying.