UIAlertView *alert=[[UIAlertView alloc]initWithTitle:
@"Test Alert" message:msg
delegate:nil
cancelButtonTitle:@"ok" otherButtonTitles:nil];
alert.transform = CGAffineTransformTranslate( alert.transform, 0, 240 );
[alert release];
Archive for the ‘Objective C’ Category
// Creating a Mutable dictionary with the contents of plist
NSMutableDictionary *videoDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"dict.plist"]];
// To check whether the key is already there
BOOL alreadyThere; alreadyThere = FALSE;
// Iterating through set of keys in the mutable dictionary
for(NSString* key in videoDictionary){
if ([key isEqualToString:keyString]) {
alreadyThere = TRUE; // If the key is already there.
}
}
if (!alreadyThere) {
NSLog(@"alreadynotthere");
// If the key is already not there, then add the value for key to the mutable dictionary.
[videoDictionary setObject:path forKey:keyString];
}
else {
NSLog(@"alreadythere");
// If the key is already there.
[videoDictionary removeObjectForKey:keyString]; // Removing the existing key and value from the mutable dictionary
[videoDictionary setObject:path forKey:keyString]; // Adding the new value with the key
}
NSLog(@"Video dict = %@",videoDictionary);
// Writing the dictionary to plist
[videoDictionary writeToFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"dict.plist"] atomically:YES];
Following are some useful UIDevice properties which can be printed on console as follows:
NSLog(@”Name: %@”, [UIDevice currentDevice].name); NSLog(@” Model: %@”, [UIDevice currentDevice].model); NSLog(@”System Name: %@”, [UIDevice currentDevice].systemName); NSLog(@”System Version: %@”, [UIDevice currentDevice].systemVersion); NSLog(@”UniqueID: %@”, [UIDevice currentDevice].uniqueIdentifier);
You need to add CoreAudio.framework to implement recording.
Also import following headers in .h file
AVFoundation/AVFoundation.h
CoreAudio/CoreAudioTypes.h
declare below in .h file itself
AVAudioRecorder * recorder;
NSError * error;
NSURL * recordedTmpFile;
Now add the following function in .m file to startRecording
-(void)StartRecording{
//Record setting in MutableArray
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
//Now that we have our settings we are going to instanciate an instance of our recorder instance.
//Generate a temp file for use by the recording.
recordedTmpFile = [NSURL fileURLWithPath:
[NSTemporaryDirectory() stringByAppendingPathComponent:
[NSString stringWithFormat:
@"Myrecording%d.%@",i++, @"caf"]]];
NSLog(@"Using File called: %@",recordedTmpFile);
//Setup the recorder to use this file and record to it.
recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
//Use the recorder to start the recording.
[recorder setDelegate:self];
//We call this to start the recording process and initialize
[recorder prepareToRecord];
//Start the actual Recording
[recorder record];
}
To stop the recording and play add following function
-(void)StopRecording{
[recorder stop];
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
[avPlayer prepareToPlay];
[avPlayer play];
}
There is an optional method for doing the recording for a limited time see :
[recorder recordForDuration:(NSTimeInterval) 10];
If you want to Play sound using cocoa Mac, then use the following code,
[yourSound play];
where yourSound is the instance of NSSound initialized with an audio file.
If you want to Cancel/Stop the sound using cocoa Mac, then use the following code,
[yourSound stop];
where yourSound is the instance of NSSound initialized with an audio file.
If you want to play sound in Cocoa Mac, then you need to create instance of NSSound using soundNamed:
For example if you want to play the sound file ‘yourAudioFile’ in the Resources folder, then use the following code in cocoa Mac
NSSound *yourSound = [NSSound soundNamed:@"yourAudioFile"];
If you want to add contents to a NSPopUpButton using cocoa Mac, then use the following line of code,
[newPopUp addItemsWithTitles: contentsArray];
where newPopUp is the name of your NSPopUpButton & contentsArray is a NSArray containing all the objects you want to add to the NSPopUpButton.
If you want to pause the playing audio, then use the following line of code,
[playingSound pause];
where playingSound is the instance of NSSound which is playing.
If you want to resume the paused audio, then use the following line of code,
[playingSound resume];
where playingSound is the instance of NSSound which is being paused.
This warning : Multiple Build Commands For Output File’ occurs when you only ‘delete reference’ than ‘move to trash’ while deleting files from ‘Resources’ folder of your project or the Copy Bundle Resources still holds the references of the same file you have deleted and then added again.
Fix : Go to the Targets->Your Project Name->Copy Bundle Resources
From ‘Copy Bundle Resources’ delete those files whose reference are still there or files which shows the warning.
If you want to get the index of selected item from a NSPopUpButton in cocoa Mac Programming then, use the following code.
NSInteger myIndex = [sender indexOfSelectedItem]; NSLog(@"Selected item index is %i", myIndex);
where ‘myIndex’ is the your required index of items from the NSPopUpButton.




