Audio Recording in vb.net - vb.net

help me I want to make a program that record mic in vb.net I got this code but I cant save audio on different location
Imports System.Runtime.InteropServices
Public Class Form1
'Dim recording As Boolean = False
Dim Filez As String = "C:\rec.mp3"
<DllImport("winmm.dll")>
Private Shared Function mciSendString(ByVal command As String, ByVal buffer As String, ByVal bufferSize As Integer, ByVal hwndCallback As IntPtr) As Integer
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)
mciSendString("record recsound", "", 0, 0)
Button1.Text = "Stop"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
My.Computer.Audio.Play(Filez)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
mciSendString("save recsound " & Filez, "", 0, 0)
mciSendString("close recsound ", "", 0, 0)
Button2.Enabled = True
End Sub
End Class
When i change path of Filez for save at another place it show error
Please help

You just need to launch your application with administrator rights.

because there is a space in the new file path
try this the full code will be
all mciSendString function commands here
https://msdn.microsoft.com/en-us/library/windows/desktop/dd743572(v=vs.85).aspx
Imports System.Runtime.InteropServices
Public Class Form1
Dim Filez As String = ""
Dim isrecording As Boolean = False
<DllImport("winmm.dll")>
Private Shared Function mciSendString(ByVal command As String, ByVal buffer As String, ByVal bufferSize As Integer, ByVal hwndCallback As IntPtr) As Integer
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not (isrecording) Then
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)
mciSendString("record recsound", "", 0, 0)
Button1.Text = "pause"
isrecording = True
Else
mciSendString("pause recsound ", "", 0, 0)
Button1.Text = "record"
isrecording = False
End If
Button3.Enabled = True
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim savedialouge As New SaveFileDialog
savedialouge.Filter = "wav file|*.wav"
If savedialouge.ShowDialog = Windows.Forms.DialogResult.OK Then
Filez = savedialouge.FileName
mciSendString("save recsound " & """" + Filez + """", "", 0, 0)
mciSendString("close recsound ", "", 0, 0)
Button1.Text = "new record"
Button2.Enabled = True
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
My.Computer.Audio.Play(Filez)
End Sub
End Class

Related

How to save my wlan settings?

I'm currently using Visual Studio 2012. My form code is given below with design screenshots. I am trying to make a virtual hotspot for my PC but I want to add one feature which takes previous SSID and password. My hotspot opens every time with blank text.
Here's my form design:
Form1 Design:
[
Form1 code:
Imports System.Runtime.InteropServices
Imports System.Collections.ObjectModel
Imports System.Text
Imports NativeWifi
Public Class Form1
Public Const WM_NCLBUTTONDOWN As Integer = &HA1
Public Const HT_CAPTION As Integer = &H2`
<DllImportAttribute("user32.dll")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, _
ByVal Msg As Integer, ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
End Function
<DllImportAttribute("user32.dll")> _
Public Shared Function ReleaseCapture() As Boolean
End Function`
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim wlan = New WlanClient()
Dim connectedSsids As Collection(Of [String]) = New Collection(Of String)()
For Each wlanInterface As WlanClient.WlanInterface In wlan.Interfaces
Dim ssid As Wlan.Dot11Ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid
connectedSsids.Add(New [String](Encoding.ASCII.GetChars(ssid.SSID, 0, CInt(ssid.SSIDLength))))
For Each item As String In connectedSsids
Label1.Text = item
Next
Next
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
ReleaseCapture()
SendMessage(Handle, WM_NCLBUTTONDOWN, _
HT_CAPTION, 0)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If Form2.TextBox1.Text = "" Then
MsgBox("Hotspot name cant be empty", MsgBoxStyle.Critical)
End If
If Form2.TextBox2.TextLength < 8 Then
MsgBox("Password should be 8+ character", MsgBoxStyle.Critical)
If Form2.TextBox2.Text = "" Then
MsgBox("Password cant be empty", MsgBoxStyle.Critical)
End If
Else
Dim process As New Process()
process.StartInfo.Verb = "runas"
process.StartInfo.UseShellExecute = True
process.Start("cmd", String.Format("/c {0} & {1} & {2}", "netsh wlan set hostednetwork mode=allow ssid=" & Form2.TextBox1.Text & " key=" & Form2.TextBox2.Text, "netsh wlan start hostednetwork", "pause"))
Form2.CheckBox1.Enabled = False
Form2.TextBox1.Enabled = False
Form2.TextBox2.Enabled = False
MsgBox("Hotspot started Successfully", MsgBoxStyle.Information)
End If
Catch
MsgBox("Failed to Establish a hotspot", MsgBoxStyle.Exclamation)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Process.Start("CMD", "/C netsh wlan stop hostednetwork")
Form2.CheckBox1.Enabled = True
Form2.TextBox1.Enabled = True
Form2.TextBox2.Enabled = True
MsgBox("Hotspot stopped Successfully", MsgBoxStyle.Information)
End Sub
Private Sub Clsbtn_Click(sender As Object, e As EventArgs) Handles Clsbtn.Click
Me.Close()
End Sub
Private Sub Minbtn_Click(sender As Object, e As EventArgs) Handles Minbtn.Click
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
End Sub
Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
Me.Hide()
Form2.Show()
End Sub
End Class
Form2 Design:
[

FileNotFoundException exception on My.Computer.Audio.Play

I'm making a program for recording audio.
Code:
Imports System.Runtime.InteropServices
Public Class Form1
Private Declare Function record Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
record("open new Type waveaudio Alias recon", "", 0, 0)
record("record recon", "", 0, 0)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
record("save recon c:\mic.wav", "", 0, 0)
record("close recon", "", 0, 0)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
My.Computer.Audio.Play("c:\mic.wav", AudioPlayMode.Background)
End Sub
End Class
Error occurs on line My.Computer.Audio.Play("c:\mic.wav", AudioPlayMode.Background):
FileNotFoundException was unhandled
Please be sure a sound file exists at the specified location.
This works For me try it and see
Public Class Form1
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
Button2.Enabled = True
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)
mciSendString("record recsound", "", 0, 0)
Label1.Text = "Recording..."
Label1.Visible = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button1.Enabled = True
Button2.Enabled = False
Button3.Enabled = True
mciSendString("save recsound c:\recsound.wav", "", 0, 0)
mciSendString("close recsound", "", 0, 0)
MsgBox("File Created: C:\recsound.wav")
Label1.Text = "Stopped..."
Label1.Visible = False
My.Computer.Audio.Stop()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Label1.Text = "Playing..."
Label1.Visible = True
My.Computer.Audio.Play("c:\recsound.wav", AudioPlayMode.Background)
End Sub
End Class

VB.NET How To Continuously Read TCP Stream

Morning,
After much mucking around I have finally got my little TCP Listener application to connect to my server and listen to the traffic.
I am able to connect and get the initial response from the port however it does not return the stream traffic constantly, I need to constantly monitor the ports traffic so I can effectively post it to a database, can anyone help me with how I would loop this?
Here is my code at the moment:
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Runtime.InteropServices ' DllImport
Imports System.Security.Principal ' WindowsImpersonationContext
Imports System.Text
Public Class Form1
Private Delegate Sub AppendTextBoxDelegate(ByVal TB As RichTextBox, ByVal txt As String)
Private Sub AppendTextBoxes(ByVal TB As RichTextBox, ByVal txt As String)
If TB.InvokeRequired Then
TB.Invoke(New AppendTextBoxDelegate(AddressOf AppendTextBoxes), New Object() {TB, txt})
Else
TB.Text = ""
TB.Text = RichTextBox1.Text + Environment.NewLine + " >> " + txt
End If
End Sub
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
AppendTextBoxes(RichTextBox1, "Client Started")
End Sub
Private Sub My_BgWorker_DoWork1(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
clientSocket.Connect("192.168.1.22", 21055)
AppendTextBoxes(RichTextBox1, "Client Socket Program - Server Connected ...")
Dim serverStream As NetworkStream = clientSocket.GetStream()
If serverStream.CanRead Then
Do While clientSocket.Connected
Dim outStream As Byte() = _
System.Text.Encoding.ASCII.GetBytes("Message from Client$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
Dim inStream(10024) As Byte
serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
Dim returndata As String = _
System.Text.Encoding.ASCII.GetString(inStream)
AppendTextBoxes(RichTextBox1, "Data from Server : " + returndata)
Console.WriteLine("Data from Server : " + returndata)
Loop
Else
AppendTextBoxes(RichTextBox1, "No Data to Receive")
clientSocket.Close()
serverStream.Close()
End If
Catch ex As Exception
MessageBox.Show(Environment.NewLine & ex.Message & Environment.NewLine & ex.StackTrace, "Dumping To Log", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
End Try
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
Handles BackgroundWorker1.ProgressChanged
'AppendTextBoxes(RichTextBox1.Text, "DONE!")
'ProgBar.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles BackgroundWorker1.RunWorkerCompleted
If e.Cancelled Then
MsgBox("Cancelled")
End If
Console.WriteLine("Finished Processing - Background Worker 1")
AppendTextBoxes(RichTextBox1, "Finished Getting Stream!")
End Sub
End Class
Any help is greatly appreciated, this is for a project I need to have in by 1st Jan 2015 :(
James
You have your condition to run while connected which means thats true the first time. Change that to:
Do Until Not Client.Connected

How to simulate mouse click?

I'm trying to make a program to click with keyboard like in Osu!.
I've tried SendKeys(), RaiseMouseEvent() and OnMouseClick(). Now I'm trying this and can't get anything to work.
The error I keep getting is:
PInvoke restriction: cannot return variants.
Public Class Form1
Dim onn As Boolean = False
Declare Function mc Lib "user32.dll" Alias "mouse_event" (flag, x, y, button, extra)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not onn Then
Button1.Text = "Off"
Label1.Text = "Status: On"
onn = True
ElseIf onn Then
Button1.Text = "On"
Label1.Text = "Status: Off"
onn = False
End If
End Sub
Private Sub Form1_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If onn And (e.KeyChar = "Z" Or e.KeyChar = "X" Or e.KeyChar = "z" Or e.KeyChar = "x") Then
mc(&H2, 0, 0, 0, 0)
mc(&H4, 0, 0, 0, 0)
End If
End Sub
End Class
This examples clicks where the mouse currently is when the feature is in the "onn" state:
Public Class Form1
Private onn As Boolean = False
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, _
ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, _
ByVal dwExtraInfo As Integer)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
Button1.Text = "Off"
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
onn = Not onn
Button1.Text = IIf(onn, "On", "Off")
End Sub
Private Sub Form1_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If onn Then
Select Case e.KeyChar
Case "Z", "z", "X", "x"
mouse_event(&H2, 0, 0, 0, 0)
mouse_event(&H4, 0, 0, 0, 0)
End Select
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Debug.Print("Button2")
End Sub
Private Sub Button3_Click(sender As Object, e As System.EventArgs) Handles Button3.Click
Debug.Print("Button3")
End Sub
End Class
Public Class Iconform
Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up
Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.Click
SettingsForm.Show()
End Sub
Private Sub OptionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptionsToolStripMenuItem.Click
SettingsForm.Show()
End Sub
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Dim bHandled As Boolean = False
Dim xPos As Integer = Windows.Forms.Cursor.Position.X.ToString
Dim zPos As Integer = Windows.Forms.Cursor.Position.Y.ToString
Select Case e.KeyCode
Case Keys.Right
Windows.Forms.Cursor.Position = New Point(xPos + 10, zPos)
Case Keys.Left
Windows.Forms.Cursor.Position = New Point(xPos - 10, zPos)
Case Keys.Down
Windows.Forms.Cursor.Position = New Point(xPos, zPos + 10)
Case Keys.Up
Windows.Forms.Cursor.Position = New Point(xPos, zPos - 10)
Case Keys.D
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
End Select
End Sub
End Class
Try using the PerformClick() method:
Button1.PerformClick()
In your code it could be like:
If onn And (e.KeyChar = "Z" Or e.KeyChar = "X" Or e.KeyChar = "z" Or e.KeyChar = "x") Then
Button1.PerformClick()
End If

Get InvalidOperatorException was unhandled upon compiling

Everything was compiling just fine until validation for text fields was implemented.
Why won't it compile?
An error occurred creating the form.
See Exception.InnerException for
details. The error is: External
component has thrown an exception.
The source:
Public Class frmAddStudent
Dim aryData(6) As String
Public Shared newStudentRecord As String
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.DialogResult = DialogResult.Cancel
End Sub
Private Sub btnAddStudent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddStudent.Click
'place all the student field data into the array of elements
aryData(0) = txtFirstName.Text
aryData(1) = txtLastName.Text
aryData(2) = txtMajor.Text
aryData(3) = txtPhone.Text
aryData(4) = txtEmail.Text
aryData(5) = txtGPA.Text
'join the array elements and place the result into a string variable
newStudentRecord = Join(aryData, " ")
'create a link between the form and the file with streamwriter and write to the text file
If System.IO.File.Exists(frmMain.FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(frmMain.FILE_NAME, True)
objWriter.WriteLine(newStudentRecord)
objWriter.Close()
End If
Me.DialogResult = DialogResult.OK
End Sub
Private Sub txtFirstName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFirstName.Validating
If txtFirstName.Text.Contains(" ") Or txtFirstName.Text.Length = 0 Or Not System.Text.RegularExpressions.Regex.IsMatch(txtFirstName.Text, "[a-z|A-Z]+$") Then
ErrorProvider1.SetError(txtFirstName, "First Name can't contain spaces or be of zero length and must consist of only letters")
e.Cancel = True
Else
ErrorProvider1.SetError(txtFirstName, "")
End If
End Sub
Private Sub frmAddStudent_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ErrorProvider1.BlinkStyle = ErrorBlinkStyle.NeverBlink
End Sub
Private Sub txtLastName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtLastName.Validating
If txtLastName.Text.Contains(" ") Or txtLastName.Text.Length = 0 Or Not System.Text.RegularExpressions.Regex.IsMatch(txtLastName.Text, "[a-z|A-Z]+$") Then
ErrorProvider1.SetError(txtLastName, "Last Name can't contain spaces or be of zero length and must consist of only letters")
e.Cancel = True
Else
ErrorProvider1.SetError(txtLastName, "")
End If
End Sub
Private Sub txtMajor_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtMajor.Validating
If txtMajor.Text.Length > 0 Then
If Not System.Text.RegularExpressions.Regex.IsMatch(Strings.Left(txtMajor.Text, 1), "[a-z|A-Z]+$") Then
ErrorProvider1.SetError(txtMajor, "Major course code is incorrect")
Else
ErrorProvider1.SetError(txtMajor, "")
End If
Else
ErrorProvider1.SetError(txtMajor, "Major cant' be zero length")
End If
End Sub
End Class
EDIT: Here's also a link to a PrtSc of the problem: http://img88.imageshack.us/i/imageqc.gif/