Search code examples
iphoneiosfile-iodirectory-structureresourcebundle

How to package a set of videos in multiple directories and play them in an iPhone app?


I am making a video delivery application and sending a bunch of files in the following directory structure:

Home/Week 1/Day 1/123.mp4,abc.mp4,42343.mp4 Home/Week 1/Day 2/123.mp4,xyz.mp4

etc. I need to maintain the directory structure and play the appropriate file. NOTE: there are multiple files with the same name in different folders.

Current code: I dragged the Home folder into xcode into the other sources folder > the action copied the files into the project directory and added the references. This is the code I wrote for playing the video "abc" from day1 in week1 in home.

NSString* p = @"home/Week 1/Day 1/abc";

NSString* moviePath2 = [[NSBundle mainBundle] pathForResource:@"Home/Week 1/Day 1/abc" ofType:@"mp4"];
NSURL* movieURL2=[NSURL fileURLWithPath:moviePath2];

In this case, I get moviePath2 as Empty.

If I run this code:

NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [docsDirectory stringByAppendingPathComponent:p];
    if(![fileManager fileExistsAtPath:filePath])
    {
        NSLog(@"exists");
    }

It prints the exists on console.

Can you please provide a solution for this? Is my approach to package the directory structure with videos correct?? Thanks for the help.


Solution

  • Access your movie by simply movie name

    NSURL* movieURL2=[NSURL fileURLWithPath: p];  
    

    Or you want to access it from main bundle then you have to copy your movie to that path

    [fileManager copyItemAtPath:moviePath2 toPath:filePath error:nil];