-5

WIP MP3-Player

The commented code doesn't work, but the currently uncommented code below does. I can open the dialog window, but after selecting the mp3-file, it doesn't play. The uncommented code does play the mp3-file.

Problem at "Öffnen der Datei" region.

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;
            }
        }
    }
}
Lawrence Johnson
  • 3,924
  • 2
  • 17
  • 30
YEA1903
  • 57
  • 8

1 Answers1

0

The code you are writing in the Offnen.cs file isn't doing anything with the file because the variable "mp" is local to the object o (Offnen). Perhaps something like this is what you are looking for:

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

I see that you have Offnen inheriting from MainWindow, but I think you might be assuming that inheritance means it will inherit the object. This is not true, class inheritance simply inherits the structure, so all of the variables (such as mp and labelsong) that belong to MainWindow will not belong to an instance of Offnen that you create within MainWindow.

It's probably beyond the scope of the question, but I would recommend you consider making the OffnenDerDatei a function that belongs to MainWindow. Otherwise, as you have it now, there is no point to Offnen inheriting from MainWindow.

Lawrence Johnson
  • 3,924
  • 2
  • 17
  • 30