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
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