0

我正在开发两个利用 iTunes COM 的 C# winform 和 WPF 应用程序,并注意到一些访问 PlayerPositionMS 属性的特殊行为,该属性是播放器的位置,以毫秒为单位。PlayerPositionMS 值的精度取决于 iTunes 应用程序是否具有焦点。我的 2 个应用程序的目标是最准确地更新玩家位置。任何援助将不胜感激。

下面是一些示例代码来说明我的观点。这是一个带有单个标签和计时器的 winforms 应用程序。计时器间隔默认为 100 毫秒,tick 方法使用 PlayerPositionMS 属性值更新标签文本。当 iTunes 获得焦点时,标签会按预期更新。但是当 winforms 应用程序或其他任何应用程序具有焦点时,标签大约每秒更新一次。winforms 和 WPF 上的行为是相同的。

  • iTunes 版本:12.9.0.167
  • iTunesLib 版本:1.13

表格截图:

在此处输入图像描述

表格代码:

namespace Test
{
    partial class Form_Test
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label_Position = new System.Windows.Forms.Label();
            this.timer_Test = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // label_Position
            // 
            this.label_Position.AutoSize = true;
            this.label_Position.Location = new System.Drawing.Point(12, 9);
            this.label_Position.Name = "label_Position";
            this.label_Position.Size = new System.Drawing.Size(16, 13);
            this.label_Position.TabIndex = 0;
            this.label_Position.Text = "---";
            // 
            // timer_Test
            // 
            this.timer_Test.Enabled = true;
            this.timer_Test.Tick += new System.EventHandler(this.UpdateLabel);
            // 
            // Form_Test
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(205, 33);
            this.Controls.Add(this.label_Position);
            this.Name = "Form_Test";
            this.Text = "Test Form";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label_Position;
        private System.Windows.Forms.Timer timer_Test;
    }
}

后面的代码:

using iTunesLib;
using System;
using System.Windows.Forms;

namespace Test
{
    public partial class Form_Test : Form
    {
        private static iTunesApp _itunes = new iTunesLib.iTunesApp();

        public Form_Test()
        {
            InitializeComponent();
        }

        private void UpdateLabel(object sender, EventArgs e)
        {
            if (_itunes.CurrentTrack != null && _itunes.PlayerState == ITPlayerState.ITPlayerStatePlaying)
                label_Position.Text = _itunes.PlayerPositionMS.ToString();
        }
    }
}
4

1 回答 1

0

在访问 PlayerPositionMS 属性之前调用 Resume 方法似乎可以解决此问题:

private void UpdateLabel(object sender, EventArgs e)
{
    if (_itunes.CurrentTrack != null && _itunes.PlayerState == ITPlayerState.ITPlayerStatePlaying)
    {
        _itunes.Resume();
        label_Position.Text = _itunes.PlayerPositionMS.ToString();
    }
}
于 2018-10-19T01:14:35.847 回答