W playerze nie widać ile zostało do końca piosenki

0

Witam jest to mój pierwszy projekt w WPF i nie wiem czemu jak odtwarzam piosenkę to nie widzę czasu ile dana piosenka trwa. Program zwraca mi że w linijce 54 mam problem.

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;
using System.Windows.Threading;

namespace PlayerSound
{
    /// <summary>
    /// Logika interakcji dla klasy MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MediaPlayer mediaPlayer = new MediaPlayer();
        public MainWindow()
        {
            InitializeComponent();

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += timer_Tick;
            timer.Start();
        }

        private void list_Click(object sender, RoutedEventArgs e)
        {

        }

        private void openFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "MP3 files (*.mp3)|*.mp3| All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() == true )
            {
                mediaPlayer.Open(new Uri(openFileDialog.FileName));
                mediaPlayer.Play();
            }
        }
        void timer_Tick(object sender, EventArgs e)
        {
            if (mediaPlayer.Source != null)
                Status.Content = String.Format("{0} / {1}", mediaPlayer.Position.ToString(@"mm\:ss"), mediaPlayer.NaturalDuration.TimeSpan.ToString(@"mm\:ss"));
            else
                Status.Content = "No file selected...";
        }

        private void play_Click(object sender, RoutedEventArgs e)
        {
            mediaPlayer.Play();
        }

        private void stop_Click(object sender, RoutedEventArgs e)
        {
            mediaPlayer.Stop();
        }

        private void pause_Click(object sender, RoutedEventArgs e)
        {
            mediaPlayer.Pause();
        }
    }
}
0

Dlaczego startujesz timer w konstruktorze zamiast po wciśnięciu play?

1

Ale to chyba jest obojętne gdzie wystartuje to sprawdzanie, ważne by instancja Timer-a żyła jakoś tak dłużej niż tylko w trakcie wywoływania konstruktora.

0

O cholera, tego akurat nie zauważyłem. Prawda.
Rozwinięcie tematu dla pytacza: Tworzysz timer w konstruktorze. I go tam startujesz. Problemem jest to, że także ten timer deklarujesz w konstruktorze. W efekcie po zakończeniu konstruktora, Timer jest niszczony. Więc nie działa.

0

Powiem tak to jest mój (wymuszony projekt ). Po prostu taki program dostałem na studiach. Dla mnie to jest trochę dziwne (uczę się C++). Z programem ma dziwny problem bo jak puszcza na nim pewne mp3 to się zawiesza a jak inne to działa. W ogóle tego nie rozumiem dlaczego tak się dzieje.

ten kod mam stąd http://www.wpf-tutorial.com/audio-video/playing-audio/ drugi program

0

MediaPlayer tak działa i już. Jak chciałbyś mieć naprawdę dobry odtwarzacz, co odtworzy Ci dużo więcej, to zainteresuj się biblioteką BASS.

1 użytkowników online, w tym zalogowanych: 0, gości: 1