Problem accessing a function in a nested class
-
I was checking out Ianier Munoz's implementation of streaming audio input using the DirectSound API and C# .NET. My question has to do with a problem I'm facing in C#. The function I want to access is declared as follows: [c] public void Play(nsStreamPlayer.PullAudioCallback pullAudio) { // Bunch of stuff } [/c] nsStreamPlayer is a namespace in which a delegate and an interface are defined. [c] namespace nsStreamPlayer { /// /// Delegate used to fill in a buffer /// public delegate void PullAudioCallback(IntPtr data, int count); /// /// Audio player interface /// public interface IAudioPlayer : IDisposable { int SamplingRate { get; } int BitsPerSample { get; } int Channels { get; } int GetBufferedSize(); void Play(PullAudioCallback onAudioData); void Stop(); } } [/c] The "Play" function is defined in a nested private class [c] private class PullStream : Stream [/c] which itself belongs to a public class StreamingPlayer [c] public class StreamingPlayer : IDisposable, nsStreamPlayer.IAudioPlayer [/c] NOW: On my mainform designer I have a button called btnStartStream. I want to write the code such that clicking btnStartStream calls the play function. How do I do this? Everything I've tried throws up an error message. I know I'm missing something basic. Could you please help me out? Thanks a lot for your time!
-
I was checking out Ianier Munoz's implementation of streaming audio input using the DirectSound API and C# .NET. My question has to do with a problem I'm facing in C#. The function I want to access is declared as follows: [c] public void Play(nsStreamPlayer.PullAudioCallback pullAudio) { // Bunch of stuff } [/c] nsStreamPlayer is a namespace in which a delegate and an interface are defined. [c] namespace nsStreamPlayer { /// /// Delegate used to fill in a buffer /// public delegate void PullAudioCallback(IntPtr data, int count); /// /// Audio player interface /// public interface IAudioPlayer : IDisposable { int SamplingRate { get; } int BitsPerSample { get; } int Channels { get; } int GetBufferedSize(); void Play(PullAudioCallback onAudioData); void Stop(); } } [/c] The "Play" function is defined in a nested private class [c] private class PullStream : Stream [/c] which itself belongs to a public class StreamingPlayer [c] public class StreamingPlayer : IDisposable, nsStreamPlayer.IAudioPlayer [/c] NOW: On my mainform designer I have a button called btnStartStream. I want to write the code such that clicking btnStartStream calls the play function. How do I do this? Everything I've tried throws up an error message. I know I'm missing something basic. Could you please help me out? Thanks a lot for your time!
You have an instance of the
StreamingPlayer
class, correct?StreamingPlayer stmPlyr = new StreamingPlayer();
private void btnStartStream_Click(object sender, EventArgs e)
{
PullAudioCallback cbk = new PullAudioCallback(someAudioCallBack);
stmPlyr.Play(cbk);
}private void someAudioCallBack(IntPtr data, int count)
{
// something here.
}- Nick Parker
My Blog | My Articles -
You have an instance of the
StreamingPlayer
class, correct?StreamingPlayer stmPlyr = new StreamingPlayer();
private void btnStartStream_Click(object sender, EventArgs e)
{
PullAudioCallback cbk = new PullAudioCallback(someAudioCallBack);
stmPlyr.Play(cbk);
}private void someAudioCallBack(IntPtr data, int count)
{
// something here.
}- Nick Parker
My Blog | My ArticlesNick, thanks for the reply! However, I still have a slight problem. The constructor for StreamingPlayer needs three arguments - owner, DirectSound playback device and waveformat. How do I include these in the function call? They are set in the function definition, i.e. public StreamingPlayer(Control owner, Device device, WaveFormat format) { m_Device = device; if (m_Device == null) { m_Device = new Device(); m_Device.SetCooperativeLevel(owner, CooperativeLevel.Normal); m_OwnsDevice = true; } BufferDescription desc = new BufferDescription(format); desc.BufferBytes = format.AverageBytesPerSecond; desc.ControlVolume = true; desc.GlobalFocus = true; m_Buffer = new SecondaryBuffer(desc, m_Device); m_BufferBytes = m_Buffer.Caps.BufferBytes; m_Timer = new System.Timers.Timer(BytesToMs(m_BufferBytes) / 6); m_Timer.Enabled = false; m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed); }
-
Nick, thanks for the reply! However, I still have a slight problem. The constructor for StreamingPlayer needs three arguments - owner, DirectSound playback device and waveformat. How do I include these in the function call? They are set in the function definition, i.e. public StreamingPlayer(Control owner, Device device, WaveFormat format) { m_Device = device; if (m_Device == null) { m_Device = new Device(); m_Device.SetCooperativeLevel(owner, CooperativeLevel.Normal); m_OwnsDevice = true; } BufferDescription desc = new BufferDescription(format); desc.BufferBytes = format.AverageBytesPerSecond; desc.ControlVolume = true; desc.GlobalFocus = true; m_Buffer = new SecondaryBuffer(desc, m_Device); m_BufferBytes = m_Buffer.Caps.BufferBytes; m_Timer = new System.Timers.Timer(BytesToMs(m_BufferBytes) / 6); m_Timer.Enabled = false; m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed); }
Sorry, disregard that...that was a really dumb question. Been working on this too long...not thinking straight. BUT: PullAudioCallback is a delegate defined as public delegate void PullAudioCallback(IntPtr data, int count); When I instantiate a PullAudioCallback object, PullAudioCallback cbk = new PullAudioCallback(...) I need arguments for data and count. What should these be? I tried digging through the source code but I don't see where he calls this function explicitly, though it is integral to the application.
-
Sorry, disregard that...that was a really dumb question. Been working on this too long...not thinking straight. BUT: PullAudioCallback is a delegate defined as public delegate void PullAudioCallback(IntPtr data, int count); When I instantiate a PullAudioCallback object, PullAudioCallback cbk = new PullAudioCallback(...) I need arguments for data and count. What should these be? I tried digging through the source code but I don't see where he calls this function explicitly, though it is integral to the application.
crushinghellhammer wrote: I need arguments for data and count. What should these be? It's a
delegate
which means that it's instance has to represent that signature it defines. Take a look at my example, the example function matches that method signature. Does that make sense? - Nick Parker
My Blog | My Articles -
crushinghellhammer wrote: I need arguments for data and count. What should these be? It's a
delegate
which means that it's instance has to represent that signature it defines. Take a look at my example, the example function matches that method signature. Does that make sense? - Nick Parker
My Blog | My ArticlesI see now! Sorry if this is exasperating...I'm new to C# and sometimes reading other people's code (in this case Ianier's) is a little confusing. My understanding of his intention in using PullAudioCallback is that the delegate is supposed to wait for count bytes to be copied to the data buffer which is an IntPtr before it plays these bytes. What exactly is IntPtr...is it a pointer to a byte array? What would the contents of "someAudioCallBack(IntPtr data, int count" be? Thanks again!
-
I see now! Sorry if this is exasperating...I'm new to C# and sometimes reading other people's code (in this case Ianier's) is a little confusing. My understanding of his intention in using PullAudioCallback is that the delegate is supposed to wait for count bytes to be copied to the data buffer which is an IntPtr before it plays these bytes. What exactly is IntPtr...is it a pointer to a byte array? What would the contents of "someAudioCallBack(IntPtr data, int count" be? Thanks again!
crushinghellhammer wrote: What exactly is IntPtr IntPtr Structure[^] - Nick Parker
My Blog | My Articles