AVPlayer playing the last link only in NSMutableArray

27 Views Asked by At

I'm trying to play a list of .mp4 links imported from .m3u file, File is save locally in the app and it contains 10 movie titles and links.

Full code

App Transport Security Settings Added in info.plist

vController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

@interface ViewController : UIViewController {
    NSMutableArray *movieTitle;
    NSMutableArray *movieURL;
    NSMutableArray *tempArray;
    NSArray *lines;
}

@property (nonatomic, retain) NSMutableArray *movieTitle;
@property (nonatomic, retain) NSMutableArray *movieURL;
@property (nonatomic, retain) NSMutableArray *tempArray;
@property (nonatomic, retain) NSArray *lines;

vController.m

@synthesize movieURL,movieTitle,tempArray,lines;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    movieURL = [[NSMutableArray alloc] init];
    movieTitle = [[NSMutableArray alloc] init];
    tempArray = [[NSMutableArray alloc] init];
    
    [self addM3UFile];
}

- (void)addM3UFile {
    NSString *addPath = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"m3u8"];
    
    NSString *fileContents1 = [NSString stringWithContentsOfFile:addPath encoding:NSUTF8StringEncoding error:NULL];
    lines = [fileContents1 componentsSeparatedByString:@"\n"];
    [tempArray addObjectsFromArray:lines];
        
    [self sortM3UFile];
    
}

- (void)sortM3UFile {
    int i;
    
    for (i=0; i<[tempArray count]; i++) {
        
        NSString *tempTitle = [tempArray objectAtIndex:i];
        
        if ([tempTitle containsString:@"://"]) {
            [movieURL addObject:tempTitle];
        }
        
        if ([tempTitle containsString:@"#EXTINF:-1 ,"]) {
            [movieTitle addObject:tempTitle];
        }
    }
}

Code to play the video

NSString *url = [movieURL objectAtIndex:9];
NSLog(@"URL: %@", url);
AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:url]];
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self presentViewController:controller animated:YES completion:nil];
controller.player = player;
[player play];

Works fine ONLY with the last object in the list

Links example : http://www.wwwwwwww.com/1234.mp4

I tested all the links and they all work fine when I enter them in the browser, I also printed each link in the log and they are all there.

I tried to play the links from NSArray instead of NSMutableArray and it didn't work.

Saved the links in NSUserDefaults also didn't work.

When I changed the list order from the .m3u8 file it only played the last link, so now I'm sure that the problem is not within the links.

one thing worked for me is when I hard encoded the links in the code manually.

any idea what's going on here ? :)

1

There are 1 best solutions below

0
aLFaRSi On

After digging more into find out the problem, I finally found the issue.

For some reason when adding urls into the array it added a space after each link except for the last one, I found the problem by counting the string length then logging each character individually.

so I added this lines of code in the sortM3UFile and it fixed the problem

NSArray *words = [tempTitle componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *nospaces = [words componentsJoinedByString:@""];
[movieURL addObject:nospaces];

hope that would help someone