c#制作视频音频歌词相关技术代码记录

c#制作视频音频歌词相关技术代码记录

July 1, 2010

开发类似千千静听或者KMplayer等音频视频播放器播放字幕或者歌词的技术

从两方面着手,一是时间轴,二是帧(对于视频)。

方法一的实现比较简单,只需要内置一个定时器,在媒体播放时开始计时,然后读指定的脚本即可。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;


namespace MediaPlayer
{
	public partial class Form6 : Form
	{
		private AxWMPLib.AxWindowsMediaPlayer axWindowsMediaPlayer2 = new AxWMPLib.AxWindowsMediaPlayer();
		PictureBox pictureBox2 = new PictureBox();
		private int t = 0;
		private int count = 0;
		private string filename;
		private string[] info = new string[100];



		public Form6(ref AxWMPLib.AxWindowsMediaPlayer axWindowsMediaPlayer1, ref PictureBox pictureBox1)
		{
			InitializeComponent();
			this.Text = "视频分析";
			timer1.Enabled = false;
			axWindowsMediaPlayer2 = axWindowsMediaPlayer1;
			pictureBox2 = pictureBox1;
		}


		private void timer1_Tick(object sender, EventArgs e)
		{
			t++;
			label2.Text = t.ToString();


			if (!info[count].Equals("end#"))
			{
				if (t == Int32.Parse(info[count].Substring(0, info[count].IndexOf(']'))))
				{
					richTextBox1.Text = info[count].Substring(info[count].IndexOf(']') + 1);

					count++;
				}
			}


		}

		private void button1_Click(object sender, EventArgs e)
		{
			openFileDialog1.ShowDialog();
			filename = openFileDialog1.FileName;
			try
			{
				// Create an instance of StreamReader to read from a file.  
				// The using statement also closes the StreamReader.  
				using (StreamReader sr = new StreamReader(filename))
				{
					String line;
					int i = 0;
					// Read and display lines from the file until the end of   
					// the file is reached.  
					while ((line = sr.ReadLine()) != null)
					{
						info[i] = line;
						i++;
					}
					sr.Close();
				}
			}
			catch (Exception a)
			{
				// Let the user know what went wrong.  
				MessageBox.Show("读取错误");


				MessageBox.Show(a.Message);
			}
		}

		private void openFileDialog2_FileOk(object sender, CancelEventArgs e)
		{
			axWindowsMediaPlayer2.URL = openFileDialog2.FileName;
			pictureBox2.Visible = false;
			timer1.Enabled = true;
		}


		private void button2_Click(object sender, EventArgs e)
		{
			openFileDialog2.ShowDialog();
		}

	}
}

方法二需要利用DirectX开发,或者flash播放的技术拆解成帧然后播放

最后更新于