Tag Archive for 'tips'

Barcamp 2008 iPhone SDK Demo Source

I hope you enjoyed the brief (!) overview of the iPhone SDK at Barcamp 2008. Here’s a link to the source in case you want to tinker or just get some ideas. This demo is just meant to help you get your feet wet. Don’t forget to use async connections for fetching large files — I can’t stress this enough.

Showing Images on iPhone

Want to show an image on iPhone?


//xpos, ypos, height, width
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(215,255,120,120)];
[r7logo setImage: [UIImage imageNamed:@"my_img.png"]];
[view addSubview: img];
[img release];

“my_img.png” needs to be imported into your project (just drag it onto your xcode project and tell xcode to copy it in).

“view” is a view — if you’re within the context of a view controller, try self.view instead.

Playing AAC+ Streams on iPhone

You can play AAC+ Streams on iPhone just like a standard MP3 stream (Fetch w/ NSURLConnection, pass filled buffers to the AudioQueue), but pass the AudioFileTypeID “kAudioFileAAC_ADTSType” to AudioFileStreamOpen instead of “kAudioFileMP3Type.” How does one go about checking what type of stream they’re dealing with? Check the content-type (parse it out of headers or the initial response data — that depends on the stream server you’re dealing with):

if ([contentTypeLine rangeOfString:@"audio/aac"].length > 0)
{
audioFileType = kAudioFileAAC_ADTSType;

//Pause and resume so we reinitialize the audio stream with the new file type (AAC+)
[self restartAudioQueue];
}

where restartAudioQueue does something along the lines of the following:

-(void)restartAudioQueue {
[self resetAudioQueue];
AudioFileStreamOpen(&audioState, &PropertyListener, &PacketsProc, audioFileType, &audioState.streamID);
}

and resetAudioQueue calls AudioFileStreamClose, AudioQueueStop and AudioQueueReset as well as cleaning up buffers, etc.

Note that it’s not necessary to cancel and recreate your NSURLConnection — it’s (obviously) independent of the AudioQueue.

Don’t forget to parse out your metadata or you’ll get skips!

Update: AAC+ playback seems to be broken in iPhone 2.1. I’ve filed a bug report w/ Apple about playback skipping w/ 2.1 (same code works great w/ 2.0).

Show the Spinner on the Carrier Bar?

Try this:
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

Fade Transition on iPhone

So you might have spent some time trying to figure out how to do a fade transition on iPhone. You probably found sample code and doc for things like curl and flip but not fade in/out, right? Well, it’s actually pretty simple:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
someView.alpha = 0.1;
[UIView commitAnimations];

Bam. You just faded from whatever prior alpha value was set on someView to 0.1 (10%) opacity. The setAnimationDuration method takes a float value in seconds. You could go all the way to 0 to hide it, or set it higher later to fade it back in. This means you can do something like this:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
someImageView.alpha = 0.0;
[UIView commitAnimations];

[someImageView setImage: [UIImage imageNamed:@"anotherImage.png"]];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
someView.alpha = 1.0;
[UIView commitAnimations];

to create a fade transition from one image to the next. The total time from hiding one image to seeing the next would be 1 second, in this case.