Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB.NET
My Vb.net application has two buttons. I want both these buttons to write a wave file such that on clicking the second button, the content of the wave file, created by clicking on the first button, is overwritten. is it possible to do so? Can Wav files be overwritten? If yes, how?
I am trying but it gives "used by another process error".
Posted 8-Oct-12 21:36pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Yes, they can be overwritten - but the problem you are having is that the code you wrote to write teh file in teh first button press (and probably the second as well) does not clean up after itself as it should.
 
If you open a stream (or other file related object) for writing, and do not Close and Dispose of it when you are finished, it remains open until the Garbage Collector decides it is no longer needed or your application ends. This could be next week for all you know!
 
Either use a Using block:
Using MyFile As New FileStream()
   ...
End Using
Or call the Dispose Method when you are finished with writing the file.
  Permalink  
Comments
Member 8244358 at 9-Oct-12 3:52am
   
yes, i am using Filestream method and i am also closing the binarywriter and filestream using the following code:
 
binarywriter.close()
filestream.close()
 
But it is still giving me error!
OriginalGriff at 9-Oct-12 4:00am
   
Show the code for both buttons.
Member 8244358 at 9-Oct-12 12:00pm
   
Yes, your suggestion worked for me. I am able to overwrite the two files now. I was only using
binarywriter.flush()
and not
binarywriter.close().
Now I am using both and it works fine.
 

My next question is how do I make Windows media player shift from playing the first wave file to another on clicking second button and back to first wave file on clicking first button, since the wave file name in both the cases is the same?
OriginalGriff at 9-Oct-12 12:05pm
   
This is from my C# code:
mediaPlayer.Ctlcontrols.stop();
mediaPlayer.URL = playingNowList[playingNow].Location;
lbPlayingNow.SelectedIndex = playingNow;
mediaPlayer.Ctlcontrols.play();
As you can see, I just stop any existing track play, set the URL of the player instance, and start a new one. Works fine for me! :laugh:
Member 8244358 at 10-Oct-12 11:39am
   
so do you mean that I should create a playlist and each time add the same wave file, that I modify, to that playlist and then use your code? Am I right?
OriginalGriff at 10-Oct-12 12:03pm
   
I don't use a playlist - I never found a reliable way of forcing a move to the next song, so I just tell it which song to play and use the PlayStateChange event with a short timer to start the next from my external lists. (The gap gives me a brief quiet between tracks so it it's quite so jarring in transition)
Member 8244358 at 10-Oct-12 12:10pm
   
Okay, then I'll try to give it shot and then let you know. Thanks :)
Member 8244358 at 11-Oct-12 2:19am
   
This is my Vb.Net Code:
Dim tbar as short
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll

tbar=Trackbar1.Value
 
Select Case tbar
Case 1
Dim filestream1 as new filestream("C:\x.wav", FileMode.Create, FileAccess.Write)
Dim writer as new binarywriter(filestream1)
------Code for creating wave file named "x.wav"-------
writer.close()
filestream1.close()
AxWindowsMediaPlayer1.URL = "C:\x.wav"
AxWindowsMediaPlayer1.Ctlcontrols.play()

Case 2
Dim filestream2 as new filestream("C:\x.wav", FileMode.Create, FileAccess.Write)
Dim writer as new binarywriter(filestream2)
------Code for creating wave file named "x.wav"-------
writer.close()
filestream2.close()
AxWindowsMediaPlayer1.URL = "C:\x.wav"
AxWindowsMediaPlayer1.Ctlcontrols.play()

End Sub
 
In both these cases I am modifying the sample values in different ways, however, naming the wav file as "x.wav".
The code produced correct results but when I use WindowsMediaPlayer, it gives error of "is used by another process". On the other hand, when I use-
My.Computer.Audio.Play("C:\x.wav")
then I get accurate results.
Can you tell me what is the problem with using WindowsMediaPlayer?
OriginalGriff at 11-Oct-12 3:47am
   
First off, don't use the root of the C drive for data storage - it will come back to bite you later, as Windows does not like people writing to the root of the boot drive and in Vista and 8 you generally need admin permission to do so - it's an antivirus thing. Use a data folder instead (Either create one, or look here:
http://www.codeproject.com/Tips/370232/Where-should-I-store-my-data
 
Secondly - Close is supposed to be equivalent to Dispose in these cases, but I wouldn't rely on it if I was you - Call Dispose on the filestream and writer instead.
 
Thirdly - It is indeed in use by another process: AxWindowsMediaPlayer which will hold it while it plays. Call stop on the media player before you try to write, and kill it's URL to try to force it to release, and / or use different names for the files (I would not automatically overwrite any file, without asking for user permission first - it's too good a way to annoy people!)
Member 8244358 at 11-Oct-12 9:48am
   
Okay, now I have created a new folder to store the audio data. Then I have also used Dispose() on filestream. But it does not solve my problem. However, I have found a method:
AxWindowsMediaPlayer1.currentplaylist.clear()
I am using this method and also am creating different sound files instead of overwriting a single file.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

A wav file is just a file, of course it can be overwritten.
Well, "used by another process" could mean as well, that it is used by the same process - you might have an unclosed file handle. But you can figure it out who and what is using it using a tool like ProcessExplorer[^], Process Hacker[^] or Unlocker[^].
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  • Yes, WAV files may be overwritten (like any other file).
  • To overwrite a file just write on it.
  • "used by another process" means there is a sharing violation (e.g. another process is reading it while you issued the write operation).
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Gihan Liyanage 332
1 Sergey Alexandrovich Kryukov 250
2 ClimerChinna 222
3 vikinghunter 188
4 OriginalGriff 161
0 Sergey Alexandrovich Kryukov 8,373
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,025
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 9 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100