Subtitles Algorithm

Introduction

Hello, here is the sample program on 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" values, which means a starting frame and an ending frame. The class " Key" also contains overriding of base method " bool Equals(object obj)" The equals method will tell the HashTable that the key can be in some range (for example, 23-43-How are you?) if the current position of film is frame 35, HashTable finds the text " How are you". It's quite a good and simple solution if you're rewinding the film.

Example

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 the current video frame (example: 333)
        }

        static Hashtable FileToString()
        {
            /*the file sub.txt is in 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;
        }
    }
}


Similar Articles