退出线程最简单的方法?

时间:2014-02-21 16:12:02

标签: vb.net

我有一个sub从谷歌翻译获得tts语音,然后保存到我的本地文件夹然后播放它。问题是谷歌只接受100个字符,所以我编写了一些东西来掩盖它。我正在使用一个单独的线程来执行此操作。

Option Strict On
Option Explicit On

Imports NAudio
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions
Imports WMPLib
Imports NAudio.Wave
Imports System.Runtime.InteropServices
Imports System.Text

Public Sub GoogleTTS(ByVal response As String)
    Try
        Dim thread As New Threading.Thread(AddressOf downloadFile)
        Player.controls.stop()
        sub_res = response
        If response.Length <= 100 Then
            thread_response = response
            convert_filename = Regex.Replace(response, "[^A-Za-z0-9]+", String.Empty)
        Else
            thread_responses = response.Split(CChar(","), CChar("."), CChar("!"), CChar(";"), CChar("?"))
            Dim i As Integer = 0
            ReDim convert_filenames(thread_responses.Count())
            For Each responses As String In thread_responses
                convert_filenames(i) = Regex.Replace(responses, "[^A-Za-z0-9]+", String.Empty)
                i += 1
            Next
        End If
        thread.Start()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

   Private Sub downloadFile()
    If sub_res.Length <= 100 Then
        Dim files = Directory.GetFiles("SpeechBank", convert_filename & ".wav")
        If files.Length > 0 Then
            PlayFile("SpeechBank\" & convert_filename & ".wav")
        Else
            Dim HttpReq As HttpWebRequest = DirectCast(WebRequest.Create("http://translate.google.com/translate_tts?tl=en&q=" & thread_response), HttpWebRequest)
            Using HttpResponse As HttpWebResponse = DirectCast(HttpReq.GetResponse(), HttpWebResponse)
                Using Reader As New BinaryReader(HttpResponse.GetResponseStream())
                    Dim RdByte As Byte() = Reader.ReadBytes(1 * 1024 * 1024 * 10)
                    Using FStream As New FileStream("SpeechBank/" & convert_filename & ".wav", FileMode.Create)
                        FStream.Write(RdByte, 0, RdByte.Length)
                    End Using
                End Using
            End Using
            PlayFile("SpeechBank\" & convert_filename & ".wav")
        End If
    ElseIf sub_res.Length > 100 Then   
        ReDim lastFiles(thread_responses.Length - 1)
        For x As Integer = 0 To thread_responses.Length - 1
            Dim files = Directory.GetFiles("SpeechBank", convert_filenames(x) & ".wav")
            If files.Length > 0 Then
                PlayFile("SpeechBank\" & convert_filenames(x) & ".mp3")
                System.Threading.Thread.Sleep(GetWavFileDuration("SpeechBank\" & convert_filenames(x) & ".mp3"))
            Else
                Dim HttpReq As HttpWebRequest = DirectCast(WebRequest.Create("http://translate.google.com/translate_tts?tl=en&q=" & thread_responses(x)), HttpWebRequest)
                Using HttpResponse As HttpWebResponse = DirectCast(HttpReq.GetResponse(), HttpWebResponse)
                    Using Reader As New BinaryReader(HttpResponse.GetResponseStream())
                        Dim RdByte As Byte() = Reader.ReadBytes(1 * 1024 * 1024 * 10)
                        Using FStream As New FileStream("SpeechBank\" & convert_filenames(x) & ".wav", FileMode.Create)
                            FStream.Write(RdByte, 0, RdByte.Length)
                        End Using
                    End Using
                End Using
                System.Threading.Thread.Sleep(50)
                PlayFile("SpeechBank\" & convert_filenames(x) & ".mp3")
                System.Threading.Thread.Sleep(GetWavFileDuration("SpeechBank\" & convert_filenames(x) & ".mp3"))
                System.Threading.Thread.Sleep(10)
            End If
        Next
    End If
End Sub

我的问题是当我使用我的GoogleTTS sub并且我输入的字符串超过100时我跟着它再次使用不同的字符串调用它。线程似乎重叠,同时播放语音​​。我怎么能防止这种情况?

这是googletts sub中包含的其他subs的其他代码片段。

Public Sub PlayFile(ByVal url As String)
    Try
        Player = New WMPLib.WindowsMediaPlayer
        Player.URL = url
        Player.controls.play()
    Catch ex As Exception
    End Try
End Sub

 Public Function GetWavFileDuration(ByVal fileName As String) As TimeSpan
    Dim wf As New Mp3FileReader(fileName)
    Return wf.TotalTime
End Function


  Public Sub stopFile(ByVal url As String)
    Try
        Player.URL = url
        Player.controls.stop()
    Catch ex As Exception
    End Try
End Sub

0 个答案:

没有答案