Example #1
0
        public AudioPlayer()
        {
            // These environment variables are necessary to locate GStreamer libraries, and to stop it from loading
            // wrong libraries installed elsewhere on the system.
            var directoryName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            if (directoryName != null)
            {
                string apppath = directoryName.Remove(0, 6);
                string pluginPath = apppath + @"\gstreamer\bin\plugins";
                Environment.SetEnvironmentVariable("GST_PLUGIN_PATH", pluginPath);
                Environment.SetEnvironmentVariable("GST_PLUGIN_SYSTEM_PATH", "");
                string pathVariable = @"C:\Windows;" + apppath + @"\gstreamer\lib;"
                                      + apppath + @"\gstreamer\bin;" + Environment.GetEnvironmentVariable("PATH");
                Environment.SetEnvironmentVariable("PATH", pathVariable);
                Environment.SetEnvironmentVariable("GST_REGISTRY", apppath + @"\gstreamer\bin\registry.bin");

                // These are for saving debug information.
                Environment.SetEnvironmentVariable("GST_DEBUG", "*:3");
                Environment.SetEnvironmentVariable("GST_DEBUG_FILE", "GstreamerLog.txt");
                Environment.SetEnvironmentVariable("GST_DEBUG_DUMP_DOT_DIR", apppath);
            }

            Application.Init();

            _playBin = new PlayBin2("_playBin");
            Element audioSink = SelectAudioSink();
            _playBin.AudioSink = audioSink;
            _playBin.Bus.AddWatch(OnBusMessage);
            // TODO: implement gapless playback (wait for gstreamer fix?)
            /*_playBin.AboutToFinish += (o, args) =>
                {
                    if (_currentTrackIndex + 1 >= _trackArray.Length)
                        return;

                    OnTrackChanged(new TrackChangedHandlerArgs
                        {
                            NewTrack = _trackArray[_currentTrackIndex + 1],
                            OldTrack = _trackArray[_currentTrackIndex]
                        });
                    _playBin.Uri = PathStringToUri(_trackArray[++_currentTrackIndex].Path);
                };*/
            _trackArray = new Track[0];
            _playBin.SetState(State.Ready);
        }
        public void PlayAudioStream(string url)
        {
            if (_currentUrl == url && _playBin != null) {
                _playBin.SetState(State.Playing);
                return;
            }

            _currentUrl = url;
            if (_playBin != null) {
                _playBin.SetState(Gst.State.Null);
                _playBin.SetVolume(StreamVolumeFormat.Linear, _initialVolume);
            }

            url = String.Format("{0}?client_id={1}", url, SoundCloudRestClient.API_CLIENT_ID);
            Console.WriteLine(url);

            _playBin = ElementFactory.Make ("playbin2", "play") as PlayBin2;
            _playBin.Uri = url;

            _playBin.Bus.AddWatch ( new BusFunc( (bus, message) => {

                switch (message.Type) {
              		case Gst.MessageType.Error:
                        Enum err;
                        string msg;
                        message.ParseError (out err, out msg);
                        Console.WriteLine ("Gstreamer error: {0}", msg);
                        //loop.Quit ();
                        break;
              		case Gst.MessageType.Eos:
               			Console.WriteLine ("Stream finished.");

                        _playBin.SetState (Gst.State.Null);
                        if (TrackEnded != null)
                            TrackEnded (this, new EventArgs());

                        break;
                }

                return true;
             }));

            // Play the stream
            if (_playBin.CurrentState != State.Paused)
                _playBin.SetState(State.Playing);

            // Start rendering the waveform
            StartTimecodeUpdate();
        }