2004/11/26 | [DirectorFAQ]如何实现声音与文字同步
类别(DirectorFAQ) | 评论(0) | 阅读(511) | 发表于 17:08
问题分析
要实现声音与文字的同步,那我们就得即时检测当前播放声音的位置,同时显示相对应的文字。
重点思路
先用声音编辑软件(如cooledit、sound forge)在声音文件中加入线索点(cuepoint),然后使用cuepointtimes或者cuePointNames属性来检测声音播放的位置。
下面我们来看一个实例,英文朗读,同时同步出现中文翻译!
制作步骤
  • (1)首先将英文文本导入到director中,命名为“aaa”,同时对其进行格式化,给每句英文的前后加上“*”,如图22.1所示。

    图片如下:

    图22.1 格式化的英文文本

  • (2)将中文翻译文本导入到director中,命名为“bbb”,同时进行格式化,如图22.2所示,每一句翻译占一行。

    图片如下:

    图22.2 中文翻译文本

  • (3)用声音编辑软件Cool Edit 2000打开英文朗读文件ccc.wav,在对应步骤1中加标记的停顿处,按F8或者点击
    图片如下:
    按钮加上线索点(Cue Point),如图22.3所示。

    图片如下:

    图22.3 添加线索点

  • (4)然后将加好线索点的声音文件导入到Director中,并新建两个文本演员,分别命名为“mytext”和“text_fanyi”,并导入一张背景图片,如图22.4所示。

    图片如下:

    图22.4 演员表

  • (5)将文本演员“mytext”、“text_fanyi”以及背景图片拖放到舞台,如图22.5所示。并给“mytext”附上“txt&sound”行为,行为脚本内容如下:

    图片如下:

    图22.5 剪辑室

    txt&sound行为脚本内容:
    global text_num
    property sentencelist,sentencenum
    property soundmember,readnum,colorchange
    on beginsprite me
      sentencelist = []
      sentencenum = 0
      colorchange = 0
      member("mytext").text = ""
      soundmember = member("ccc")
      readnum = 0
      member("mytext").font = "System"
      member("mytext").fixedlinespace = 22
      member("mytext").firstindent = 0
      member("mytext").fontstyle = [#plain]
      member("mytext").color = rgb(0,0,0)
      member("text_fanyi").text = ""
      m = member("aaa")
      s = m.text
      n = length(s)
      repeat with i = 1 to n
        if s.char[i] = "*" then
          delete s.char[i]
          add sentencelist,i
        end if
      end repeat
      member("mytext").text = s
      n = sentencelist.count
      repeat with i = 3 to n
        if i mod 2 = 1 then
          ch1 = sentencelist[i-1]
          ch2 = sentencelist[i] - 1
          member("mytext").char[ch1..ch2].color = rgb(0,0,255)
        end if
      end repeat
      if sentencelist[1] > 1 then
        ch1 = 1
        ch2 = sentencelist[1] - 1
        member("mytext").char[ch1..ch2].color = rgb(0,0,255)
      end if
      
      m = member("ccc")
      puppetsound 1,m
    end
    
    on exitFrame me
      if soundbusy(1) then
        colorchange = 1
        t1 = sound(1).currenttime
        if readnum = 0 then
          t2 = 0
        else
          t2 = soundmember.cuepointtimes[readnum]
        end if
        if t1 > t2 then
          if soundmember.cuepointtimes.count > readnum then
            readnum = readnum + 1
            ch1 = sentencelist[readnum*2-1]
            ch2 = sentencelist[readnum*2] - 1
            if readnum > 1 then
              ch3 = sentencelist[readnum*2-3]
              ch4 = sentencelist[readnum*2-2]
              member("mytext").char[ch3..ch4].color = rgb(0,0,0)
            end if
            member("mytext").char[ch1..ch2].color = rgb(255,0,0)
            member("text_fanyi").text = member("bbb").line[readnum]        
          end if
        end if
      else
        if soundmember.cuepointtimes.count > 0 then
          if colorchange = 1 then
            ch1 = sentencelist[readnum*2-1]
            ch2 = sentencelist[readnum*2] - 1
            member("mytext").char[ch1..ch2].color = rgb(0,0,0)
            colorchange = 0
            readnum = 0
          end if
        end if
      end if
    end
    

    完成效果
    运行程序,完成效果如图22.6所示:

    图片如下:

    图22.6 完成效果

  • 0

    评论Comments

    日志分类
    首页[38]
    DirectorFAQ[9]
    学习笔记[14]
    c语言[3]
    生活碎片[11]
    关于xiaoma[1]