C# BroadCast Mp3 File To ShoutCast Server

994 views Asked by At

Im trying to Make a radio Like Auto Dj to Play List Of Mp3 Files in series Like What Happen In Radio. I tried a lot of work around but finally i thought of sending mp3 files to shoutcast server and play the output of that server my problem is i don't how to do that i have tried bass.radio to use bass.net and that's my code

    private int _recHandle;
    private BroadCast _broadCast;
    EncoderLAME l;
    IStreamingServer server = null;

        // Init Bass
        Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT,IntPtr.Zero);
        // create the stream
        int _stream = Bass.BASS_StreamCreateFile("1.mp3", 0, 0,
              BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_PRESCAN);
       l= new EncoderLAME(_stream);
        l.InputFile = null;    //STDIN
        l.OutputFile = null;

        l.Start(null, IntPtr.Zero, false);
        // decode the stream (if not using a decoding channel, simply call "Bass.BASS_ChannelPlay" here)
        byte[] encBuffer = new byte[65536]; // our dummy encoder buffer
        while (Bass.BASS_ChannelIsActive(_stream) == BASSActive.BASS_ACTIVE_PLAYING)
        {
            // getting sample data will automatically feed the encoder
            int len = Bass.BASS_ChannelGetData(_stream, encBuffer, encBuffer.Length);
        }

        //l.Stop();  // finish
       //Bass.BASS_StreamFree(_stream);
        //Server
        SHOUTcast shoutcast = new SHOUTcast(l);
        shoutcast.ServerAddress = "50.22.219.37";
        shoutcast.ServerPort = 12904;
        shoutcast.Password = "01008209907";
        shoutcast.PublicFlag = true;
        shoutcast.Genre = "Hörspiel";
        shoutcast.StationName = "Kravis Server";
        shoutcast.Url = "";
        shoutcast.Aim = "";
        shoutcast.Icq = "";
        shoutcast.Irc = "";
        server = shoutcast;

        server.SongTitle = "BASS.NET";

        // disconnect, if connected
        if (_broadCast != null && _broadCast.IsConnected)
        {
            _broadCast.Disconnect();
        }
        _broadCast = null;
         GC.Collect();
        _broadCast = new BroadCast(server);
        _broadCast.Notification += OnBroadCast_Notification;
        _broadCast.AutoReconnect = true;
        _broadCast.ReconnectTimeout = 5;
        _broadCast.AutoConnect();

but i don't get my File Streamed to streamed to the server even the _broadCast Is Connected. so if any solution of code or any other thing i can do.

1

There are 1 answers

2
Brad On

I haven't used BASS in many years, so I can't give you specific advice on the code you have there. But, I wanted to give you the gist of the process of what you need to do... it might help you get started.

As your file is in MP3, it is possible to send it directly to the server and hear it on the receiving end. However, there are a few problems with that. The first is rate control. If you simply transmit the file data, you'll send say 5 minutes of data in perhaps a 10 second time period. This will eventually cause failures as the clients aren't going to buffer much data, and they will disconnect. Another problem is that your MP3 files often have extra data in them in the form of ID3 tags. Some players will ignore this, others won't. Finally, some of your files might be in different sample rates than others, so even if you rate limit your sending, the players will break when they hit a file in a different sample rate.

What needs to happen is the generation of a fresh stream. The pipeline looks something like this:

[Source File] -> [Codec] -> [Raw PCM Audio] -> [Codec] -> [MP3 Stream] -> [SHOUTcast Server] -> [Clients]

Additionally, that raw PCM audio step needs to run in at a realtime rate. While your computer can definitely decode and encode faster than realtime, it needs to be ran at realtime so that the players can listen in realtime.