MPMoviePlayerController unmutes hardware mute in AVAudioPlayer - ios

I've set my phone to muted state by switching vibrate/sound hardware button. In whole application some background music is playing unless the device is in muted state. So in this case, my music is playing but muted. Now in some point I want to present video (which is without sound but this shouldn't matter), however the video in MPMoviePlayerController turns music from AVAudioPlayer on even though the device is is 'muted' state. The AVAudioPlayer continues to play loudly even when i quit MPMoviePlayerController.
Some code samples, but i doubt it will help:
// movie without sound
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"myVideo" ofType:#"mp4"]];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[_scrollView addSubview:_moviePlayerController.view];
[_moviePlayerController prepareToPlay];
[_moviePlayerController play];
// music
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[_audioPlayer prepareToPlay];
[_audioPlayer play];

To fix this, I had to rewrite the code but instead of using MPMoviePlayerController I've used AVPlayer. Everything works like a charm now.

Related

AVAudioPlayer quiet when changing songs on speaker

I have an AVAudioPlayer playing a song through the speaker. I made the audio play through the speaker like this.
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
Now I have an AVAudioPlayer which I set up like this.
player = [[AVAudioPlayer alloc] initWithContentsOfURL:currentSong.songLocation error:nil];
and I play it like this
[player prepareToPlay];
[player play];
Everything is fine until I change the audio player like this, where I first update currentSong to a new object and init from that
player = [[AVAudioPlayer alloc] initWithContentsOfURL:currentSong.songLocation error:nil];
[player setVolume:1.0f];
[player prepareToPlay];
[player play];
Now the output is quiet, still on the speaker but quiet. If I change the audio output back to AVAudioSessionPortOverrideNone and the back to AVAudioSessionPortOverrideSpeaker, the sound is loud again, but switching the AVAudioPlayer sends me back to "quiet".

iOS playback audioServices

I have a working AudioServicesPlaySystemSound code to run a 10s background music.
Is there a way for me to loop this audio every 10s ? I want it to run forever until an action is triggered to stop the audio (e.g: a button is pressed to stop the loop)
I read an article about dispatch_after and thought that it might be an appropriate way to create the loop.
Switch to AVAudioPlayer instead. Not only can you specify a number of times to play the sound, but you have more control over the sound, including volume level.
It isn't that much harder. For example, here is how you would play a sound, looped indefinitely:
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *player = [{AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
player.numberofLoops = -1;
[player play];
and this is how you would stop it:
[player stop];

Play multiple sounds (one by one) with AVAudioPlayer

I am using AVAudioPlayer to make an MP3 player. I have multiple MP3 sounds and would like to play these sounds one by one. Following is the logic of my app:
///// For playing 1st sound
mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil];
[mp3Player prepareToPlay];
[mp3Player play];
///// For playing 2nd sound
mp3Player = nil;
mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url2 error:nil];
[mp3Player prepareToPlay];
[mp3Player play];
///// For playing 3rd sound
mp3Player = nil;
mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url3 error:nil];
[mp3Player prepareToPlay];
[mp3Player play];
etc...
Just would like to know: I nil out the player and alloc AVAudioPlayer before playing the next sound. Is there any performance (speed) or memory (leak) concern in this logic? (I am using ARC). If so, is there any way to alloc AVAudioPlayer just ONE time, and to accept diffrent URLs pointing to those sounds? Thanks a lot in advance.
I think below link may help you:
How to play multiple audio files in sequence by loop in AVAudioPlayer?
In which audioPlayerDidFinishPlaying is used to play next song after one finishes.

MPMoviePlayerController freezes if background audio is active

In a view controller I have a video playing and also some background music. If the background music is playing, the video freezes when it goes to loop. If the background music is not playing the video loops normally as it should.
Here's how I'm setting up the video player:
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getBundleClip:#"MyVideo"]];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.shouldAutoplay = YES;
self.moviePlayer.repeatMode = MPMovieRepeatModeOne;
The audio is playing via this Sound Manager. Any idea why the video would be freezing?
Try to use play music in background AVAudioPlayer and play same time MPMoviePlayerController like below code for play the AVAudioplayer in repeat mode:
first initialize the AVAudioplayer object in your header file (.h) like below:
#interface Yourviewcontroller : UIViewController
{
//your some codes after
AVAudioPlayer *audioPlayer;
}
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
after implement play audio file in background like below code:
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:#"audio" ofType:#"mp3"]] error:NULL];
audioPlayer.numberOfLoops =-1;//it's play audio repeatedly
audioPlayer.currentTime=0.0f;
[audioPlayer prepareToPlay];
after play audio any part of project like below code:
[audioPlayer play];
you want stop the audio player like below code:
[audioPlayer stop];

Is it possible to change the audio play speed with AVAudioPlayer in iPhone?

I'm playing a local mp3 file with class 'AVAudioPlayer' on my iOS App.
And I want to change the speed when play it. such as I want to play this mp3 file slower.
How to achieve it?
Now it is possible to change the speed of sound.
here is my sample code:
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:path] error:&err];
player.volume = 0.4f;
player.enableRate=YES;
[player prepareToPlay];
[player setNumberOfLoops:0];
player.rate=2.0f;
[player play];
you set "enableRate" to YES and you can change it.
see more docs
That's not possible with AVAudioPlayer. See AVAudioPlayer: How to Change the Playback Speed of Audio? for potential workarounds.

Resources