iPhone:Strange Issue with videoMaximumDuration property in Video recorder API - iphone

I am facing a strange issue with videoMaximumDuration property in Video recorder API. I am trying to fix this issue more than a week, but couldn't. I posted in several forums, but no help yet.
I am using the following code to launch Camera from my application and start recording the video. As per my requirement, i should set the time duration for recording the video to stop. So, i use "videoMaximumDuration" property to set the time duration for recording the video.
if ([types containsObject:(id)kUTTypeMovie])
{
appDelegate.pickerController = [[UIImagePickerController alloc] init];
appDelegate.pickerController.delegate = self;
appDelegate.pickerController.videoQuality = setVideoQuality;
appDelegate.pickerController.allowsEditing = NO;
appDelegate.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
appDelegate.pickerController.showsCameraControls = YES;
appDelegate.pickerController.navigationBarHidden = YES;
appDelegate.pickerController.toolbarHidden = YES;
appDelegate.pickerController.wantsFullScreenLayout = YES;
appDelegate.pickerController.cameraViewTransform =
CGAffineTransformScale(appDelegate.pickerController.cameraViewTransform,
CAMERA_TRANSFORM_X,
CAMERA_TRANSFORM_Y);
appDelegate.pickerController.mediaTypes = [NSArray arrayWithObject:(id)kUTTypeMovie];
appDelegate.pickerController.videoMaximumDuration = maxDuration;
[self presentModalViewController:appDelegate.pickerController animated:YES];
}
Here is the issue:
If if i set videoQuality as UIImagePickerControllerQualityTypeHigh, then the time duration(videoMaximumDuration) works as expected, i.e exactly after the time duration it stops recording the video automatically. If i go and see that recorded video in Photo album to make sure that, it is recorded well as per the time.
If i change the videoQuality to UIImagePickerControllerQualityTypeMedium (or) UIImagePickerControllerQualityTypeLow like that, then the time duration(videoMaximumDuration) for video recording is not working as expected,
i.e. it is able to automatically stop the video recording at the time duration set, no issues, But if i go and see that recorded video in Photo album to make sure, it is NOT as per the time taken, rather i can see the smaller video than what i recorded. For example, if i set the videoMaximumDuration for 30 seconds, after recording the video, if i go and see that recorded video in Photo album, it could able to record= only unto 22 seconds. Seems to be issue with the API itself. This is not happening when i use video quality as UIImagePickerControllerQualityTypeHigh.
I tried even using Custom overlay view and do start and stop recording video through code like below by setting timer(NStimer). But still i see the same issue has observed.
overlay = [[OverlayView alloc]initWithFrame:CGRectMake(0, 0, 768, 1024)];
if ([types containsObject:(id)kUTTypeMovie])
{
appDelegate.pickerController = [[UIImagePickerController alloc] init];
appDelegate.pickerController.delegate = self;
appDelegate.pickerController.videoQuality = setVideoQuality;
appDelegate.pickerController.allowsEditing = NO;
appDelegate.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
appDelegate.pickerController.showsCameraControls = NO;
appDelegate.pickerController.navigationBarHidden = YES;
appDelegate.pickerController.toolbarHidden = YES;
appDelegate.pickerController.wantsFullScreenLayout = YES;
appDelegate.mTimerSelectionForVideo = maxDuration; // TIME SET HERE IN VARIABLE
appDelegate.pickerController.cameraViewTransform =
CGAffineTransformScale(appDelegate.pickerController.cameraViewTransform,
CAMERA_TRANSFORM_X,
CAMERA_TRANSFORM_Y);
appDelegate.pickerController.mediaTypes = [NSArray arrayWithObject:(id)kUTTypeMovie];
appDelegate.pickerController.videoMaximumDuration = maxDuration;
[self presentModalViewController:appDelegate.pickerController animated:YES];
appDelegate.pickerController.cameraOverlayView =overlay;
}
OverlayView.m
-(void)startAction:(id)sender
{
BOOL bStop = TRUE;
void (^hideControls)(void);
hideControls = ^(void) {
cameraSelectionButton.alpha = 0;
startButton.enabled = NO;
lbl.hidden = NO;
};
void (^recordMovie)(BOOL finished);
recordMovie = ^(BOOL finished) {
stopButton.enabled = YES;
[appDelegate.pickerController startVideoCapture];
};
// Hide controls
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:hideControls completion:recordMovie];
if ( appDelegate.mTimerSelectionForVideo==0 )
{
bStop = FALSE;
}
if ( bStop )
timer = [NSTimer scheduledTimerWithTimeInterval:(appDelegate.mTimerSelectionForVideo)+1
target:self selector:#selector(stopCamera:) userInfo:nil repeats:NO];
}
- (void)stopCamera:(NSTimer *)theTimer
{
startButton.enabled = YES;
if ( timer )
{
[timer invalidate];
timer = nil;
}
[appDelegate.pickerController stopVideoCapture];
[appDelegate.pickerController dismissModalViewControllerAnimated:YES];
}
But still i see the same issue observed. Why does other video quality settings not working as per the videoMaximumDuration set?
I tested on iPhone 4.1 and iPad 4.3, the same issue has been observed. Looks like, issue with the API or video recorder hardware itself to support it.
Could someone please guild me to fix this issue if there is any possibility (or) through your experience?
Thank you in Advance!

Fixed it by creating an overlay view on top of the camera view and handle start/stop thru code.

Related

SpriteKit App Using Excessive CPU

I wrote a SpriteKit app last year targeting 10.10 (Yosemite). Everything ran fine, but when I upgraded to El Capitan this year it freezes in one particular spot. It's a tough problem to diagnose because there is a lot of code so I'll try to be as descriptive as possible. I've also created a YOUTUBE screen recording of the issue.
App's Purpose
The app is basically a leaderboard that I created for a tournament at the school that I teach at. When the app launches, it goes to the LeaderboardScene scene and displays the leaderboard.
The app stays in this scene for the rest of the time. The sword that says "battle" is a button. When it is pressed it creates an overlay and shows the two students that will be facing each other in video form (SKVideoNode).
The videos play continuously and the user of the app eventually clicks on whichever student wins that match and then the overlay is removed from the scene and the app shows the leaderboard once again.
Potential Reasons For High CPU
Playing video: Normally the overlay shows video, but I also created an option where still images are loaded instead of video just in case I had a problem. Whether I load images or video, the CPU usage is super high.
Here's some of the code that is most likely causing this issue:
LeaderboardScene.m
//when the sword button is pressed it switches to the LB_SHOW_VERSUS_SCREEN state
-(void) update:(NSTimeInterval)currentTime {
switch (_leaderboardState) {
...
case LB_SHOW_VERSUS_SCREEN: { //Case for "Versus Screen" overlay
[self showVersusScreen];
break;
}
case LB_CHOOSE_WINNER: {
break;
}
default:
break;
}
}
...
//sets up the video overlay
-(void) showVersusScreen {
//doesn't allow the matchup screen to pop up until the producer FLASHING actions are complete
if ([_right hasActions] == NO) {
[self addChild:_matchup]; //_matchup is an object from the Matchup.m class
NSArray *producers = #[_left, _right];
[_matchup createRound:_round WithProducers:producers VideoType:YES]; //creates the matchup with VIDEO
//[_matchup createRound:_round WithProducers:producers VideoType:NO]; //creates the matchup without VIDEO
_leaderboardState = LB_CHOOSE_WINNER;
}
}
Matchup.m
//more setting up of the overlay
-(void) createRound:(NSString*)round WithProducers:(NSArray*)producers VideoType:(bool)isVideoType {
SKAction *wait = [SKAction waitForDuration:1.25];
[self loadSoundsWithProducers:producers];
[self runAction:wait completion:^{ //resets the overlay
_isVideoType = isVideoType;
[self removeAllChildren];
[self initBackground];
[self initHighlightNode];
[self initOutline];
[self initText:round];
if (_isVideoType)
[self initVersusVideoWithProducers:producers]; //this is selected
else
[self initVersusImagesWithProducers:producers];
[self animationSequence];
_currentSoundIndex = 0;
[self playAudio];
}];
}
...
//creates a VersusSprite object which represents each of the students
-(void) initVersusVideoWithProducers:(NSArray*)producers {
Producer *left = (Producer*)[producers objectAtIndex:0];
Producer *right = (Producer*)[producers objectAtIndex:1];
_leftProducer = [[VersusSprite alloc] initWithProducerVideo:left.name LeftSide:YES];
_leftProducer.name = left.name;
_leftProducer.zPosition = 5;
_leftProducer.position = CGPointMake(-_SCREEN_WIDTH/2, _SCREEN_HEIGHT/3);
[self addChild:_leftProducer];
_rightProducer = [[VersusSprite alloc] initWithProducerVideo:right.name LeftSide:NO];
_rightProducer.name = right.name;
_rightProducer.zPosition = 5;
_rightProducer.xScale = -1;
_rightProducer.position = CGPointMake(_SCREEN_WIDTH + _SCREEN_WIDTH/2, _SCREEN_HEIGHT/3);
[self addChild:_rightProducer];
}
VersusSprite.m
-(instancetype) initWithProducerVideo:(NSString*)fileName LeftSide:(bool)isLeftSide {
if (self = [super init]) {
_isVideo = YES;
_isLeftSide = isLeftSide;
self.name = fileName;
[self initVideoWithFileName:fileName]; //creates videos
[self addProducerLabel];
}
return self;
}
...
//creates the videos for the VersusSprite
-(void) initVideoWithFileName:(NSString*)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDesktopDirectory, NSUserDomainMask, YES);
NSString *desktopPath = [paths objectAtIndex:0];
NSString *resourcePath = [NSString stringWithFormat:#"%#/vs", desktopPath];
NSString *videoPath = [NSString stringWithFormat:#"%#/%#.mp4", resourcePath, fileName];
NSURL *fileURL = [NSURL fileURLWithPath:videoPath];
AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:fileURL];
_vid = [SKVideoNode videoNodeWithAVPlayer:avPlayer];
//[_vid setScale:1];
[self addChild:_vid];
[_vid play];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[avPlayer currentItem]];
}
//used to get the videos to loop
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
UPDATE
The issue has been identified and is very specific to my project, so it probably won't help anyone else unfortunately. When clicking on the "sword" icon that says "Battle", the scene gets blurred and then the overlay is put on top of it. The blurring occurs on a background thread as you'll see below:
[self runAction:[SKAction waitForDuration:1.5] completion:^{
[self blurSceneProgressivelyToValue:15 WithDuration:1.25];
}];
I'll have to handle the blur in another way or just remove it altogether.

Slow preformance during replaceScene

I'm facing a performance issue whenever the code reach "replaceScene". It happens only in the Play Scene. So after the game is over, I display a score, and then, it's time for CCDirector to do replaceScene in order to go back to the main menu.
After waiting for around 20 seconds then it finally display the main menu. But somehow this is not right, player will feel that the game suddenly hang. Then I tried to put some animation like preloader, it happens the same, the preloader picture did animate a while then suddenly stop, and I think due to the same issue triggered by replaceScene, although still it'll display the main menu scene. Care to give some tips how to speed up the releasing of all the objects which no longer needed.
Hoping to get a solution from experts here. Thanks.
Here is my code :
............
//button at the score pop up sprite
CCMenuItem *btContinue = [CCMenuItemImage itemFromNormalImage:BTCONTINUE
selectedImage:BTCONTINUE_ON
target:self
selector:#selector(goLoader)];
btContinue.anchorPoint = ccp(0,0);
btContinue.position = ccp(340, 40);
CCMenu *menu = [CCMenu menuWithItems:btContinue, nil];
menu.position = CGPointZero;
[self addChild:menu z:ZPOPUP_CONTENT];
//prepare the loader, but set visible to NO first
CCSprite *loaderBg = [CCSprite spriteWithFile:LOADER_FINISH];
loaderBg.anchorPoint = ccp(0,0);
loaderBg.position = ccp(0,0);
loaderBg.visible = NO;
[self addChild:loaderBg z:ZLOADER_BG tag:TAG_LOADER_BG];
NSLog(#"prepare loader finish");
//animate loader
CCSprite *loaderPic = [[CCSprite alloc] initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:LOADER]];
loaderPic.anchorPoint = ccp(0.5,0.5);
loaderPic.position = ccp(200,35);
loaderPic.visible = NO;
[self addChild:loaderPic z:ZLOADER_PIC tag:TAG_LOADER_PIC];
[loaderPic runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:0.05f angle:10.0f]]];
}
-(void)goLoader {
NSLog(#"goMainMenuScene");
CCSprite *tmpBg = (CCSprite *) [self getChildByTag:TAG_LOADER_BG];
if (tmpBg != nil)
tmpBg.visible = YES;
CCSprite *tmpPic = (CCSprite *) [self getChildByTag:TAG_LOADER_PIC];
if (tmpPic != nil)
tmpPic.visible = YES;
double time = 2.0;
id delay = [CCDelayTime actionWithDuration: time];
id proceed = [CCCallFunc actionWithTarget:self selector:#selector(goMainMenuScene)];
id seq = [CCSequence actions: delay, proceed, nil];
[self runAction:seq];
}
-(void)goMainMenuScene {
[[GameManager sharedGameManager] runSceneWithID:SCENE_MAIN_MENU];
}
Your problem is most likely the new scene, and whatever happens in the new scene's and its child node's init methods. Loading resource files can take quite a while. If you defer doing this into onEnter you might see better results. But 20 seconds, that's a lot. Check what you're doing that takes THIS long. I bet it's loading a gross amount of resources, or loading them in an extremely inefficient way. JPG files are known to load very slowly, if you use JPG convert them to PNG.

Slider stop at the end of playing audio

In my project I am using an AVAudioPlayer to play my saved audio.I am showing the slider for playing audio. The value of the slider is set using the current time property of the player.
playSlider.value = avAudioPlayer.currentTime;
This is the timer code
updateTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(updateCurrentTime) userInfo:p repeats:YES];
//Calls the update current time function
- (void)updateCurrentTime
{
[self updateCurrentTimeForPlayer:self.player];
}
//Updates the current time while the audio is playing
-(void)updateCurrentTimeForPlayer:(AVAudioPlayer *)avAudioPlayer
{
currentTimeLabel.text = [NSString stringWithFormat:#"%d:%02d", (int)avAudioPlayer.currentTime / 60, (int)avAudioPlayer.currentTime % 60, nil];
playSlider.value = avAudioPlayer.currentTime;
if (playSlider.value==playSlider.minimumValue)
{
moveToBeginningButton.enabled = NO;
rewindButton.enabled = NO;
NSLog(#"PlaySliderMaxValue1 === %f",playSlider.maximumValue);
NSLog(#"PlaySliderValue1 === %f",playSlider.value);
}
else
{
moveToBeginningButton.enabled = YES;
rewindButton.enabled = YES;
NSLog(#"PlaySliderMaxValue2 === %f",playSlider.maximumValue);
NSLog(#"PlaySliderValue2 === %f",playSlider.value);
}
}
The maximum value of slider is set as follows
playSlider.maximumValue = audioPlayer.duration;
The problem that I am experiencing is that the slider point will always return to the beginning of the audio after playing.Is this a bug? How can I change this? Please provide your suggestion.
I cannot say whether this is a bug or not, But try using delegate methods of AVAudioPlayer. In didFinishPlaying method set Slider value to maximumValue. This could be one of the solution.
Print the value of avAudioPlayer.currentTime before assigning it to playslider.value
I think at the end it will become zero. and it will assign zero to playslider.value

iPhone:Video recording based on timer?

I have implemented the code to do video recording feature. It works fine on 3GS device. I want to restrict the video recording based on some timer setting. Lets say, i want to allow user to do video recording only upto 20 seconds or 35 seconds like that. How can i do that? Can i show the timer kind of control on top of media player while recording the video?
Please suggest me.
Here is my code for video recording:
UIImagePickerController *pickerController =
[[[UIImagePickerController alloc] init] autorelease];
pickerController.delegate = self;
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerController.showsCameraControls = YES;
pickerController.mediaTypes = [NSArray arrayWithObject:(id)kUTTypeMovie];
[self presentModalViewController:pickerController animated:YES];
videoMaximumDuration expects an NSTimeInterval, which is a tpyedef for a float value. So you should pass it a float value. Try it like this:
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
pickerController.delegate = self;
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerController.showsCameraControls = YES;
pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie]; // kUTTypeMovie is actually an NSString.
pickerController.videoMaximumDuration = 30.0f; // limits video length to 30 seconds.
[self presentModalViewController:pickerController animated:YES];
[pickerController release];
UIImagePickerController class has property videoMaximumDuration which works for NSTimeInterval.
By default it's value is set for 10 minutes but you can change it's float value according to your need.
So if you make object of UIImagePickerController class named videoPickerController then you can set timer for capturing video like-
videoPickerController.videoMaximumDuration = 25.0f;

UIImagePickerController not working for iOS 3 yet works for iOS 4...backwards compatibility issue

So I created an entire app and it works flawlessly the way I envisioned. Created an update that uses UIImages, and that works perfectly as well ONLY ON iOS 4 and above. For some reason the exact same code will not work the same on 3.0. Since the original app is on the store at min OS being 3, I do not want to just cut off my user base because of some silly mistake on my part. I'm not doing anything new, so it should all be backwards compatible.
Basically the app takes an image chosen from either the camera or the photo album and then saves it, and displays the image on the screen. When you go to the next screen it pulls the image file off the disk and displays it in the image view on the next screen. Again, works fine on iOS4 and above.
Using the 3.0 sim on an older version of xcode, it just will not function the same way. I know allowImageEditing changed to allowEditing, so I allowed for that, but everything else it seems according to the docs, should work on iOS 3 and above.
Below is my going to photo album and returning.
-(IBAction) getPhotoFromAlbum {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if (editingSwitch.on) {
#ifdef __IPHONE_3_0
picker.allowsImageEditing = YES;
#else
picker.allowsEditing = YES;
#endif
} else if (!editingSwitch.on) {
#ifdef __IPHONE_3_0
picker.allowsImageEditing = NO;
#else
picker.allowsEditing = NO;
#endif
}
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *image = nil;
if (editingSwitch.on) {
image = [info objectForKey:#"UIImagePickerControllerEditedImage"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:isImageEdited];
} else if (!editingSwitch.on) {
image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:isImageEdited];
}
imageView.image = [self imageWithImage:image];
imageView.hidden = NO;
buttonPreview.hidden = YES;
[NSThread detachNewThreadSelector:#selector(myThreadSavingImage) toTarget:self withObject:nil];
[activitySaving startAnimating];
When I debug, it seems the info that is coming back from the picker only has 1 key. When it sets the image, the image is blank. Basically no image ever comes back and if for some rare occasion it does, like when I edit the picture and not just use the original, it does not show up on the next page.
Hopefully everyone can help as the app is finished besides this 3.0 issue and its frustrating as I want to get it out soon.
Turns out the answer is that it was not working on the simulator due to the updates to the xcode program. When actually installed onto a 3.0 device, the code worked perfectly.