简体   繁体   中英

UWP Metronome App - MediaPlayerElement transport controls and performance

I have been decided to create a Metronome App. So, i am a "beginner" and i made this project with the purpose to learn more about OOP and other things. My application Works pretty well, but, i still have some problems (little problems).

I have used the MediaPlayerElement to play the sound tick. I do not want that the Media transport controls appear during execution, for this, i wrote

mediaPlayer.MediaPlayer.SystemMediaTransportControls.IsEnabled = false;

but the media transport controls still enabled.

The other issue is that the application Works pretty in PC (maybe by has more processing capability), but in smartphone, ah, doesn't work well! When the BPM is higher (greater than 190 or 200) the application doesn't play the sound correctly, lagging almost all the time. Code below:

PLAYER SOURCE

class Player
{
    MediaPlayerElement mediaPlayer = new MediaPlayerElement();

    public void Init()
    {
        try
        {
            Uri src = new Uri("ms-appx:///Assets/beep.wav");
            mediaPlayer.Source = MediaSource.CreateFromUri(src);
            mediaPlayer.MediaPlayer.SystemMediaTransportControls.IsEnabled = false;
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Error!" + e);
        }
    }

    public void Play()
    {
        mediaPlayer.MediaPlayer.Play();
    }

    public Player()
    {
        Init();
    }
}

METRONOME SOURCE

class Metronome
{
    private Player player = new Player();


    private DispatcherTimer dispTimer = new DispatcherTimer();
    private double interval;

    public int Tempo { get; set; }  
    public bool IsActive { get; set; }  //returns the state

    private void Init()
    {
        interval = (double)60 / Tempo - 0.02;
        dispTimer.Tick += DispTimer_Tick;
        dispTimer.Interval = TimeSpan.FromSeconds(interval);
    }

    private void DispTimer_Tick(object sender, object e)
    {
        player.Play();
    }

    public void Start()
    {
        Init();
        dispTimer.Start();
        IsActive = true;
    }

    public void Stop()
    {
        dispTimer.Stop();
        IsActive = false;
    }
}

I have a slider that is associated with Tempo property and when the user press the start button the metronomo plays the sound. This Works like a timer that when hit the specified value it plays the sound.

In computer (Core I3) this application uses the maximium of 1.0% of CPU, i don't know the CPU usage in Smartphone (Lumia 535).

** My CPU Supports VT-X, but my Chipset not, so, i make what i can! :p

Thanks for the help! :)

Instead of MediaPlayer, you can use a BackgroundMediaPlayer.

Uri src = new Uri("ms-appx:///Assets/beep.wav");
BackgroundMediaPlayer.Current.SetUriSource(src);

Also... Every time you Init() the metronome, you are adding an additional event handler. I'm assuming that your are not creating a new Metronome every time This means that when you click start, then stop, then start again - each tick is calling player.Play() twice. This is also a good way to get memory leaks. I would unsubscribe from the event on Stop(). More on that here (MSDN).

dispTimer.Tick -= DispTimer_Tick;

Good luck with your learning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM