我正在使用C#应用程序,它将读取.srt文件并在屏幕上显示。
文件格式就像这样;
1 00:00:06,000 00:00:07,400 Enjoy the movie!
2 00:00:07,500 00:00:09,500 Hi, my name is Mary
3 00:00:22,000 00:00:25,000 Hello my name is John.
我正在使用一个定时器,每100毫秒和每一个滴答滴答我 想要检查值是否在开始和结束之间。在这种情况下,首先介于两者之间 00:00:06,000和00:00:07,400但我不确定我应该使用什么价值。我试图将它解析为DateTime但它没有转换。
有什么想法吗?
答案 0 :(得分:1)
您必须使用TimeSpan类型解析here
答案 1 :(得分:1)
您可以使用DateTime或TimeSpan,但需要提供correct format到ParseExact
功能。
答案 2 :(得分:1)
使用TimeSpan。 这里是简单的Console Project,可以在正确的时间显示字幕
class Program
{
private static Queue<Subtitle> _subtitles;
private static Subtitle _activeSubtitle;
private static TimeSpan _currentTime = new TimeSpan();
static void Main(string[] args)
{
_subtitles = new Queue<Subtitle>();
Subtitle title1 = new Subtitle()
{
StartTime = TimeSpan.Parse("00:00:06,000"),
EndTime = TimeSpan.Parse("00:00:07,400"),
Text = "Enjoy the movie!"
};
Subtitle title2 = new Subtitle()
{
StartTime = TimeSpan.Parse("00:00:07,500"),
EndTime = TimeSpan.Parse("00:00:09,500"),
Text = "Hi, my name is Mary"
};
Subtitle title3 = new Subtitle()
{
StartTime = TimeSpan.Parse("00:00:22,000"),
EndTime = TimeSpan.Parse("00:00:25,000"),
Text = "Hello my name is John."
};
_subtitles.Enqueue(title1);
_subtitles.Enqueue(title2);
_subtitles.Enqueue(title3);
Timer timer = new Timer(ShowSubtitles, null, 0, 100);
while (_currentTime <= new TimeSpan(0, 0, 0, 30))
{
}
Console.WriteLine("End");
}
private static void ShowSubtitles(object state)
{
if (_activeSubtitle == null && _subtitles.Count > 0)
_activeSubtitle = _subtitles.Dequeue();
Console.WriteLine(_currentTime);
if (_activeSubtitle != null)
{
if (_currentTime >= _activeSubtitle.StartTime && _currentTime <= _activeSubtitle.EndTime)
Console.WriteLine("\t{0}", _activeSubtitle.Text);
if (_currentTime >= _activeSubtitle.EndTime)
_activeSubtitle = null;
}
_currentTime = _currentTime.Add(new TimeSpan(0, 0, 0, 0, 100));
}
}
internal class Subtitle
{
public TimeSpan StartTime { get; set; }
public TimeSpan EndTime { get; set; }
public string Text { get; set; }
}