为什么我不能从单独的对象播放MP3文件?

时间:2016-06-26 01:16:35

标签: c# wpf class openfiledialog

WIP MP3播放器

评论的代码不起作用,但下面的当前未注释的代码可以。我可以打开对话框窗口,但在选择mp3文件后,它无法播放。未注释的代码会播放mp3文件。

“ÖffnenderDatei”地区的问题。

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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您在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,但我认为你可能假设继承意味着它将继承该对象。这不是真的,类继承只是继承了结构,因此属于MainWindow的所有变量(例如mp和labelsong)都不属于您在MainWindow中创建的Offnen实例。

这可能超出了问题的范围,但我建议你考虑让OffnenDerDatei成为属于MainWindow的函数。否则,正如你现在所做的那样,Offnen从MainWindow继承是没有意义的。