My application has the ability to call. Sound used to call AVAudioPlayer. The problem is that after a call to my application, if the music played in the background (for example, in the application "Music"), it does not restore music to play.
Tried some of the methods in this forum, but unfortunately nothing has helped. Help please, how can I solve this problem? Sorry for my english.
I solved this problem for myself. I check the music is playing or not, if yes then stop the music and put the flag.
thirdPartyPlayer = [MPMusicPlayerController iPodMusicPlayer];
[thirdPartyPlayer stop];
if (thirdPartyPlayer.playbackState == MPMusicPlaybackStateInterrupted || thirdPartyPlayer.playbackState == MPMusicPlaybackStatePlaying)
{
isThirdPartyPlayerPlay = true;
}
Then when the call ends, I restore play back music, if there is a flag.
if (isThirdPartyPlayerPlay)
{
[thirdPartyPlayer play];
isThirdPartyPlayerPlay = NO;
}
Related
I am working on Audio playing app using "MpmusicPlayer" and want to resume it after paused.
I am using "self.appMusicPlayer.currentPlaybackTime" but it is not working in swift 4.1 (IOS 11).
Is there have any other way to resume audio after pause using MpMusicPlayer?
It seems that this is a problem with iOS 11 that some people have run into when setting the currentPlaybackTime after calling play(). There was a work around posted on this apple dev forum here, but please find the code below (thanks to RunLoop).
I had just run into this exact problem. I managed to get around it
with the following code.
It creates a background thread that continuously checks the
currentPlaybackTime of the player. As soon as currentPlaybackTime
is not the time I set it to be I set currentPlaybackTime back to
what I wanted.
It feels like a terrible hack but it's working for me so far.
MPMusicPlayerController *player = [MPMusicPlayerController systemMusicPlayer];
player.currentPlaybackTime = _startTime;
[player play];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
while(true) {
if (player.currentPlaybackTime != _startTime) {
player.currentPlaybackTime = _startTime;
break;
}
}
});
Sorry for the weird title but AVAudioPlayer acts a bit weird when it comes to playing a sound again, right before the sound that it's playing ends.
I can play an mp3 file when the player is not playing, when the player finished playing and when the player is playing.
I cannot play an mp3 when the song that is currently playing is about to end. This is my code, I documented the important bits.
-(void) play:(int)p{
if([players[p] isPlaying]){
NSLog(#"is playing");
[players[p] setCurrentTime:0.025f]; //when it's playing, set it to the start of the
//file so it starts playing again. Usually works
if(![players[p] isPlaying]){
NSLog(#"is not playing"); //although I just told the player to play again
//it sometimes returns that it in fact does NOT play
//so it's playing in the first if condition but
//not playing in the second if condition
[players[p] prepareToPlay];
[players[p] play]; //from here I try to resurrect the player numerous times
//but everything fails, I have to call the function again to make it work
[players[p] setCurrentTime:0.025f];
if([players[p] isPlaying]){
NSLog(#"playing"); //never gets called; although I tell it to play so many times
//it just won't do
}else {
NSLog(#"NOTplaying");
[players[p] play];
}
} else {
NSLog(#"is still playing");
}
} else {
NSLog(#"not playing at all");
[players[p] play]; } //if not playing, play. Always works
}
I think this might have something to do with how the AVAudioPlayer releases the file or that it's playing but is not playing anyumore in the millisecond after that. (playing during the first if condition but stopped right after that).
The window in which this occurs can easily be reproduced with with a 5sec audio file. Just wait till it's about to end and then click "play" and the method gets executed but nothing's playing. The method works in every other case (not playing and playing but not going to finish playing).
I can turn off my audio player by using:
- (IBAction)sndOnOff:(id)sender {
if (_sndBtn.selected == NO) {
[_sndBtn setSelected:YES];
[audioPlayer stop];
}
else{
[_sndBtn setSelected:NO];
[audioPlayer play];
}
But how do I turn off a System Sound that I've created?
NSURL *soundURL1 = [[NSBundle mainBundle] URLForResource:#"selectSound"
withExtension:#"wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL1, &sound2);
There is a reason that we don't get fine control over system sound - because it is supposed to be short (in iOS < 30 seconds) and complete. Imagine, how many times do we experience a system sound cut-off in any OS?
But you could do this:
AudioServicesDisposeSystemSoundID (sound2);
to stop it. But this means you would need to create the sound again by using:
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL1, &sound2);
And to play it again:
AudioServicesPlaySystemSound(sound2);
You could of course instead to play your "system sound" on AVPlayer and stop it like you do a song, but that would mean it "shouldn't be" a system sound in the first place. Hope this helps.
Just do this...
AudioServicesDisposeSystemSoundID (sound2);
I cannot think of any negative consequences.
I would like to use AVPlayer to stream a .mp3 from internet
[[AVPlayer alloc] initWithURL:url];
It all works apart from the fact that I am unable to tell whether player is Loading (buffering) or playing ?
I have tried to use KVO on many properties, but none seem to provide me with a simple way of determining whether the AVPlayer is currently actually playing audio or whether it is loading data. It is driving me nuts.
Thank you for your suggestions in advance
You can detect when buffering is done and playback has started by creating a periodic time observation (using addPeriodicTimeObserverForInterval:queue:usingBlock:) to detect when the play time has started to move. For example:
AVPlayer *player;
// allocate AVPlayer and start playing
__weak PlayerController *blockSelf = self;
id observerToken = [player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0, NSEC_PER_SEC)
queue:nil
usingBlock:^ (CMTime time)
{
if (CMTimeGetSeconds(time) > 0.0)
{
// done buffering, should be playing now
[player removeTimeObserver:[blockSelf playerTimeObserverToken]];
[blockSelf setPlayerTimeObserverToken:observerToken:nil];
}
}];
[self setPlayerTimeObserverToken:observerToken]; // Keep a reference to the token for later use in block.
In my application I am using an AVAudioRecorder object to allow the user to record a sound file, and when the user is done recording they can play the sound via the AVAudioPlayer. The issue I'm having is when the user tries to play the audio file it only plays for one second. What is even more odd is that this code worked perfect on iOS 5, but since upgrading to iOS 6 this issue has started to happen.
I have checked the file that is being created via iTunes file sharing, and I'm able to play the entire sound file on my desktop.
Below I have pasted the code from my manipulation file that loads the audio player. From what I can tell the
- (void)playRecordingBtnClk:(id)sender
{
NSLog(#"Play btn clk");
if (!isRecording) {
// disable all buttons
[_recordButton disableButton];
[_stopButton disableButton];
[_playButton disableButton];
[_saveButton disableButton];
// create the player and play the file from the beginning
if (audioPlayer) {
[audioPlayer release];
audioPlayer = nil;
}
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioRecorder.url error:&error];
audioPlayer.delegate = self;
audioPlayer.currentTime = 0.0;
[audioPlayer prepareToPlay];
if (error) {
NSLog(#"Error Playing Audio File: %#", [error debugDescription]);
return;
}
[audioPlayer play];
[self startTicker];
}
}
It appears that the
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
method is being called after one second which is why the audio player stops. Anyone have any ideas what is going on here?
For iOS 6.0, add two more lines:
soundPlayer.currentTime = soundPlayer.duration + soundPlayer.duration;
soundPlayer.numberOfLoops = 1;
Not a perfect solution. We may need to wait for iOS 6.0.1/6.1 update.
For more, please visit this, someone commented there, saying it might be a problem of CDAudioManager.