首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的输出设备停止并重新启动文件,然后在暂停后恢复

我的输出设备停止并重新启动文件,然后在暂停后恢复
EN

Stack Overflow用户
提问于 2021-10-26 19:09:04
回答 1查看 60关注 0票数 0
代码语言:javascript
运行
复制
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音乐播放器原型。我在表单中做了一个音乐播放器,所有NAudio的东西都工作得很好,但是在WPF上我的播放/暂停按钮不能正常工作。暂停部分重新启动播放器,而不是暂停它。我试着从这个函数中提取audioFile初始化,并将其放入wpf_load方法中,但它给出了一个错误。

希望我的问题不只是我真的很傻,也不是一个转帖。

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2021-10-27 09:40:16

代码语言:javascript
运行
复制
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用于按钮更改其图像。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69728683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档