关键词搜索

源码搜索 ×
×

文字转换为语音VB.NEt实现

发布2021-02-07浏览744次

详情内容

Imports System.Speech.Synthesis
Imports SpeechLib
 
 
Public Class Form1
    Public Sub New()
        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
        ' 在 InitializeComponent() 调用之后添加任何初始化。
    End Sub
    '分别用两种方法实现
    Dim ss As SpeechSynthesizer '定义一个语音引擎访问全局变量
    Dim voice As SpVoice = New SpVoice '调用第三方工具库SpeechLib
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ss = New SpeechSynthesizer() '实例化
        Dim voicestring As String = "" '定义一个字符串
        For Each iv As InstalledVoice In ss.GetInstalledVoices() '遍历已安装的语音引擎
            ComboxVoice.Items.Add(iv.VoiceInfo.Name) '将其名字添加到组合框
        Next
        ComboxVoice.SelectedIndex = 0 '默认显示第一个
    End Sub
 
    Private Sub ButtonRead_Click(sender As Object, e As EventArgs) Handles ButtonRead.Click
        voice.Voice = voice.GetVoices(String.Empty, String.Empty).Item(0) '设置中文语音
        'voice.Voice = voice.GetVoices(String.Empty, String.Empty).Item(1) '设置英文语音,只读英文
        ' voice.Rate = trackBarSpeed.Value '设置语速
        'voice.Volume = trackBarVolumn.Value '设置音量
        ss.SelectVoice(ComboxVoice.SelectedItem) '选择语音引擎
        ss.Rate = trackBarSpeed.Value '设置语速
        ss.Volume = trackBarVolumn.Value '设置音量
        'voice.Speak(txtMsg.Text, SpeechVoiceSpeakFlags.SVSFDefault) '同步朗读
        ' voice.Speak(txtMsg.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync) '异步朗读
        ss.SpeakAsync(txtMsg.Text) '异步发出语音
 
    End Sub
 
    Private Sub ButtonPause_Click(sender As Object, e As EventArgs) Handles ButtonPause.Click
        ss.Pause() '暂停播放
        'voice.Pause()
    End Sub
 
    Private Sub ButtonContinue_Click(sender As Object, e As EventArgs) Handles ButtonContinue.Click
        ss.Resume() '继续播放
        'voice.Resume()
    End Sub
 
    Private Sub ButtonRecord_Click(sender As Object, e As EventArgs) Handles ButtonRecord.Click
        Dim ss As SpeechSynthesizer = New SpeechSynthesizer() '定义一个局部变量
        ss.Rate = trackBarSpeed.Value
        ss.Volume = trackBarVolumn.Value
        Dim sfd As SaveFileDialog = New SaveFileDialog() '定义一个文件保存提示框,并且实例化
        sfd.Filter = "Wave Files|*.wav" '设置文件类型为语音文件(.wav)
        If sfd.ShowDialog = DialogResult.OK Then '如果选择了文件名
            ss.SetOutputToWaveFile(sfd.FileName) '输出音频文件
            ss.Speak(txtMsg.Text)
            ss.SetOutputToDefaultAudioDevice()
            MessageBox.Show("完成录音~~", "提示")
        End If
    End Sub
 
    Private Sub ButtonClose_Click(sender As Object, e As EventArgs) Handles ButtonClose.Click
        Application.Exit() '应用退出
    End Sub
End Class

    转载于:https://blog.51cto.com/10882708/1890438

    相关技术文章

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载