Откройте видеофайл из папки Assets и воспроизведите его - PullRequest
0 голосов
/ 09 апреля 2019

Я пытаюсь загрузить файл .mp4, который я добавил в папку assets / videos, но у меня ничего не получается.Пока это мой код:

- (IBAction)showVideo:(id)sender
{
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths1 objectAtIndex:0];
    NSString *strPath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,@"myVideoName.mp4"];
    NSLog(@"strPath %@",strPath);

    NSURL *videosURL = [NSURL fileURLWithPath:strPath];

    AVPlayer *player = [AVPlayer playerWithURL:videosURL];

    // create a player view controller
    AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
    [self presentViewController:controller animated:YES completion:nil];
    controller.player = player;
    [player play];
}

Я думаю, что я что-то не так с пути к видеофайлу.Вы можете мне помочь?Спасибо!

1 Ответ

0 голосов
/ 09 апреля 2019

Я нашел решение.Я заменил это:

 NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths1 objectAtIndex:0];
    NSString *strPath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,videoName];
    NSLog(@"strPath %@",strPath);

на это:

 NSBundle *mainBundle = [NSBundle mainBundle];
 NSString *myFile = [mainBundle pathForResource: @"videoName" ofType: @"mp4"];
...