A-Jay Bee
A-Jay Bee

Reputation: 33

C# NAudio library, can't list codecs in combo box

I'm trying to list all the codecs possible for audio compression in a combo box. NAudio have source code and a demo application which im trying to emulate in my form application. If anyone can help me, their source code is here.

So they have about 7 other .cs files in their project which resembles the codecs they use so I copied them all into my project. I then copied all the (from what I see) relevant code for listing them in a combo box. No errors so far or anything so I run it and it stops as the value of each codec seems to be null (posted below the code).

Here is my code so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using NAudio.Wave;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.ComponentModel.Composition;
using NAudioDemo.NetworkChatDemo;

namespace NAudio_VoIP_UDP
{
public partial class Form1 : Form
{
    [ImportMany(typeof(INetworkChatCodec))]
    public IEnumerable<INetworkChatCodec> Codecs { get; set; }

    public Form1()
    {
        InitializeComponent();
        PopulateCodecsCombo(Codecs);
    }

    private void PopulateCodecsCombo(IEnumerable<INetworkChatCodec> codecs)
    {
        var sorted = from codec in codecs
                     where codec.IsAvailable
                     orderby codec.BitsPerSecond ascending
                     select codec;

        foreach (var codec in sorted)
        {
            string bitRate = codec.BitsPerSecond == -1 ? "VBR" : String.Format("{0:0.#}kbps", codec.BitsPerSecond / 1000.0);
            string text = String.Format("{0} ({1})", codec.Name, bitRate);
            comboBoxCodecs.Items.Add(new CodecComboItem { Text = text, Codec = codec });
        }
        comboBoxCodecs.SelectedIndex = 0;
    }

    class CodecComboItem
    {
        public string Text { get; set; }
        public INetworkChatCodec Codec { get; set; }
        public override string ToString()
        {
            return Text;
        }
    }
}
}

but then when i run the code, it stops here:

            var sorted = from codec in codecs
                     where codec.IsAvailable
                     orderby codec.BitsPerSecond ascending
                     select codec;

as the value cannot be null.

Below is an image of my solution explorer where you can see all the codecs I added and what should be shown in the combo box:

solution explorer

Upvotes: 0

Views: 404

Answers (1)

Michael Edenfield
Michael Edenfield

Reputation: 28338

The code you copied is using the Microsoft Extensibility Framework, which is why you have [ImportMany(typeof(INetworkChatCodec))] at the top. If you also copied those codec files from NAudio, they all include something like [Export(typeof(INetworkChatCodec))] in them.

MEF will then scan your program for all instances of INetworkChatCodec, create an instance of them, and store the list in Codecs. But you need to initialize it.

The code to initialize it is found in the startup code for that demo, but it's pretty standard MEF stuff:

var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
var exportFactoryProvider = new ExportFactoryProvider();
var container = new CompositionContainer(catalog, exportFactoryProvider);
exportFactoryProvider.SourceProvider = container;

Note, though, that this demo was written for .NET 3.5; it includes local references to some MEF bits (they are in the Lib\MEF folder), which you'll need in order to use ExportFactoryProvider.

IIRC, in .NET 4.5, the System.ComponentModel.Composition library is built in, but you would use ExportFactory<T> instead of ExportFactoryProvider to do an ImportMany

Upvotes: 1

Related Questions