[Newbie]Problem writing streaming audio using NetworkStream

K

keithh

I've been using C# for a few weeks now and I'm still working on some of
the intricacies and form. My goal is to write a simple C# app that
will generate a list of files and stream them to a port on my computer.
I populate a string array (mp3String) with a couple of mp3 files
(a.mp3 and b.mp3) and would like them to stream in order.

I'm not sure if this is the correct way to load an mp3 file and stream
it out to a port. If I'm headed in the wrong direction, please advise.

//------------------------------------------------------------------------------------------------

// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1"); //testing

TcpListener server = new TcpListener(localAddr, port);
server.Start();

int songCount = mp3String.Length;
int currentSongIndex = 0;

//wait for client to connect
TcpClient client = server.AcceptTcpClient();

// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();

while(true) //true for testing
{
//load music file into byte array
FileStream myFileStream =
new FileStream(mp3String[currentSongIndex],
FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] b = new byte[myFileStream.Length];
for(long ix = 0; ix < myFileStream.Length; ix++)
b[ix] = (byte)myFileStream.ReadByte();
myFileStream.Close();

stream.Write(b, 0, b.Length);
stream.Flush();
currentSongIndex++;
}
//------------------------------------------------------------------------------------------------


Basic synopsis: I'm loading the mp3 file into a byte array, then
writing the bytes to the NetworkStream. Both files are loading and
making their way out the port. My problem is...I know that the
while(true) loop will continue looping forever...I do not know how to
detect that the NetworkStream is busy and shouldn't continue looping.
I need the loop to load one song and wait until the song is completely
streamed before loading another. How do you do this? I tried using
NetworkStream's position property...but it is not currently supported.
I appreciate any ideas or hints!!!

Keith
 
J

Jon Skeet [C# MVP]

I've been using C# for a few weeks now and I'm still working on some of
the intricacies and form. My goal is to write a simple C# app that
will generate a list of files and stream them to a port on my computer.
I populate a string array (mp3String) with a couple of mp3 files
(a.mp3 and b.mp3) and would like them to stream in order.

I'm not sure if this is the correct way to load an mp3 file and stream
it out to a port. If I'm headed in the wrong direction, please advise.

Well, you're loading it very inefficiently - it would be much better to
read a chunk at a time rather than a byte at a time.

Just read and write chunks:

byte[] buffer = new byte[32*1024]; // 32K buffer

using (Stream fileStream = new FileStream (mp3String[currentSongIndex],
(etc))
{
int length;
while ( (length=fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
stream.Write (buffer, 0, length);
}
stream.Flush();
}

Anyway, going back to your original problem - I wouldn't worry about
it. Just keep writing to the stream. If it gets backed up, the call to
Write or Flush will block.

If you just have an array of MP3 names to use though, then rather than
using while (true), do:

foreach (String song in mp3String)
{
...
}

That way, you'll just go through all the songs and then stop.
 
C

Cool Guy

I need the loop to load one song and wait until the song is completely
streamed before loading another. How do you do this?

You're already doing it. NetworkStream.Write blocks execution until the
data has been written (or something goes wrong). From the documentation:

"The Write method blocks until the requested number of bytes are sent or a
SocketException is thrown."
 
C

Cool Guy

I said:
"The Write method blocks until the requested number of bytes are sent or a
SocketException is thrown."

Actually, I believe that the documentation is incorrect here. I believe it
should instead be:

"The Write method blocks until the requested number of bytes are sent or
*an IOException* is thrown."

http://tinyurl.com/bttn8
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top