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方法中,但它给出了一个错误。
希望我的问题不只是我真的很傻,也不是一个转帖。
提前感谢您的帮助!
发布于 2021-10-27 09:40:16
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用于按钮更改其图像。
https://stackoverflow.com/questions/69728683
复制相似问题