How to use .srt file to show subtitles in xcode - xcode4.3

I need to show subtitle under video in xcode.. I knew we can use .srt file to show subtitles..I can parse .srt file.. But my problem is I don't know how to make the text in .srt file to be show under video, How to set the time intervals .. Anybody please help me

I got it from stack overflow link.... I forgot that link... code in that link is this-
NSString *path = [[NSBundle mainBundle] pathForResource:#"srtfilename" ofType:#"srt"];
NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
NSScanner *scanner = [NSScanner scannerWithString:string];
while (![scanner isAtEnd])
{
#autoreleasepool
{
NSString *indexString;
(void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&indexString];
NSString *startString;
(void) [scanner scanUpToString:#" --> " intoString:&startString];
// My string constant doesn't begin with spaces because scanners
// skip spaces and newlines by default.
(void) [scanner scanString:#"-->" intoString:NULL];
NSString *endString;
(void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&endString];
NSString *textString;
// (void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&textString];
// BEGIN EDIT
(void) [scanner scanUpToString:#"\r\n\r\n" intoString:&textString];
textString = [textString stringByReplacingOccurrencesOfString:#"\r\n" withString:#" "];
// Addresses trailing space added if CRLF is on a line by itself at the end of the SRT file
textString = [textString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// END EDIT
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
indexString , #"index",
startString, #"start",
endString , #"end",
textString , #"text",
nil];
NSLog(#"%#", dictionary); } }
I did parse srt file by this... I added UITextView on movie player(MPMovieControlStyleNone) view.... I did change text automatically by using startString and endString using timer... I can only play and pause the player by custom play and pause button...

Related

Parsing an NSString to find and increment numeric values

I have an string value.
NSString *getAllData = #"0,testing,u,4,u";
Now I want to increment in all the numeric values in the string e.g. #"1,testing,u,5,u"
How can this be done?
Here is your answer...
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *getAllData = #"0,testing,u,4,u";
NSMutableArray *resultArray = [NSMutableArray array];
[[getAllData componentsSeparatedByString:#","] enumerateObjectsUsingBlock:^(NSString *subString, NSUInteger idx, BOOL *stop) {
if([self isNumeric:subString])
{
[resultArray addObject:[NSString stringWithFormat:#"%d", [subString intValue] + 1]];
}
else [resultArray addObject:subString];
}];
NSString *finalOutput = [resultArray componentsJoinedByString:#","];
NSLog(#"%#", finalOutput);
}
- (BOOL)isNumeric:(NSString *)aString
{
NSString *expression = [NSString stringWithFormat:#"^[0-9]*$"];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:aString options:0 range:NSMakeRange(0, [aString length])];
return (numberOfMatches != 0);
}
You can use the NSScanner class to find the numbers in the string, and convert them to integers. The NSMutableString class can be used to accumulate the output string.
- (NSString *)incrementValuesInString:(NSString *)input
{
NSScanner *scanner = [NSScanner scannerWithString:input];
[scanner setCharactersToBeSkipped:nil];
NSMutableString *result = [NSMutableString string];
while ( ![scanner isAtEnd] )
{
// copy characters to the result string until a digit is found
NSString *temp;
if ( [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&temp] )
[result appendString:temp];
// scan the number and increment it
if ( ![scanner isAtEnd] )
{
int value;
if ( [scanner scanInt:&value] )
[result appendFormat:#"%d", value + 1];
}
}
return( [result copy] );
}

how to fetch the string starts with &abc and ends with &

i like to know how to fetch the specific string which starts with &abc and ends with &. I tried with had prefix and sufix . but this is not new line ,
&xyz;123:183:184:142&
&abc;134:534:435:432&
&qwe;323:535:234:532&
my code :
NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:s];
[scanner scanUpToString:#"&abc" intoString:nil]; //
NSString *substring = nil;
[scanner scanString:#"&abc" intoString:nil]; // Scan the # character
if([scanner scanUpToString:#"&" intoString:&substring]) {
// If the space immediately followed the &, this will be skipped
[substrings addObject:substring];
NSLog(#"substring is :%#",substring);
}
// do something with substrings
[substrings release];
how to make "scanner scanUpToString:#"&abc" and count ":"==3 till "#"???? can help me
NSArray *arr = [NSArray arrayWithObjects:#"&xyz;123:183:184:142&",
#"&abc;134:534:435:432&",
#"&qwe;323:535:234:532&",
#"& I am not in it",
#"&abc I am out &" ,nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"self BEGINSWITH[cd] %# AND self ENDSWITH[cd] %#",#"&abc",#"&"];
NSLog(#"Sorted Array %#",[arr filteredArrayUsingPredicate:predicate]);
NSArray *sortedArray = [arr filteredArrayUsingPredicate:predicate];
NSMutableArray *finalResult = [NSMutableArray arrayWithCapacity:0];
for(NSString *string in sortedArray)
{
NSString *content = string;
NSRange range1 = [content rangeOfString:#"&abc"];
if(range1.length > 0)
content = [content stringByReplacingCharactersInRange:range1 withString:#""];
NSRange range2 = [content rangeOfString:#"&"];
if(range2.length > 0)
content = [content stringByReplacingCharactersInRange:range2 withString:#""];
[finalResult addObject:content];
}
NSLog(#"%#",finalResult);
Try using NSRegularExpression:
- (BOOL)isValidString:(NSString *)string
{
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:#"^&abc.*&$" options:0 error:NULL];
NSTextCheckingResult *result = [regularExpression firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
return (result != nil);
}

NSScanner unrecognized selector on scanUpToString

I use the URL parser class from Dimitris but i encounter a problem when init object trought initWithURLString:
- (id) initWithURLString:(NSString *)url{
self = [super init];
if (self != nil) {
NSString *string = url;
NSScanner *scanner = [NSScanner scannerWithString:string];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:#"&?"]];
NSString *tempString;
NSMutableArray *vars = [NSMutableArray new];
//ignore the beginning of the string and skip to the vars
[scanner scanUpToString:#"?" intoString:nil];
while ([scanner scanUpToString:#"&" intoString:&tempString]) {
[vars addObject:[tempString copy]];
}
self.variables = vars;
}
return self;
}
On line [scanner scanUpToString:#"?" intoString:nil]; i get an error:
[NSURL length]: unrecognized selector sent to instance 0x1f8c2050
How is it possible?
EDIT: maybe you want to know how i call URLParser:
URLParser *urlParser = [[URLParser alloc]initWithURLString:[info valueForKey:UIImagePickerControllerReferenceURL]];
UIImagePickerControllerReferenceURL value is : assets-library://asset/asset.PNG?id=8D2F0449-11A3-4962-9D60-C446831645D7&ext=PNG
You pass a NSURL to initWithURLString, but you should use it with NSString like this:
NSString* urlString = [NSString stringWithFormat:#"%#",[info valueForKey:UIImagePickerControllerReferenceURL]];
URLParser *parser = [[[URLParser alloc] initWithURLString:urlString] autorelease];

Search tags in string

I have a string, and I want to search words (tags) that begin with "#" and end with "." or "," or " " I found this online, but is limited because:
- You can find a single word in the string (although there are more words)
- "RangeOfString" does not allow multiple choices
NSString *stringText = #"test #hello #world";
NSString *result = nil;
// Determine "#"
NSRange hashRange = [stringText rangeOfString:#"#" options:NSCaseInsensitiveSearch];
if (hashRange.location != NSNotFound)
{
// Determine " " location according to "#" location
NSRange endHashRange;
endHashRange.location = hashRange.length + hashRange.location;
endHashRange.length = [stringText length] - endHashRange.location;
endHashRange = [stringText rangeOfString:#" " options:NSCaseInsensitiveSearch range:endHashRange];
if (endHashRange.location != NSNotFound)
{
// Tags found: retrieve string between them
hashRange.location += hashRange.length;
hashRange.length = endHashRange.location - hashRange.location;
result = [stringText substringWithRange:hashRange];
}
}
you have idea how can I do?
Thank you!
You can use NSRegularExpression class, like this:
NSError *error = NULL;
NSRegularExpression *tags = [NSRegularExpression
regularExpressionWithPattern:#"[#]([^, .]+)([, .]|$)"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSArray *matches = [tags matchesInString:str options:0 range:NSMakeRange(0, str.length)];
for (NSTextCheckingResult *match in matches) {
NSLog(#"%#", [str substringWithRange:[match rangeAtIndex:1]]);
}
You may need to play with your regular expression to get it just right. The reference that I liked describes the grammar of the regex language supported by Apple's classes.
You should use NSRegularExpression, which will give you multiple matches.
The following is an untested example:
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"\\b(#\\S*[.,])\\b" options:NSRegularExpressionCaseInsensitive];
int numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, string.length)];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
You will most likely want to use NSScanner.
NSString *stringText = #"test #hello #world";
NSString *result = nil;
NSCharacterSet *endingChars = [NSCharacterSet characterSetWithCharactersInString:#"., "];
NSScanner *scanner = [NSScanner scannerWithString:stringText];
scanner.charactersToBeSkipped = nil;
[scanner scanUpToString:#"#" intoString:NULL];
[scanner scanString:#"#" intoString:NULL];
[scanner scanUpToCharactersFromSet:endingChars intoString:&result];
[scanner scanCharactersFromSet:endingChars intoString:NULL];
STAssertEqualObjects(result, #"hello", nil);
At that point you just loop until [scanner isAtEnd];
NSString *stringText = #"test #hello #world";
NSString *match = nil;
NSMutableArray *results = [NSMutableArray arrayWithCapacity:2];
NSCharacterSet *endingChars = [NSCharacterSet characterSetWithCharactersInString:#"., "];
NSScanner *scanner = [NSScanner scannerWithString:stringText];
scanner.charactersToBeSkipped = nil;
while (![scanner isAtEnd]) {
[scanner scanUpToString:#"#" intoString:NULL];
[scanner scanString:#"#" intoString:NULL];
[scanner scanUpToCharactersFromSet:endingChars intoString:&match];
[scanner scanCharactersFromSet:endingChars intoString:NULL];
[results addObject:match];
}
STAssertEquals(results.count, 2, nil);
STAssertEqualObjects([results objectAtIndex:0], #"hello", nil);
STAssertEqualObjects([results objectAtIndex:1], #"world", nil);

Extract Contents of Anchor Tag

What I'm trying to do is extract the contents of a an anchor tag being stored in an NSString.
If for example I have a string with the following:
Amazon <b>Kindle</b>: Welcome
How would I go about extracting the contents of the anchor tag so that I would have the following:
https://kindle.amazon.com/&sa=U&ei=GdiWT5uCEI6BhQfihoTzDQ&ved=0CCUQFjAB&usg=AFQjCNEoRolsgoynLNS0H60VWz-9EaQdtw
Any help would be greatly appreciated!
I'm completely stumped, whereas this should be quite simple? The answer posted below keeps returning null.
If you can require Lion, then you can use NSRegularExpression.
NSString* stringToSearch = #"Amazon <b>Kindle</b>: Welcome";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"href\\s*=\\s*\"\\/url\\?q=([^\"]*)\""
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult* match = [regex firstMatchInString:stringToSearch options:0 range:NSMakeRange(0, [stringToSearch length])];
if(match.numberOfRanges == 2)
{
NSRange capture = [match rangeAtIndex:1];
NSString* URLString = [stringToSearch substringWithRange:capture];
NSLog(#"%#",URLString);
}
One Possible solution is by using NSScanner -
NSString *urlString = nil;
NSString *htmlString = #"Amazon <b>Kindle</b>: Welcome";
NSScanner *scanner = [NSScanner scannerWithString:htmlString];
[scanner scanUpToString:#"<a" intoString:nil];
if (![scanner isAtEnd]) {
[scanner scanUpToString:#"http" intoString:nil];
NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:#">"];
[scanner scanUpToCharactersFromSet:charset intoString:&urlString];
}
NSLog(#"%#", urlString);
In Logs -
https://kindle.amazon.com/&sa=U&ei=GdiWT5uCEI6BhQfihoTzDQ&ved=0CCUQFjAB&usg=AFQjCNEoRolsgoynLNS0H60VWz-9EaQdtw