MPMoviePlayerController freezes if background audio is active - ios

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];

Related

AVAudioPlayer crashes my app with EXC_BAD_ACCESS(code=1) when running multiple sounds at once

Hello my sounds play but if I keep hitting multiple sounds rapidly I get a crash EXC_BAD_ACCESS(code=1). Been trying for a long time to figure this one out. I tried using properties as well but no luck. Anybody ever seen this happen when playing multiple simultaneous sounds?
#interface SoundBoardScene : SKScene<AVAudioPlayerDelegate>
AVAudioPlayer *Player1;
AVAudioPlayer *Player2;
AVAudioPlayer *Player3;
AVAudioPlayer *Player4;
AVAudioPlayer *Player5;
AVAudioPlayer *Player6;
AVAudioPlayer *Player7;
AVAudioPlayer *Player8;
-(AVAudioPlayer*) PlayAudioFile:(AVAudioPlayer*)player withAudioFileName:(NSString*)audioFileName withExtensionType: (NSString*)audioFileExtension withLoopCount:(uint)NumberOfLoops
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:audioFileName
ofType:audioFileExtension]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
player.numberOfLoops = NumberOfLoops;
return player;
}
-(void) initSounds
Player1 = [animalObject PlayAudioFile:Player1 withAudioFileName:#"Sound1" withExtensionType:#"mp3" withLoopCount:0];
Player1.delegate=self;
Player2 = [animalObject PlayAudioFile:Player2 withAudioFileName:#"Sound2" withExtensionType:#"mp3" withLoopCount:0];
Player2.delegate=self;
etc
etc
}
if([node.name isEqualToString:#"Sound1"])
{
[Player1 play];
}
if([node.name isEqualToString:#"Sound2"])
{
[Player2 play];
}
there is no need to use avplayer for play sound file.
try this code
[self runAction:[SKAction repeatAction:[SKAction playSoundFileNamed:#"pew1.wav" waitForCompletion:NO] count:count]];

How to mute a audio sound when an action is tapped?

Adcolony is a video advert platform integrated into Xcode to play ads to users in video and sound,
One of the requirements is to block audio when the add starts playing,
This is the audio:
//Audio Sound On - uncomment
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"soundTrack"
ofType:#"mp3"]];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
it starts as soon as the game starts, in view controller,
The triggerVideo action is place in PlayViewController and needs to be set off when ad is triggered,
- (IBAction)TriggerVideo:(id)sender {
[AdColony playVideoAdForZone:#"ZONE-NUMBER-HERE" withDelegate:nil];
}
I've left this aside for a while now, wondering if anyone knows how to employ this function?
add these methods to your viewController that's the delegate for adcolony, which should also be the viewcontroller in which you initialize the AVAudioPlayer. I probably don't need to state this, but i'm assuming you have an #Property for your audioPlayer
- (void) onAdColonyAdStartedInZone:(NSString *)zoneID{
self.audioPlayer.volume = 0.0f;
}
- (void) onAdColonyAdAttemptFinished:(BOOL)shown inZone:(NSString *)zoneID{
self.audioPlayer.volume = 1.0f;
}

MPMoviePlayerController unmutes hardware mute in AVAudioPlayer

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.

iOS - Play audio/sound while record a video

I need to play audio sound from the main speaker of the iPhone while the user is recording a video. Is it possible? How can i do?
I need the sound is being played from the main speaker so in that way I can record this sound in my video.
Have a look at this answer!
He was already using video and sound the way you want, except the routing was wrong... An answer there has a code snipped that should do the trick.
Yes you can do it by setting AVAudioSession class property and using AVAudioPlayer to play sound
Set AVAudioSession category property to AVAudioSessionCategoryPlayAndRecord and also set its category options to AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers this will solve the problem. Please check the below set of code
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"VoicemailSound" ofType:#"mp3"]]; //Download and add a system sound to your project and mention its name and type here
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error: nil];
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];

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.

Resources