【问题标题】:My output device stops and restarts the file then resuming after pause我的输出设备停止并重新启动文件,然后在暂停后恢复
【发布时间】:2021-12-12 03:44:51
【问题描述】:
private void PlayPause_Click(object sender, RoutedEventArgs e)
{
   audioFile = new AudioFileReader($@"D:\kondio.mp3"); tru1 = true;

   if (!tru)
   {
       PlayImage.Source = BitmapToImageSource(Resource1.pause);
       tru = true;
       outputDevice = new WaveOutEvent();
       if (outputDevice != null && audioFile!=null)
       {
           outputDevice.Init(audioFile);
           dispatcherTimer.IsEnabled = true;
           
           outputDevice?.Play();
           dispatcherTimer.Start();
       }
   }
   else
   {
       PlayImage.Source = BitmapToImageSource(Resource1.play);
       tru = false;
       outputDevice?.Pause();
       dispatcherTimer.IsEnabled = false;
   }

我对 wpf 和一般的编码非常陌生.. 我的问题是关于我的 NAudio Music 播放器原型。 我在 Forms 中做了一个音乐播放器,所有 NAudio 的东西都工作得很好,但是在 WPF 上我的播放/暂停按钮不能正常工作。 暂停部分重新启动播放器而不是暂停它。 我尝试从这个函数中取出 audioFile 初始化并将其放入 wpf_load 方法中,但它给出了一个错误。

希望我的问题不只是我真的很笨,也不是转发。

提前感谢您的帮助!

【问题讨论】:

  • 请贴出完整代码
  • 我在 cmets 萌芽阶段做过

标签: c# wpf naudio


【解决方案1】:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NAudio.Wave;
using Brushes = System.Windows.Media.Brushes;

namespace MusicPlayer
{
    public partial class MainWindow : Window
    {
        private WaveOutEvent outputDevice;
        private AudioFileReader audioFile;
        BitmapImage img = new BitmapImage();
        TimeSpan timeSpan = new TimeSpan();
        bool tru = false; 
        System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        public MainWindow()
        {
            InitializeComponent();
        }
        BitmapImage BitmapToImageSource(Bitmap bitmap)
        {
            using (MemoryStream memory = new MemoryStream())
            {
                bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
                memory.Position = 0;
                BitmapImage bitmapimage = new BitmapImage();
                bitmapimage.BeginInit();
                bitmapimage.StreamSource = memory;
                bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapimage.EndInit();

                return bitmapimage;
            }
        }
        private void MusicPlayer_Load()
        {

            dispatcherTimer.Tick += dispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
            this.SizeToContent = SizeToContent.WidthAndHeight;
            Seekbar.Maximum = audioFile.TotalTime.Seconds;
            Seekbar.Minimum = 0;
            PlayImage.Source = BitmapToImageSource(Resource1.play);
            timeSpan = audioFile.TotalTime;
        }
        private void PlayPause_Click(object sender, RoutedEventArgs e)
        {
            bool check = false;
            if (check)
            {
                audioFile = new AudioFileReader($@"D:\kondio.mp3");
                check = true;
            }
            if (!tru)
            {
                PlayImage.Source = BitmapToImageSource(Resource1.pause);
                tru = true;
                outputDevice = new WaveOutEvent();
                if (outputDevice != null)
                {
                    
                    outputDevice.Init(audioFile);
                    dispatcherTimer.IsEnabled = true;
                    
                    outputDevice?.Play();
                    dispatcherTimer.Start();
                }
            }
            else
            {
                PlayImage.Source = BitmapToImageSource(Resource1.play);
                tru = false;
                outputDevice?.Pause();
                dispatcherTimer.IsEnabled = false;
            }
 
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {

            if (Seekbar.Value != Seekbar.Maximum)
            {
                Seekbar.Value = Convert.ToInt32(audioFile.CurrentTime.TotalSeconds);
                
            }

        }

        private void Sound_Click(object sender, RoutedEventArgs e)
        {

        }

        private void SoundImage_MouseEnter(object sender, MouseEventArgs e)
        {
                SoundImage.Source = BitmapToImageSource(Resource1.sound100hover);
        }
        private void SoundImage_MouseLeave()
        {
            SoundImage.Source = BitmapToImageSource(Resource1.sound100);
            Sound.Background = Brushes.White;
        }

    }
}
            }
 
        }

PlayImage.Source 用于按钮更改其图像。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2021-11-25
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 2014-02-15
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多