使用 NAudio 库在 C# 中控制播放速度的问题

问题描述 投票:0回答:1

我正在尝试使用以下代码通过 NAudio 库控制音频对象的播放速度,但遇到了一些问题。 如何在不遇到异常和错误的情况下改变媒体播放对象的播放速度,提前致谢。

using NAudio.Wave;
using NAudio.Wave.SampleProviders;
 namespace \_2
 {
 
 public partial class Form1 : Form
    {
       private WaveOutEvent outputDevice;
       private AudioFileReader audioFile;
       private VarispeedSampleProvider speedControl;
       public Form1()
      {
           InitializeComponent();
      }

       private void Form1_Load(object sender, EventArgs e)
         {
           outputDevice?.Dispose();
            audioFile?.Dispose();
         }

         private void Button1_Click(object sender, EventArgs e)
         {
             OpenFileDialog openFileDialog = new OpenFileDialog();
             if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFilePath.Text = openFileDialog.FileName;
            }
         }
 
       private void Button2_Click(object sender, EventArgs e)
         {
          if (outputDevice == null)
            {
                outputDevice = new WaveOutEvent();
               outputDevice.PlaybackStopped += OnPlaybackStopped;
             }
            if (audioFile == null)
           {
                 audioFile = new AudioFileReader(txtFilePath.Text);               outputDevice.Init(audioFile);
           }            outputDevice.Play();        }
       private void OnPlaybackStopped(object sender, StoppedEventArgs args)
        {
            outputDevice.Dispose();             outputDevice = null;
            audioFile.Dispose();
           audioFile = null;
        }
       private void Button3_Click(object sender, EventArgs e)
       {
            outputDevice?.Stop();
       }

       private void TrackBar1_Scroll(object sender, EventArgs e)        {
           if (speedControl != null)
           {
            speedControl.PlaybackRate = trackBar1.Value / 10f;
            }
       }
   }
     
 }
c# naudio playback trackbar
1个回答
0
投票

您的代码没有显示您如何在

Init
上调用
outputDevice
。但是
AudioFileReader
需要传递到
VarispeedSampleProvider
然后需要传递到
Init
.

© www.soinside.com 2019 - 2024. All rights reserved.