iPhone UISlider crashing - iphone

I have an UISlider that shows the progress of a song, and the user can go back and forth using it.
I'm using a custom player an the code for my UISlider is the following:
mySlider.value = (float)myAudio.packetPosition / (float)myAudio.totalTimeInBytes;
All works perfectly but sometimes the application crashes, receiving a EXC_BAD_ACESS. Maybe it's a division by zero, I don't know.
How do I prevent this?
Thank you!

if(!(myAudio.totalTimeInBytes == 0.0f))
{
mySlider.value = (float)myAudio.packetPosition / (float)myAudio.totalTimeInBytes;
}
else
{
mySlider.value = 0.0f;
}

Related

UISlider value assigining issue ios

I'm using a UISlider programmatically in a MPMoviePlayerController and set its value with the movie current playback time. This doesn't work properly in some cases, the value of the slider remains zero not changed with the movie's current playback time. Can anyone help me please?
My code is set to fire after each second. Both labels work properly but the UISlider value doesn't get updated and remains zero.
float playbackTime = player.currentPlaybackTime;
float duration = player.duration;
timeLabel.text = [NSString stringWithFormat:#"%.0f / ",playbackTime];
durationlbl.text=[NSString stringWithFormat: #"%.0f",`duration];`
progressSlider.value = playbackTime;
you should probably do this:
progressSlider.minimumValue = 0.0;
progressSlider.maximumValue = player.duration;
you should do this not all the times that you update the slider but only when you initialize the slider or when you start a new video
i resolved this issue by stop the video on close action of the player before play other video.
this issue occur because of previous video state in the player due to which on launching new video slider valur disturbed.

CADisplay link seems to be speeding up each time method is called

I'm working on an iPhone app and in my app there's an object that moves from the top to the bottom of the screen. To do this I use a CADisplay link. Once the object leaves the screen it is supposed to restart its route. The problem that I'm encountering is that each time the object restarts its route, it speeds up. This continues until the object is going so fast that you can barely see it. Any ideas why this is happening and how to stop it? Any help is appreciated, thanks in advance!
-(void)spawnButton{
int x = (arc4random() % (240) + 40;
int y = -100;
button1.center = CGPointMake(x,y);
displayLink = [CADisplayLink displayLinkWithTarget:self selector:#selector(moveObject)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
-(void) moveObject {
int z = 1;
button1.center = CGPointMake(button1.center.x , button1.center.y +z);
if (button1.center.y >= 480) {
[self spawnButton];
}
}
You are creating a new display link on each call to spawnButton. If you're not doing anything to remove the old display link from the run loop, then the old display link will continue sending moveObject: messages. So after two calls to spawnButton you will get two moveObject: messages per video frame, and after three calls you will get three moveObject: messages per video frame, and so on.
I seem to have solved the problem by invalidating the display link each time I restart the objects route.
if (button1.center.y >= 480) {
[self spawnButton];
[displaylink invalidate];
}
}

How to set ICarousel to slide one time for one one image

I am using ICarousel to make my Electronic Album. When you slide the album , the default setting by ICarousel is that it will move to some distance. What I need is slide one time for one one image only. I found ICarousel is not based on ScrollView , So I can not figure out how to achieve my purpose, is someone who know about it?
Updated answer with the more recent versions of iCarousel :
iCarousel now supports single-page swiping by setting
pagingEnabled=YES.
I would recommend turning off the native scrolling and attaching a PanGestureRecognizer that utilizes the scrollByNumberofItems method.
[iCarousel setScrollEnabled:NO];
Then inside your gestureRecognizer:
[iCarousel scrollByNumberOfItems:1 duration:0.25];
I tried this myself and it worked great.
It seems that you have to use another library called SwipeView, implemented by the same author.
The issue was found here.
https://github.com/nicklockwood/iCarousel/issues/247
I achieved that for type iCarouselTypeCoverFlow by setting:
//In ViewController.m
self.carousel.pagingEnabled = YES;
//In iCarousel.m change for smooth animation
-(void)scrollByOffset:(CGFloat)offset duration:(NSTimeInterbal)duration{
if (duration > 0.0)
{
_decelerating = NO;
_scrolling = YES;
_startTime = CACurrentMediaTime();
_startOffset = _scrollOffset;
// _scrollDuration = duration;
// set constant duration instead
_scrollDuration = 1.0;
_endOffset = _startOffset + offset;
if (!_wrapEnabled)
{
_endOffset = [self clampedOffset:_endOffset];
}
[_delegate carouselWillBeginScrollingAnimation:self];
[self startAnimation];
}
else
{
self.scrollOffset += offset;
}
}
Modify iCarousel source code iCarousel.m file may do this!
- (void)didPan:(UIPanGestureRecognizer *)panGesture {
......
case UIGestureRecognizerStateChanged: {
CGFloat translation = _vertical? [panGesture translationInView:self].y: [panGesture translationInView:self].x;
translation = translation * 0.35; // Add This line to change the really translation.
......
}
}
That solve my problem,Hope to help you!

Detect when GameCenter UI is displayed

I'm trying to integrate my game with Game Center and encountered this problem:
When user is authenticated for a first time, Game Center shows its UI for setting up the profile.
My problem is that I can not detect when this windows is shown - I want to pause my game at that moment and not play any sounds.
viewWillDisapper, viewDidDisapper in UIViewController are not called, neither are any of AppDelegate methods are called at this time.
I think I know how detect alert views (using changing key window notification), but that Account windows still is not detected there.
Is there any way to do this?
Building on executor21's answer here, I put this together which seems to do the trick in early testing. You can probably adapt it into something less fragile. It is built on the premise that the Game Center notification gets its own window, and it has exactly one subview of type GKGameEventView:
+(BOOL)isGameCenterNotificationUp
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *win in windows)
{
NSArray *winSubViews = [win subviews];
if([winSubViews count] == 1)
{
Class gcNotificationClass = NSClassFromString(#"GKGameEventView");
if(gcNotificationClass && ([[winSubViews objectAtIndex:0] isKindOfClass:gcNotificationClass]))
{
return YES;
}
}
}
return NO;
}

Drumming app - Laggy Sounds... How To Fix?

I have a drum app that I've created basing it off of AVAudioPlayer instead of the common system sounds in order to have a bit of control.
The problem is that whenever two or more sounds are played at once, it lags and stops alls sounds, so your drum beat gets choppy and randomly stopped.
For example: boom boom chhhhh boom boom ch--- boom bo---- chhhhh
If you can tell at all from that onomatopoeia.
Here's the sound code, I have an array of preloaded AVAudioPlayer so that it loads faster.
- (void)triggerSound:(NSInteger)soundNumber {
NSInteger deltaNum = soundNumber*numberOfBuffers;
AVAudioPlayer *lowBuffer = [bufferBox objectAtIndex:deltaNum];
Boolean soundFired = FALSE;
// Find an unused buffer if possible, otherwise play sound from first buffer.
for (int i=0; i<numberOfBuffers; i++) {
NSLog(#"Buffer loop: %d", i);
NSLog(#"Buffer to load: %d", deltaNum);
AVAudioPlayer *tempBuffer = [bufferBox objectAtIndex:deltaNum+i];
if (!tempBuffer.playing) {
tempBuffer.currentTime = 0;
[tempBuffer play];
soundFired = TRUE;
break;
} else if (lowBuffer.currentTime>tempBuffer.currentTime) {
lowBuffer = tempBuffer;
}
}
if (!soundFired) {
lowBuffer.currentTime = 0;
[lowBuffer play];
soundFired = TRUE;
}
}
That method is called in the IBAction button press. ex: [drumObject triggerSound:3].
All help appreciated!
I've had much better experiences with the OpenAL API for some games. It is very different and more low level (you have to deal with sources and buffers) but it worked much better for me than AVAudioPlayer.
You could try using an iOS game engine for your audio needs.
For example: http://gamua.com/sparrow/
You don't actually have to use the graphics part of this engine. Just import and use the audio part of the framework (which provides a simple API on top of OpenAL).