People want something to happen. And me too.
So I made the MP3 player. And it really can play MP3.
With the “MediaElement”, you can play video files too.

mp3_test.jpg (17 KB)

“MainPage.xaml” looks like this.

<UserControl x:Class="MP3MediaTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
        <StackPanel>
            <MediaElement AutoPlay="False" x:Name="myMP3" Source="Etudo.mp3" />
            <TextBlock FontSize="20" x:Name="myStatus" />
            <StackPanel Orientation="Horizontal">
                <Button x:Name="myPlayButton" Width="100" Content="Play" Click="OnPlay" />
                <Button x:Name="myPauseButton" Width="100" Content="Pause" Click="OnPause" />
                <Button x:Name="myStopButton" Width="100" Content="Stop" Click="OnStop" />
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>

And “MainPage.xaml.cs” looks like this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MP3MediaTest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.myMP3.CurrentStateChanged += new RoutedEventHandler(OnCurrentStateChanged);
        }

        void OnCurrentStateChanged(object sender, RoutedEventArgs e)
        {
            bool isPlaying = (this.myMP3.CurrentState == MediaElementState.Playing);
            this.myPauseButton.IsEnabled = isPlaying && this.myMP3.CanPause;
            this.myPlayButton.IsEnabled = !isPlaying;
            this.myStatus.Text = this.myMP3.CurrentState.ToString() + " - " + this.myMP3.Source.ToString();
        }

        private void OnPlay(object sender, RoutedEventArgs e)
        {
            this.myMP3.Play();
        }

        private void OnPause(object sender, RoutedEventArgs e)
        {
            this.myMP3.Pause();
        }

        private void OnStop(object sender, RoutedEventArgs e)
        {
            this.myMP3.Stop();
        }
    }
}

Leave a Reply

*