View Full Version : pathForResource Issue / Convert

Jul 17, 2010, 10:40 PM
I have been working on a problem from around 12 hours now and I am stumped. The first bit of code is the MoviePlayerViewController and the second bit is for Audio. The Second bit works when loading the path from an indexPath of a Table yet I cant get the first bit of code to do the same thing by loading from an Array.

I need to convert this bit of code to...

// Play movie from the bundle
NSString *path = [[NSBundle mainBundle] pathForResource:@"Movie-1" ofType:@"mp4" inDirectory:nil];

// Create custom movie player
moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithPath:path] autorelease];

// Show the movie player as modal
[self presentModalViewController:moviePlayer animated:YES];

// Prep and play the movie
[moviePlayer readyPlayer];

This bite of code...

NSUInteger row = [indexPath row];
NSString *rowTitle = [filmSound objectAtIndex:row];

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], rowTitle]];
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

Jul 18, 2010, 12:18 PM
You don't really need to separate the resource from the type; you can pass both in the resource parameter. So this line should work:

NSString *path = [[NSBundle mainBundle] pathForResource:rowTitle ofType:nil];

You can simplify your URL line in the same way.

NSURL *url = [NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:rowTitle ofType:nil]

Thats make sense. Using the above the result works out to be...

NSInteger row = indexPath.row;
NSString *rowTitle = [filmSound objectAtIndex:row];

NSURL *url = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:rowTitle ofType:nil]];

// Create custom movie player
moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithPath:url] autorelease];

Errors says struct NSURL expected NSString.

What about this...

How would one populate (the bold) with an array in any of these examples.

NSString *path = [[NSBundle mainBundle] pathForResource:@"INFO FROM ARRAY" ofType:@"mp4"];


NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"INFO FROM ARRAY"];

I can use the above within an If statement to populate a Cell depending on the indexPath yet the cells are editable and movable so I have a problem when it come to retaining the information for the move. If I have an array populating any of the above I can use an idObject to retain the info per cell.

By changing the NSString *path to NSURL*path seem so never work as the MoviePlayerViewController I have written to handle all SDKs requires NSString.


Jul 18, 2010, 06:38 PM
Thank you for your help.

The solution was the following...

NSInteger row = indexPath.row;
NSString *rowTitle = [filmMovie objectAtIndex:row];

NSString *path = [[NSBundle mainBundle] pathForResource:rowTitle ofType:nil];