|
-
Apr 15th, 2016, 11:38 PM
#1
Thread Starter
New Member
Morse Code Translator - Play Audio & Image Sequentially Issue
The Morse Code Translator is for a school project. I'm fairly inexperienced with coding. I'll try to explain the program the best I can.
There are 2 Textboxes and 1 Button. Input a character, word or sentence into TextBox1, Click the Translate Button, and then TextBox2 will show those characters in Morse Code.
The button also plays Audio from my resources. (If I type A and click the button, a 'dit + dah' (.-) will play)
The button also flashes a picturebox with an image of a light that corresponds to the dits and dahs.
The Problem
When there are 2 or more characters entered, only the audio for the last letter plays. (Type CAT, only audio for T plays)
I countered this by using WaitToComplete instead of Background
My.Computer.Audio.Play(My.Resources.MorseA, _
AudioPlayMode.WaitToComplete)
And it worked, it played the audio for C, A then T. BUT WaitToComplete stops executing code, and now my pictureboxes won't flash. I've tried using timers and progressbars, but I'm really struggling to get it right.
Is there a way to play the audio files sequentially, without stopping the code from executing, allowing the pictureboxes to flash with the audio?
This is probably not a great explanation, but please do ask questions if you don't understand something. And thanks in advance for the help!
-
Apr 16th, 2016, 07:09 AM
#2
Re: Morse Code Translator - Play Audio & Image Sequentially Issue
try a BackgroundWorker for playing the audio. put the code for it in the DoWork event. that will run it on a secondary thread. leave the rest of the code that deals with images where it is at behind the button click.
-tg
-
Apr 16th, 2016, 08:04 AM
#3
Thread Starter
New Member
Re: Morse Code Translator - Play Audio & Image Sequentially Issue
I tried doing a bit of research on background workers, but I still don't understand how to use them although it possibly could be a solution.
The audio is in the DoWork event but I'm not sure how to use it.
Here's The Code before I tried using BackgroundWorker ---- For Morse A and B... etc.
Code:
Function EngToMorse(ByVal ToMorse As String) As Boolean
Select Case ToMorse
Case "A"
TextBox2.Text = TextBox2.Text & " " & A
My.Computer.Audio.Play(My.Resources.MorseA, _
AudioPlayMode.Background)
Case "B"
TextBox2.Text = TextBox2.Text & " " & B
My.Computer.Audio.Play(My.Resources.MorseB, _
AudioPlayMode.Background)
Dim A As String = ".-"
Dim B As String = "-..."
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim text As String = TextBox1.Text.ToUpper() 'Convert the text entered to upper case
TextBox2.Text = "" 'Refresh the Output Textbox
For i As Integer = 0 To text.Length - 1
EngToMorse(text.Chars(i).ToString()) 'Grabs letters sequentially
Next
End Sub
Definitely not the most efficient way.
Also, I'm not worrying about the images right now. Just getting audio to play for each character in the textbox without WaitToComplete.
But where and how do I get the BackgroundWorker to Play Audio?
-
Apr 17th, 2016, 10:49 PM
#4
Re: Morse Code Translator - Play Audio & Image Sequentially Issue
something like this:
Code:
Imports System.ComponentModel
Public Class Form1
Private WithEvents bgw As New BackgroundWorker
Private Delegate Sub appendTextCallBack(ByVal text As String)
Private Sub appendText(ByVal text As String)
If TextBox2.InvokeRequired Then
TextBox2.Invoke(New appendTextCallBack(AddressOf appendText), text)
Else
TextBox2.Text &= " " & text
End If
End Sub
Private Delegate Sub showImageCallBack(ByVal img As Bitmap)
Private Sub showImage(ByVal img As Bitmap)
If PictureBox1.InvokeRequired Then
PictureBox1.Invoke(New showImageCallBack(AddressOf showImage), img)
Else
PictureBox1.Image = img
PictureBox1.Refresh()
End If
End Sub
Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
Dim text As String = e.Argument.ToString
For i As Integer = 0 To text.Length - 1
appendText(text.Chars(i).ToString())
showImage([yourImageFile])
My.Computer.Audio.Play(DirectCast(My.Resources.ResourceManager.GetObject("Morse" & text.Chars(i).ToString()), IO.Stream), _
AudioPlayMode.WaitToComplete)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim text As String = TextBox1.Text.ToUpper() 'Convert the text entered to upper case
TextBox2.Text = "" 'Refresh the Output Textbox
bgw.RunWorkerAsync(text)
End Sub
End Class
Last edited by .paul.; Apr 17th, 2016 at 10:55 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 18th, 2016, 06:25 AM
#5
Re: Morse Code Translator - Play Audio & Image Sequentially Issue
Almost... I figured the image handling could remain in the main thread, and only handle the audio portion from the BGW. I'm guessing that there's more than one image involved, as far as I know PictureBoxes don't show animated images (I could be wrong) and so in order to show even a simple "dat" it would have to swap in the "on" image, then swap in the "off" image. But the playing of the audio files interferes with that, which is why I suggested offloading the audio to the alternate thread.
-tg
-
Apr 18th, 2016, 06:30 AM
#6
Re: Morse Code Translator - Play Audio & Image Sequentially Issue
Pictureboxes can show animated gifs but they tend to lag...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 18th, 2016, 07:07 AM
#7
Re: Morse Code Translator - Play Audio & Image Sequentially Issue
That's cool, I didn't know that... it's been sometimes since I've done anything "that fancy pants". There isn't usually much call for animation in enterprise software.
-tg
-
Apr 28th, 2016, 07:22 PM
#8
Thread Starter
New Member
Re: Morse Code Translator - Play Audio & Image Sequentially Issue
I've taken some time to understand your code .paul. and it's working great! Just had to adapt it a bit to fit the project. But there were a few problems, and some I could luckily solve on my own. But there are 2 big issues I don't know how to get around.
1. When I use a space " " or any other character that is not a letter "," or " ' " in the textbox, the program crashes. I think it's because there isn't a file audio file for those characters. Is there a way to make exceptions for every other character, so it does not search for an audio file?
(Crashes at the audio line below)
Code:
For i As Integer = 0 To text.Length - 1
appendText(text.Chars(i).ToString())
showImage(My.Resources.YellowLight)
My.Computer.Audio.Play(DirectCast(My.Resources.ResourceManager.GetObject("Morse" & text.Chars(i).ToString()), IO.Stream), _
AudioPlayMode.WaitToComplete)
Next
2. If you click the button to translate again, while bgw is doing its work, it crashes and says the bgw is busy. Is there a way to cancel the bgw with the same button or another button.
Thanks for your patience everyone!
-
Apr 29th, 2016, 10:41 AM
#9
Addicted Member
Re: Morse Code Translator - Play Audio & Image Sequentially Issue
Each of your problems can be dealt with using a conditional statement.
In the first case if you only have audio for the upper case letters A through Z the condition could be something like the following which skips anything outside the limits.
Code:
Dim mychar As Integer = Asc(text.Chars(i).ToString)
If mychar > 64 AndAlso mychar < 91 Then 'decimal values A through Z
My.Computer.Audio.Play(DirectCast(My.Resources.ResourceManager.GetObject("Morse" & text.Chars(i).ToString()), IO.Stream), _
AudioPlayMode.WaitToComplete)
End If
The second condition can be used to cancel the BGW or wait until it has finished.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If BackgroundWorker1.IsBusy Then
Else
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|