Using vibrate and AVCaptureSession at the same time - iphone

I'm trying to vibrate the phone while recording video, but I'm finding that AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); doesn't play nicely with AVCaptureSession. Is there any other way to vibrate the phone or am I stuck with losing the vibrate function while recording video?

You probably need to set the audio to mix with other, I found this useful:
- (void)setupAudio {
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
}
from here

Related

ios remote sometimes defaults to native music player instead of my app

About 1 in 20 times my app (after launch) does not seems to 'beginReceivingRemoteControlEvents' and hence the ios remote defaults to the native music player. Because of this my app has been rejected (at an update, not initial release), the thing is I can never see it happen when the iPhone is plugged into mac and xcode only when running the app on the phone, below is my audio initialization routine.
Hope someone can help, thanks Mike.
-(void)viewDidAppear:(BOOL)animated
{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
[[AVAudioSession sharedInstance] setDelegate: self];
//Set the audio category of this app to playback.
NSError *setCategoryError = nil; [[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
{
ShowMessageBox(#"Audio error", #"Category failed");
}
//Activate the audio session
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
if (activationError) {
ShowMessageBox(#"Audio error", #"Activate failed");
}
}
// And also
- (BOOL)canBecomeFirstResponder
{
return YES;
}

Airplay using AVPlayer does not always work. 'iPhone' option listed twice in the Airplay menu

NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
//Direct audio to speakers when there is no headphone
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[player setAllowsExternalPlayback:YES];
[player setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[player setAllowsAirPlayVideo:NO];
The above is my code trying to set the appropriate session to enable Airplay. It works sometimes, but regardless of whether it works or not, it usually lists 'iPhone' twice in the Airplay menu, sometimes not showing the actual Airplay device. Tapping either of those two duplicate options does not seem to do anything. Also the play icon on the status bar sometimes appears, and sometimes it doesn't. I am guessing the session is not getting set properly every time.
Can anybody kindly tell me what I am doing wrong here?! Also, if I want to enable playing over Bluetooth, do I need to implement some other delegate?
I see your post today, using your code in this mode:
In your AppDelegate .M this:
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
[[AVAudioSession sharedInstance] setActive:NO error:nil];
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
And in your View.m using this:
[player setAllowsExternalPlayback:YES];
[player setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[player setAllowsAirPlayVideo:NO];//this is deprecated in iOS 6.0
Hope this help you or future people!
I see but is simple and strange in same time :) you have to only remove the funcion.
this method:
[player setAllowsExternalPlayback:YES];
[player setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[player setAllowsAirPlayVideo:NO];//this is deprecated in iOS 6.0
become to:
[player setAllowsExternalPlayback:YES];
[player setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
in your info.plist allow Required Background Modes a and set: App Plays Audio and App Communicates with an accessory, then add one row and set: Application uses WiFi.
That's that's all ;)
PS: is possibile you can helpme on my problem here thanks.

How to play even background mode using MPMoviePlayerController

Audio Live Streaming Service using IIS7 is prepared.
Audio Live streaming server setting is very complex, but have succeeded.
I obtained a URL for streaming like this(#"http://xxx.xxx.xx.xx/liveStream.isml/manifest(format=m3u8-aapl).m3u8").
My using code is unexpectedly simple.
self.theURL = [NSURL URLWithString:#"http://xxx.xxx.xx.xx/liveStream.isml/manifest(format=m3u8-aapl).m3u8"];
if(moviePlayer == nil)
{
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
}
[moviePlayer play];
Hm... I can't control music player as I wished.
Question is like this.
I don't want to stop to play music even though backgroundMode.
Please tell me some advice. Thanks.
please check in only device.I have also done in my Application. My code is given below.i am talking about iOS5
And in plist i made an array named "Required background mode" and insert an item in array name, "App Plays Audio". some times URL problem is occurs check this URL MPMoviePlayer sound working in Simulators and ipad device, but not Working in iPhone Device
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
}

How to resume AVAudioPlayer after interrupted in background

I am playing music using AVAudioPlayer in background. The problem is: if there is a incoming calling interrupts the player, it will never resume unless switch to foreground and do it manually.
The code is simple, to play it in background:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
url = [[NSURL alloc] initFileURLWithPath:...];
audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:NULL];
audio_player.delegate = self;
bool ret = [audio_player play];
delegate to handle interruptions:
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
//tried this, not working [[AVAudioSession sharedInstance] setActive: NO error: nil];
NSLog(#"-- interrupted --");
}
//----------- THIS PART NOT WORKING WHEN RUNNING IN BACKGROUND ----------
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
NSLog(#"resume!");
//--- tried, not working: [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
//--- tried, not working: [[AVAudioSession sharedInstance] setActive: YES error: nil];
//--- tried, not working: [audio_player prepareToPlay];
[audio_player play];
}
Can any one help me?
Found the solution!
I had the same problem, my app was resuming the audio nicely after an interruption, only if my app was open. When it was on the background it failed ro resume playing the audio after an interruption.
I fixed this by adding the following lines of code:
Add this line whenever your app start playing audio.[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
And on the endInterruption method, wait 1 or 2 seconds before resume playing the audio. This to allow the OS to stop using the audio channel.
- (void)endInterruptionWithFlags:(NSUInteger)flags {
// Validate if there are flags available.
if (flags) {
// Validate if the audio session is active and immediately ready to be used.
if (AVAudioSessionInterruptionFlags_ShouldResume) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1), dispatch_get_main_queue(), ^{
// Resume playing the audio.
});
}
}
}
You can also add this line when your app stops (not pause) playing audio. But is not required.
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Try this
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer withFlags:(NSUInteger)flags{
if (flags == AVAudioSessionFlags_ResumePlay) {
[audioPlayer play];
}
Hope it helps.

AVAssetWriter / AVAudioPlayer Conflict?

Weeks ago, I posted this thread regarding problems I was having with AVAssetWriter: AVAssetWriter Woes
Further research seems to lead to a conflict using AVAssetWriter while playing audio with AVAudioPlayer or, really, any audio system. I tried with OpenAL as well.
Here's the background:
Using AVAssetWriter to write frames to a video from an image or set of images works fine UNTIL [AVAudioPlayer play] is called.
This only happens on the device, not the sim.
The error occurs when attempting to create a pixel buffer from CVPixelBufferPoolCreatePixelBuffer.
Once the audio starts playing, the AVAssetWriterInputPixelBufferAdaptor.pixelBufferPool which existed before suddely becomes nil.
You can download the representative project here: http://www.mediafire.com/?5k7kqyvtbfdgdgv
Comment out AVAudioPlayer play and it will work on the device.
Any clues are appreciated.
I've found the solution to this issue.
If you want to have AVAudioPlayer and AVAssetWriter behave correctly together, you must have and audio session category that is 'mixable'.
You can use a category that is mixable like AVAudioSessionCategoryAmbient.
However, I needed to use AVAudioSessionCategoryPlayAndRecord.
You can set any category to be mixable by implementing this:
OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryMixWithOthers, // 1
sizeof (allowMixing), // 2
&allowMixing // 3
);
This above answer is in complete. It doesn't work. Do this instead
// Setup to be able to record global sounds (preexisting app sounds)
NSError *sessionError = nil;
if ([[AVAudioSession sharedInstance] respondsToSelector:#selector(setCategory:withOptions:error:)])
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDuckOthers error:&sessionError];
else
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
// Set the audio session to be active
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
//then call your asset writer
movieWriter = [[AVAssetWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];