Ejemplo n.º 1
0
        public void Run_Btn_Click(object sender, EventArgs e)
        {
            isNeedAudio = isNeedAudio_Checkbox.Checked;
            switch (run_btn.Text)
            {
            case "開始錄影 (F2)":
                write_finish = false;
                renew_path();
                isRecording  = true;
                run_btn.Text = "停止錄影 (F2)";
                f2.can_drag  = false;

                videoOption.V_l = f2.Left;
                videoOption.V_t = f2.Top;
                videoOption.V_w = f2.Width;
                videoOption.V_h = f2.Height;


                int width  = videoOption.V_w;
                int height = videoOption.V_h;
                //from https://en.code-bude.net/2013/04/17/how-to-create-video-files-in-c-from-single-images/
                /*my.file_put_contents(video_path, "");*/

                //writer.Open("C:\\temp\\a.avi", V_w, V_h);
                show_hide_f2(false);
                writer = new VideoFileWriter();
                int w            = Convert.ToInt32(Math.Ceiling(videoOption.V_w / 10.0)) * 10;
                int h            = Convert.ToInt32(Math.Ceiling(videoOption.V_h / 10.0)) * 10;
                int videoBitRate = w * h * 3 * 30;
                //int videoBitRate = 1200 * 1000;
                //int audioBitRate = 320 * 1000;
                if (isNeedAudio)
                {
                    //audio = new AudioCaptureDevice();
                    //audio.Format = Accord.Audio.SampleFormat.Format16Bit;
                    //audio.SampleRate = 22050;// Accord.DirectSound.Accord.Audio.Tools.Default.SampleRate;
                    //audio.DesiredFrameSize = 4096;
                    //audio.NewFrame += audioDevice_NewFrame;
                    //audio.DesiredFrameSize = 4096;



                    //mciSendString("open new Type waveaudio alias recsound", null, 0, IntPtr.Zero);
                    //mciSendString("set capture time format ms bitspersample 16 channels 2 samplespersec 48000 bytespersec 192000 alignment 4", null, 0, IntPtr.Zero);
                    //mciSendString("record recsound", null, 0, IntPtr.Zero);
                    //alert("gg");
                    //writer.Open(video_path, w, h, 30, VideoCodec.H264, videoBitRate, AudioCodec.MP3, audioBitRate, 44100,1);
                    //writer.Open(video_path, w, h, 30, VideoCodec.H264, videoBitRate);
                    //audio = new WasapiLoopbackCapture();

                    //audio.Initialize();
                    //audio_w = new WaveWriter(audio_path, audio.WaveFormat);

                    //setup an eventhandler to receive the recorded data
                    //audio.DataAvailable += (s, ee) =>
                    // {
                    //save the recorded audio
                    //    audio_w.Write(ee.Data, ee.Offset, ee.ByteCount);
                    // };

                    //start recording
                    //audio.Start();
                    //Console.ReadKey();
                    system_audio = new WasapiLoopbackCapture();
                    //system_audio = new WasapiLoopbackCapture();

                    //wtf.DataAvailable
                    //waveSource.WaveFormat = new WaveFormat(44100, 2);

                    system_audio.DataAvailable += new EventHandler <WaveInEventArgs>(waveSource_DataAvailable);

                    //system_audio.DataAvailable += (s, ee) =>
                    // {
                    //save the recorded audio
                    //    waveSource_DataAvailable(s, ee);
                    //audio_w.Write(ee.Data, ee.Offset, ee.ByteCount);
                    // };

                    system_audio.RecordingStopped += new EventHandler <StoppedEventArgs>(waveSource_RecordingStopped);

                    waveFile = new WaveFileWriter(audio_path, system_audio.WaveFormat);

                    system_audio.StartRecording();
                    // system_audio.StartRecording();
                }

                writer.Open(video_path, w, h, 30, VideoCodec.H264, videoBitRate);
                //

                //msc = new Thread(thread_msc);
                // msc.Start();
                timer1.Enabled = true;
                sc             = new Thread(thread_sc);
                sc.Start();



                break;

            default:
                show_hide_f2(true);
                run_btn.Text = "開始錄影 (F2)";
                f2.can_drag  = true;
                isRecording  = false;
                if (isNeedAudio)
                {
                    //mciSendString("save recsound " + audio_path, null, 0, IntPtr.Zero);
                    //mciSendString("close recsound", null, 0, IntPtr.Zero);
                    //https://github.com/accord-net/framework/issues/418
                    //byte[] bffr = File.ReadAllBytes(audio_path);
                    //writer.WriteAudioFrame()
                    //writer.WriteAudioFrame()
                    //
                    // audio.Stop();
                    // audio.Dispose();
                    //audio = null;
                    //audio_w.Dispose();
                    //audio_w = null;
                    //GC.Collect();
                    //audio.Dispose();
                    system_audio.StopRecording();
                    if (system_audio != null)
                    {
                        system_audio.Dispose();
                        system_audio = null;
                    }

                    if (waveFile != null)
                    {
                        waveFile.Dispose();
                        waveFile = null;
                    }
                    // system_audio.StopRecording();
                    //  system_audio.Dispose();
                    //my.copy(audio_path, _nt);

                    //sc.Abort();
                    //Thread.Sleep(1000);
                    //GC.Collect();

                    while (write_finish == false)
                    {
                        Thread.Sleep(10);
                    }
                    writer.Close();
                    writer = null;

                    AviManager aviManager = new AviManager(video_path, true);
                    aviManager.AddAudioStream(audio_path, 0);
                    aviManager.Close();
                    timer1.Enabled = false;

                    my.unlink(audio_path);
                }


                break;
            }
        }