首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在xcode中使用.srt文件显示字幕

如何在xcode中使用.srt文件显示字幕
EN

Stack Overflow用户
提问于 2013-04-05 20:15:50
回答 1查看 809关注 0票数 1

我需要在xcode的视频下显示字幕..我知道我们可以用.srt文件来显示字幕..我可以解析.srt文件..但我的问题是,我不知道如何使.srt文件中的文本在视频下显示,如何设置时间间隔。有谁能帮帮我

EN

回答 1

Stack Overflow用户

发布于 2014-01-16 12:56:51

我是从堆栈溢出链接获得的.我忘了那个链接。链接中的代码是这样的-

代码语言:javascript
复制
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); } }

我确实用这个解析了srt文件...我在电影播放器(MPMovieControlStyleNone)视图中添加了UITextView ....我确实通过使用计时器的startString和endString自动更改了文本...我只能通过自定义的播放和暂停按钮来播放和暂停播放器...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15833735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档