NAudio change volume in runtime - naudio

I would like change the volume of my audiostream in runtime.
I use this code:
Public Volume as Single = 0.01
Dim Wave1 As New NAudio.Wave.WaveOut
Dim xa() As Byte = IO.File.ReadAllBytes("C:\Song - Come Out and Play.wav")
Sub PlaySound()
Dim data As New IO.MemoryStream(xa)
Wave1.Init(New NAudio.Wave.BlockAlignReductionStream(NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(New NAudio.Wave.WaveFileReader(data))))
Wave1.Volume = Volume
Wave1.Play()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PlaySound()
End Sub
But how can i change the Volume in runtime?
It doesn´t change, when i click a button with
Volume = 2.0
Why?
Thanks for this.
Second problem: How must i change this code, to play MP3 instead of WAV?
Because WAV is to large..
Thanks for both :)
Sorry for my bad English.
Regards, René :)

First question about volume was answered by Mark - Volume is in the range 0.0 <= volume <= 1.0, so setting the volume to 2.0 is invalid.
As to how to use MP3 files...
You can replace the WaveFileReader with Mp3FileReader in your code if the data you are feeding in is MP3 instead of WAV. If the data is always coming from a file you could use new AudioFileReader(filename) instead and let it work out what the compression format is.
Here's your code with MP3 instead of WAV:
Public Volume as Single = 0.01
Dim Wave1 As New NAudio.Wave.WaveOut
Dim xa() As Byte = IO.File.ReadAllBytes("C:\Song - Come Out and Play.mp3")
Sub PlaySound()
Dim data As New IO.MemoryStream(xa)
Wave1.Init( _
New NAudio.Wave.BlockAlignReductionStream( _
NAudio.Wave.WaveFormatConversionStream.CreatePcmStream( _
New NAudio.Wave.Mp3FileReader(data) _
)))
Wave1.Volume = Volume
Wave1.Play()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PlaySound()
End Sub

The volume property has a maximum value of 1.0 which indicates 1.0. The latest NAudio will throw an exception if you try to set it any higher.

Related

Windows Media Player VB.net

I have a WMP in my vb.net project and I wanted to load the next media automatically after the first is finnished I did some research on googel and found the simple to understand code as per below.
Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
AxWindowsMediaPlayer1.URL = ("Test2.mp4")
MessageBox.Show("Playing End")
End If
End Sub
I however cant get it to automatically play the next (Test2.mp4) unless I have the messagebox pop-up. I discovered this purely by accident. However I dont want the Messagebox to pop-up everytime a new Mp4 file is ready to be played. Dose anybody know what is going on here and how I can fix this?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim oTask01 As Threading.Thread
oTask01 = New Threading.Thread(AddressOf oStarting01)
oTask01.Start()
Dim omessagebox As MessageBox = Nothing
omessagebox.Show("Playing End", "", MessageBoxButtons.OK)
oTask01.Abort()
End Sub
Private Function oStarting01() As Byte
While True
System.Windows.Forms.SendKeys.SendWait(vbCr)
End While
Return 0
End Function
End Class
Hi, try with this code. It works. Diving more deeply in Windows system and subsystems is not an easy task, not at least for me. I hope you get what you were looking for for your software. Thank you very much. Happy codding!. :)
Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As
System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent)
Handles AxWindowsMediaPlayer1.PlayStateChange
If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
AxWindowsMediaPlayer1.URL = ("Test2.mp4")
'MessageBox.Show("Playing End") 'This line was commented because is not neccesary in this fragment of code
End If
End Sub
Hi, if I have understood well to you, you wanted to supress the message box. It is got doing a comment's line with "'". I hope you like it and continue enjoying with computers and software. Thank you very much and happy codding. :)

VB.NET Upload to FTP with progressbar

I relise there is so many other questions out there regarding the progressbar, though I've looked through them "all" and can not find one that works.
I am trying to upload c:\screenshot.png to my ftp with a progress bar and a msgbox once finished.
Could someone provide a working example for me?
Thankyou
Edit heres the code I tried. Uploading works, though the progress bar dosent.
Sub UpdateProgressBar(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs)
If ProgressBar1.InvokeRequired Then
ProgressBar1.Invoke(New UploadProgressChangedEventHandler(AddressOf UpdateProgressBar), sender, e)
Exit Sub
End If
ProgressBar1.Value = CInt(ProgressBar1.Minimum + _
((ProgressBar1.Maximum - ProgressBar1.Minimum) * _
e.ProgressPercentage) / 100)
End Sub
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Label16.Text = "Uploading now..."
Label16.Update()
Dim client As New System.Net.WebClient()
AddHandler client.UploadProgressChanged, AddressOf UpdateProgressBar
With client
.Credentials = New NetworkCredential( _
"damon#slimar.eu", "mine123!")
.UploadFile("ftp://slimar.eu/screenshot.png", "C:\screenshot.png")
End With
Label16.Text = "Done!"
Label16.Update()
End Sub
Progress bar has minValue,Max value, StepValue which is used to perform a step and Value to setup arbitray value.When you uploading a file or downloading you should be able to see via e paramenter total byte and actual byte trasmission.So you can setup Progress bar value and max value.
Also personally i invite you to use backgroundworker which :
Not Freeze GUI
Give you much controll on thread with no issue and no invoke needs
Make it more simple :)

Check A Picturebox For A Specific Image Name

I need to check a picturebox for a specific image. I know you can check if the picturebox is populated with an image...
If Not pictureBox.Image is Nothing Then
Else
End If
But in my case, I need to check this picturebox for an image I loaded earlier on in the process.
Here is the current code I'm using to load the image...
PictureBox1.Image = My.Resources.TestImage1
I thought by using the following code I could check the image name, but this apparently does not work.
If PictureBox1.Image = My.Resources.TestImage1 Then
'do something
Else
'do something else
End if
Suggestions?
Image does not have any knowledge of the file name or any other name that it has been loaded from. What you can do, however, is compare images pixel-by-pixel. Try this code:
Public Function AreSameImage(ByVal I1 As Image, ByVal I2 As Image) As Boolean
Dim BM1 As Bitmap = I1
Dim BM2 As Bitmap = I2
For X = 0 To BM1.Width - 1
For y = 0 To BM2.Height - 1
If BM1.GetPixel(X, y) <> BM2.GetPixel(X, y) Then
Return False
End If
Next
Next
Return True
End Function
Credit goes here.
A useful article I found when looking for this answer:
Compare two images to find differences greater than a threshold value in VB .NET
This is how you can check if your images are less than 100% equal, i.e. similar.
Dim a as image=my.resources.image1.jpg' imported file from resources
Dim b as image=my.resources.image2.jpg' imported file from resources
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
picturebox1.image=a
picturebox2.image=b
end sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
if picturebox1.image is a and picturebox2.image=b then
picturebox2.image=a
picturebox1.image=nothing
else
picturebox2.image=b
picturebox1.image=a
end if
end sub
..................Just try it! :)

Text is selected in the text box

When I load the form where some text has been given to text box. All the text in that textbox is highlighted. I want vb not to load it this way.
How to fix it.
Thanks
Furqna
You could set the tab index on your textbox to something else so that it's not the lowest index.
You could set the TextBox1.SelectionLength = 0 in the form.activated event.
I don't like this as much because if the user had the text hilited and minized the application then they will lose the hilite, but is fairly easy to do. I guess you could use a flag to make sure it only did it on the first activate.
You could set a timer event in the load to clear it immediately after the load event, but that seems like overkill. I have worked at places where they had a standard function that happened on every form 100 ms after load because of problems such as this.
You could try this(it looks like a workaround):
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.SelectionStart = TextBox1.Text.Length
End Sub
It depends on the TabIndex of your TextBox, if it has the lowest TabIndex it gets focus and therefore it's Text is selected.
' VS.net 2013. Use the "Shown" event.
' GotFocus isn't soon enough.
Private Sub Form_Shown(sender As Object, e As EventArgs) Handles Me.Shown
TB.SelectionLength = 0
End Sub
Type 1 Method
Dim speech = CreateObject("sapi.spvoice")
speech.speak(TextBox1.Text)
Type 2 Method
Dim oVoice As New SpeechLib.SpVoice
Dim cpFileStream As New SpeechLib.SpFileStream
'Set the voice type male or female and etc
oVoice.Voice = oVoice.GetVoices.Item(0)
'Set the voice volume
oVoice.Volume = 100
'Set the text that will be read by computer
oVoice.Speak(TextBox1.Text, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault)
oVoice = Nothing
Type 3 Method
Imports System.Speech.Synthesis
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim spk As New SpeechSynthesizer
For Each voice As InstalledVoice In spk.GetInstalledVoices
ListBox1.Items.Add(voice.VoiceInfo.Name)
Next
ListBox1.SelectedIndex = 0
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim spk As New SpeechSynthesizer
spk.SelectVoice(ListBox1.SelectedItem.ToString)
spk.Speak(TextBox1.Text)
End Sub
End Class
This will also happen sometimes if The TextChanged or other similar Event is fired twice for the control.
When creating each form. Each object is indexed you can set the tab Index higher then the indexed object. Example: On the third form you put a text box in.
private void textBox1_TextChanged(object sender, EventArgs e)
This was the 12th object in the project, it would be indexed at 12. if you put the tab index higher then the indexed objects throughout the project. Tab index 1000 (problem solved.)
Have a great day.
Scooter

How to get image from vb6 MSFlexGrid OLEDragDrop event

I have a VB project that is converted from VB6 to VB.NET.
In this, I have a MSFlexGrid that is used as an interop compatibiliy. That means it is somewhat converted to .NET, but internally, many of the mechanisms are still from VB6/COM.
I need to drag an image from a PictureBox (which is .NET) and drop it on the flexgrid.
This is what I do to initialize the drag:
Private Sub picStartSymbol_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picStartSymbol.MouseDown
picStartSymbol.DoDragDrop(picStartSymbol.Image, DragDropEffects.Copy)
End Sub
And this is where I catch the drop in the FlexGrid:
Private Sub flxConstructionPoints_OLEDragDrop(ByVal sender As Object, ByVal e As AxMSFlexGridLib.DMSFlexGridEvents_OLEDragDropEvent) Handles flxConstructionPoints.OLEDragDrop
Dim image As Image
Dim oleImage As Object
oleImage = e.data.GetData(2) ''This gets an object of type 2 (bitmap)
''How to convert oleImage to a .NET Image?
End Sub
I don't have VB6 anymore so I can't test this but try adding a reference to Microsoft.VisualBasic.Compatibility and then call:
Dim image as Image = Microsoft.VisualBasic.Compatibility.VB6.IPictureToImage(oleImage)
or
Dim image as Image = Microsoft.VisualBasic.Compatibility.VB6.IPictureDispToImage(oleImage)