Subtitles Algorithm



Hello, here is the sample program, how easily you can implement subtitles into your simple movie player. This sample code works with the HashTable which contains "key" and "value".
As a Key I used my own class that contains "min" and "max" value, it means starting frame in and ending frame. The class "Key" also contains overriding of base method "bool Equals(object obj)". Equals method will tell the HashTable, that key can be in some range (example: 23-43-How are you?) so, if current position of film is frame 35, HashTable finds the text "How are you". It's quite good and simple solution if you're rewinding the film.

using System;

using System.Collections.Generic;

using System.Text;

using System.Collections;

using System.IO;

namespace Subtitles

{

          class Program

          {

                   static void Main(string[] args)

                   {

                             Hashtable table = FileToString();

                             Console.WriteLine("OUTPUT:" + table[new Key(333)]); // there you enter current video frame(example: 333)

                   }

                   static Hashtable FileToString()

                   {

                             /*the file sub.txt is int this solution in this form: start_frame-end_frame-text 345-543-

                             How are you? */

                             Hashtable table = new Hashtable();

                             Stream stream = new FileStream("sub.txt",FileMode.Open);

                             StreamReader read = new StreamReader(stream);

                             while (true)

                             {

                                      string line = read.ReadLine();

                                      if (line == null)

                                                break;

                                     string[] field = line.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                                      Key key = new Key(Convert.ToInt32(field[0]),Convert.ToInt32(field[1]));

                                      table.Add(key,field[2]);

                             }

                             return table;

                   }

          }

          class Key

          {

                   int min;

                   int max;

                   public Key(int min, int max)

                   {

                             this.max = max;

                             this.min = min;

                   }

                   public Key(int pos)

                   {

                             this.max = pos;

                             this.min = pos;

                   }

                   public override bool Equals(object obj)

                   {

                             Key pom = (Key)obj;

                             if (pom.max <= max && pom.min >=min)

                             {

                                      return true;

                             }

                             else

                                      return false;

                   }

                   public override int GetHashCode()

                   {

                             return 1;

                   }

          }

}

Up Next
    Ebook Download
    View all
    Learn
    View all