为什么我不能从单独的对象播放 MP3 文件?
Why can't I play an MP3 file from a separate object?
WIP MP3 播放器
注释的代码不起作用,但下面当前未注释的代码起作用。我可以打开对话框 window,但在选择 mp3 文件后,它无法播放。未注释的代码确实播放了 mp3 文件。
“Öffnen der Datei”区域出现问题。
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
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 Microsoft.Win32;
namespace Music_Player
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public MediaPlayer mp = new MediaPlayer();
#region Öffnen der Datei
private void menuOffnen_Click(object sender, RoutedEventArgs e)
{
mp.Pause();
OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = ".mp3";
dlg.Filter = "MP3 files (*.mp3)|*.mp3|M4A files (*.m4a)|*.m4a|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
mp.Open(new Uri(dlg.FileName));
labelsong.Content = dlg.SafeFileName;
}
//Offnen o = new Offnen();
//o.OffnenDerDatei();
mp.Play();
}
#endregion
#region ActionButtons
private void button_play_Click(object sender, RoutedEventArgs e)
{
mp.Play();
}
private void button_pause_Click(object sender, RoutedEventArgs e)
{
mp.Pause();
}
private void button_stop_Click(object sender, RoutedEventArgs e)
{
mp.Stop();
}
#endregion
private void slider_volume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
slidervolume.Maximum = 100;
slidervolume.Minimum = 0;
}
#region Beenden
private void menuBeenden_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
#endregion
}
}
Offnen.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Windows.Media;
namespace Music_Player
{
class Offnen : MainWindow
{
public void OffnenDerDatei()
{
MediaPlayer mp = new MediaPlayer();
OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = ".mp3";
dlg.Filter = "MP3 files (*.mp3)|*.mp3|M4A files (*.m4a)|*.m4a|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
mp.Open(new Uri(dlg.FileName));
labelsong.Content = dlg.SafeFileName;
}
}
}
}
您在 Offnen.cs 文件中编写的代码未对该文件执行任何操作,因为变量 "mp" 是对象 o (Offnen) 的本地变量。也许您正在寻找这样的东西:
MainWindow.xaml.cs
#region Öffnen der Datei
private void menuOffnen_Click(object sender, RoutedEventArgs e)
{
mp.Pause();
Offnen o = new Offnen();
o.OffnenDerDatei(mp);
mp.Play();
}
#endregion
Offnen.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Windows.Media;
namespace Music_Player
{
class Offnen : MainWindow
{
public void OffnenDerDatei(MediaPlayer mPlayer)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = ".mp3";
dlg.Filter = "MP3 files (*.mp3)|*.mp3|M4A files (*.m4a)|*.m4a|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
mPlayer.Open(new Uri(dlg.FileName));
//labelsong.Content = dlg.SafeFileName; // fyi this variable looks to be undeclared
}
}
}
}
我看到您从 MainWindow 继承了 Offnen,但我认为您可能假设继承意味着它将继承该对象。这不是真的,class 继承只是继承结构,所以所有属于 MainWindow 的变量(例如 mp 和 labelsong)将不属于您在 MainWindow 中创建的 Offnen 实例。
这可能超出了问题的范围,但我建议您考虑将 OffnenDerDatei 设为属于 MainWindow 的函数。否则,就像你现在拥有的那样,Offnen 从 MainWindow 继承是没有意义的。
WIP MP3 播放器
注释的代码不起作用,但下面当前未注释的代码起作用。我可以打开对话框 window,但在选择 mp3 文件后,它无法播放。未注释的代码确实播放了 mp3 文件。
“Öffnen der Datei”区域出现问题。
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
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 Microsoft.Win32;
namespace Music_Player
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public MediaPlayer mp = new MediaPlayer();
#region Öffnen der Datei
private void menuOffnen_Click(object sender, RoutedEventArgs e)
{
mp.Pause();
OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = ".mp3";
dlg.Filter = "MP3 files (*.mp3)|*.mp3|M4A files (*.m4a)|*.m4a|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
mp.Open(new Uri(dlg.FileName));
labelsong.Content = dlg.SafeFileName;
}
//Offnen o = new Offnen();
//o.OffnenDerDatei();
mp.Play();
}
#endregion
#region ActionButtons
private void button_play_Click(object sender, RoutedEventArgs e)
{
mp.Play();
}
private void button_pause_Click(object sender, RoutedEventArgs e)
{
mp.Pause();
}
private void button_stop_Click(object sender, RoutedEventArgs e)
{
mp.Stop();
}
#endregion
private void slider_volume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
slidervolume.Maximum = 100;
slidervolume.Minimum = 0;
}
#region Beenden
private void menuBeenden_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
#endregion
}
}
Offnen.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Windows.Media;
namespace Music_Player
{
class Offnen : MainWindow
{
public void OffnenDerDatei()
{
MediaPlayer mp = new MediaPlayer();
OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = ".mp3";
dlg.Filter = "MP3 files (*.mp3)|*.mp3|M4A files (*.m4a)|*.m4a|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
mp.Open(new Uri(dlg.FileName));
labelsong.Content = dlg.SafeFileName;
}
}
}
}
您在 Offnen.cs 文件中编写的代码未对该文件执行任何操作,因为变量 "mp" 是对象 o (Offnen) 的本地变量。也许您正在寻找这样的东西:
MainWindow.xaml.cs
#region Öffnen der Datei
private void menuOffnen_Click(object sender, RoutedEventArgs e)
{
mp.Pause();
Offnen o = new Offnen();
o.OffnenDerDatei(mp);
mp.Play();
}
#endregion
Offnen.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Windows.Media;
namespace Music_Player
{
class Offnen : MainWindow
{
public void OffnenDerDatei(MediaPlayer mPlayer)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = ".mp3";
dlg.Filter = "MP3 files (*.mp3)|*.mp3|M4A files (*.m4a)|*.m4a|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
mPlayer.Open(new Uri(dlg.FileName));
//labelsong.Content = dlg.SafeFileName; // fyi this variable looks to be undeclared
}
}
}
}
我看到您从 MainWindow 继承了 Offnen,但我认为您可能假设继承意味着它将继承该对象。这不是真的,class 继承只是继承结构,所以所有属于 MainWindow 的变量(例如 mp 和 labelsong)将不属于您在 MainWindow 中创建的 Offnen 实例。
这可能超出了问题的范围,但我建议您考虑将 OffnenDerDatei 设为属于 MainWindow 的函数。否则,就像你现在拥有的那样,Offnen 从 MainWindow 继承是没有意义的。