根据SRT文件填充对象

时间:2012-06-13 13:49:20

标签: c#

我的应用程序在我的控制台中显示子标题(一切正常),但我想改进代码,

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;

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:01.000"),
            EndTime = TimeSpan.Parse("00:00:02.400"),
            Text = "Enjoy the movie!"
        };

        Subtitle title2 = new Subtitle()
        {
            StartTime = TimeSpan.Parse("00:00:02.900"),
            EndTime = TimeSpan.Parse("00:00:03.500"),
            Text = "Hi, my name is Mary"
        };

        Subtitle title3 = new Subtitle()
        {
            StartTime = TimeSpan.Parse("00:00:09.000"),
            EndTime = TimeSpan.Parse("00:01:02.100"),
            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)
    {
        Console.Clear();

        if (_activeSubtitle == null && _subtitles.Count > 0)
            _activeSubtitle = _subtitles.Dequeue();
       // Console.WriteLine(_currentTime);

        if (_activeSubtitle != null)
        {


            if (_currentTime >= _activeSubtitle.StartTime && _currentTime <= _activeSubtitle.EndTime)
                Console.WriteLine( _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; }
}


每个信号字幕:

    Subtitle title3 = new Subtitle()
    {
        StartTime = TimeSpan.Parse("00:00:09.000"),
        EndTime = TimeSpan.Parse("00:01:02.100"),
        Text = "Hello my name is John."
    };


我想将其更改为:

  1. 阅读.SRT文件
  2. 并逐行插入字幕,如:

    Subtitle title (i) = new Subtitle()<br>
    {
        StartTime = TimeSpan.Parse(**StartTimeSEtring**),<br>
        EndTime = TimeSpan.Parse(**EndTimeString**),<br>
        Text = TextString;<br>
    };
    

0 个答案:

没有答案