How to PrepareforSegue for multiple objects : UIImageView and audiofile - iphone

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"])
{
NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];
// load the image, to prevent it from being cached we use 'initWithContentsOfFile'
NSString *imageNameToLoad = [NSString stringWithFormat:#"%d_full", selectedIndexPath.row];
NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:#"jpg"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.image = image;
}
}
How can i add audio file in prepareforSegue along with UIImageview for destinationViewController
audioArray =[[NSArray alloc] initWithObjects:#"Theme1", #"Theme2", #"Theme3", #"Theme4", nil];
NSString *filePath = [audioArray objectAtIndex:0];
NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: #"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: audioFilePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
Thanks

Related

Load audiofile array to collection view cells in sequence manner

I have loaded images and labels to custom collectionview cells in this method
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
Giving the cells title based on actual NSIndexPath value in the same method
cell.label.text = [NSString stringWithFormat:#"{%ld,%ld}", (long)indexPath.row, (long)indexPath.section];
Loading different images in each cell of the custom collection view cells in the same method
NSString *imageToLoad = [NSString stringWithFormat:#"%d.jpg", indexPath.row];
cell.image.image = [UIImage imageNamed:imageToLoad];
I want to load different audio files in each cell of the custom collection view cells in the same method
audioArray =[[NSArray alloc] initWithObjects:#"Theme1", #"Theme2", #"Theme3", #"Theme4", nil];
NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: #"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: audioFilePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
If i add indexPath.row below
NSString *filePath = [audioArray objectAtIndex:indexPath.row];
Then it always plays "Theme4" last audio file from within the array in all cells
And if i write 0
NSString *filePath = [audioArray objectAtIndex:0];
Then it plays always "Theme1" the very first audio file in all cells
How i can load different audio files in each cell of the custom collection view cells.
Thanks for help.
You should add these lines of code in
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
audioArray =[[NSArray alloc] initWithObjects:#"Theme1", #"Theme2", #"Theme3", #"Theme4", nil];
NSString *filePath = [audioArray objectAtIndex:indexPath.row];
NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: #"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: audioFilePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
}
rather than in
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
Hope this helps.

UIButtons event Touch up Inside not work properly?

My question is little abmigious ,thats why I posted all my code,so I request to every one please test all this code before giving me the Answer.Thanx
In my application i create all UIButtons programmatically and then save all these UIButtons in NSMutableArray. Here is my code:-
-(void)button:(id)sender
{
int btnn = 0;
int spacex = 152;
int spacey=20;
int k=0;
saveBtn = [[NSMutableArray alloc] init];
for (int i=0; i<48; i++)
{
if (btnn>6)
{
spacey=spacey+25;
spacex = 152;
btnn = 0;
}
else
{
btnn++ ;
k++;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(spacex, spacey, 25.0, 25.0);
int idx;
idx = arc4random()%[arr count];
NSString* titre1 = [arr objectAtIndex:idx];
[btn setTitle: titre1 forState: UIControlStateNormal];
[btn setTitleColor: [UIColor yellowColor] forState: UIControlStateNormal];
[btn.titleLabel setFont:[UIFont fontWithName:#"TimesNewRomanPS-BoldMT" size:22.0]];
spacex = spacex + 25;
btn.tag=k;
[btn addTarget:self action:#selector(aMethod:)forControlEvents:UIControlEventTouchUpInside];
[saveBtn addObject:btn];
[self.view addSubview:btn];
}
}
}
Now in (aMethod:) I add click sound on Each UIButton TouchUpInside event. Here is my code.
- (void)aMethod:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
Every thing work fine in start of my application , but continiously clicking the UIButtons approximatilly after 100 clicks i hear no sound of click and when i click on Next UIButtons touchup inside event its crash.Can any body help me in this issue.Thanx
app due to uncaught exception NSInternalInconsistencyException, reason: Could not load NIB in bundle: NSBundle </Users/moon/Library/Application Support/iPhone Simulator/4.1/Applications/3E14E5D6-4D1B-4DAC-A2B9-07667E99C399/soundtesting.app‌​> (loaded) with name secondclass
Ah, yes. What we have here is a failure to understand log messages. Your UIButton, which I can only assume pushes a new view onto the stack, is referencing a NIB that simply doesn't exist. It's a naming issue, not a button issue. Most likely, I would check for any calls to -initWithNibName: and see if your NIB really is named "secondclass.". Remember, iOS file names are CaSe SeNsITive.
//.h
...
#property (nonatomic, retain) AVAudioPlayer * player;
//.m
-(void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
player.delegate=self;
//in MRC, use [path release];
}
-(void)someMethod:(id)sender
{
[player play];
}
The reason might me memory leak for playing same audio these many times it may fill memory use this code for playing audio
- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundleOrNil
{
if (self = [super initWithNibName:nibName bundle:nibBundleOrNil]) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
theAudio = [AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:NULL];
}
return self;
}
- (IBAction)playSound
{
[theAudio play];
}
- (void)dealloc
{
[theAudio release];
[super dealloc];
}
You have a memory issue. When you do :
- (void)aMethod:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
1/ path is never released
2/ theAudio is never release
So you need to release theAudio in delegate method :
– audioPlayerDidFinishPlaying:successfully:
And you need to release path like this :
- (void)aMethod:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
[path release];
}
I think your problem should be solved.
I test your code . Changes in your aMethod following code. Though this I can get tag value.
- (void)aMethod:(id)sender
{
UIButton *btn=(UIButton *)sender;
NSLog(#"\n\n\nselectedbutton tag === %d \n\n\n",btn.tag );
NSString *path = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer play];
}

MWPhotoBrowser add data from plist?

trying to get some data from a plist into the MWPhotoBrowser sample app.
Any ideas how to do it?
UIViewController *converterController = nil;
NSMutableArray *photos = [[NSMutableArray alloc] init];
converterController = [[MWPhotoBrowser alloc] initWithPhotos:photos];
converterController.hidesBottomBarWhenPushed = YES;
//here individual images can be added. However need to add images from a plist.
[photos addObject:[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"image1" ofType:#"jpg"]]];
[photos addObject:[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"image2" ofType:#"jpg"]]];
[photos addObject:[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"image3" ofType:#"jpg"]]];
[photos addObject:[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"image4" ofType:#"jpg"]]];
if (converterController) {
[self.navigationController pushViewController:converterController animated:YES];
[converterController release];
}
[photos release];
Can add objects individually as shown above, but not able to do it with a plist.
Can anyone share an idea?
thanks
w
ell i got this but it doesnt work.
SString *path = [[NSBundle mainBundle] pathForResource:
#"ImageData" ofType:#"plist"];
// Build the array from the plist
photos = [[NSMutableArray alloc] initWithContentsOfFile:path];
crashes the app.
I must use the MWPhoto property.
Help anyone?
Maybe Michael Waterfall wants to look into this?
EDIT:
OK here is the working code (well it works ok so far)
NSUInteger nimages = 0;
for (; ; nimages++) {
NSString *nameOfImage_ = #"imageSufix";
NSString *imageName = [NSString stringWithFormat:#"%#%d.jpg", nameOfImage_, (nimages + 1)];
//NSLog(#"Name is %#", imageName); log the names
image = [UIImage imageNamed:imageName];
if (image == nil) {
break;
}
[photos addObject:[MWPhoto photoWithImage:image]];

Change NIB Size

Can some one show me how to change the nib frame width to match the UITableViewCell. Right now it's set for 320 when i edit the nib directly, but when i open it in Ipad, it's still 320 width.
- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)path {
PMID = nil;
NSString *str = [[NSString stringWithFormat:#"%#",[object URL]] stringByReplacingOccurrencesOfString:#"tt://actions/"
withString:#""];
NSArray *firstSplit = [str componentsSeparatedByString:#"/"];
NSString *number1 = [firstSplit objectAtIndex:0];
NSString *number2 = [firstSplit objectAtIndex:1];
TTTableViewCell* cell = (TTTableViewCell*) [self.tableView
cellForRowAtIndexPath:path];
NSArray *nib = nil;
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"Admin"] isEqualToString:#"Admin"]){
nib = [[NSBundle mainBundle] loadNibNamed:#"MenuView"
owner:self options:nil];
}else if ([[NSString stringWithFormat:#"%d",[number1 intValue]] isEqualToString:[NSString stringWithFormat:#"%#", [[NSUserDefaults standardUserDefaults] objectForKey:#"IDKey"]]]){
nib = [[NSBundle mainBundle] loadNibNamed:#"MenuView2"
owner:self options:nil];
}else{
nib = [[NSBundle mainBundle] loadNibNamed:#"MenuView3"
owner:self options:nil];
}
UIView *menuView = nil;
for (id object in nib) {
if ([object isKindOfClass:[UIView class]]) {
menuView = (UIView *) object;
}
}
[self showMenu:menuView forCell:cell animated:YES];
}

Trouble loading html file from plist to webView on iPhone

trouble loading html file from plist to webView using following code in
FAQDetailViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *WebPath = [Path stringByAppendingPathComponent:WebFile];
UIWebView *tempWeb = [[UIWebView alloc] initWithContentsOfFile:WebPath];
[webView loadData:tempWeb]; //I think my issue is here. I am not understanding how to implement the the code here
[tempWeb release];
}
loaded here with this peace of code in FAQViewController.m:
FAQDetailViewController *faqdvController = [[FAQDetailViewController alloc] initWithNibName:#"FAQDetailView" bundle:[NSBundle mainBundle]];
faqdvController.WebFile = [dictionary objectForKey:#"faqDesc"]; //html file from plist file placed here
faqdvController.hidesBottomBarWhenPushed = NO;
[self.navigationController pushViewController:faqdvController animated:YES];
[faqdvController release];
You can try it this way:
NSString *bundle = [[NSBundle mainBundle] bundlePath];
NSString *webPath = [bundle stringByAppendingPathComponent:{htmlFilename}]
UIWebView* browser = [[UIWebView alloc] initWithFrame:
CGRectMake(0, 0, 200, 300)];
[browser loadRequest:[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:webPath]]];
[self addSubview:browser];
You'll also want to set delegates on the browser so you get informed when the contents have finished loading.