酢ろぐ!

カレーが嫌いなスマートフォンアプリプログラマのブログ。

Windows Phone Developer Tools 7.1から追加されたテンプレートプロジェクト

Visual Stuido 2010 Express for Windows Phoneで、New Projectを選択したときに表示されるテンプレートプロジェクトに追加がありました。殆どがバックグランド・エージェントと呼ばれていた機能になります。

  • Windows Phone 3D Graphics Application
    • SilverlightアプリケーションからXNAのコンテンツを呼び出すことができます。ゲームループからXNAのプロジェクトを呼び出しているXAMLが含まれています。SilverlightベースのXNAとの相互運用用のテンプレートプロジェクトです。
  • Windows Phone Audio Playback Agent
    • AudioPlayerAgentをベースにしたUIの無いテンプレートプロジェクトです。カスタムイベントを処理するためにBackgroundAudioPlayerに接続されるクラスライブラリのこのようなものをが、検討してください。
  • Windows Phone Audio Streaming Agent
    • Audio Playback Agentと同様に、このプロジェクトはバックグランド・プレイヤーとストリーミング再生の仕組みを実装するためのテンプレートプロジェクトです。
  • Windows Phone Task Scheduler Agent
    • ScheduledTaskAgentをベースにしたUIの無いテンプレートプロジェクトです。定期的に通知されるイベントごとに処理を行うことが出来ます。

バックグランド・エージェントについては以下の記事が詳しいです(英語しかありませんが……)。

テンプレートプロジェクトの中身

それぞれがどんなソースコードを出力されるかをみてみましょう。超意訳付きです。時間を作ってサンプルプロジェクトを作っていきたいと思います。

Windows Phone Audio Playback Agent

音声の再生用のエージェント。

using System;
using Microsoft.Phone.BackgroundAudio;

namespace AudioPlaybackAgent1
{
    public class AudioPlayer : AudioPlayerAgent
    {
        /// <summary>
        /// エラー状態を除いて再生状態に変更があったときに呼び出されます
        /// </summary>
        /// <param name="player">The BackgroundAudioPlayer</param>
        /// <param name="track">The track playing at the time the playstate changed</param>
        /// <param name="playState">The new playstate of the player</param>
        /// <remarks>
        /// Play State changes cannot be cancelled. They are raised even if the application
        /// caused the state change itself, assuming the application has opted-in to the callback
        /// </remarks>
        protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
        {
            base.OnPlayStateChanged(player, track, playState);

            //TODO: 再生状態の変更を処理するコードを追加します。

            NotifyComplete();
        }


        /// <summary>
        /// アプリケーションからエージェントに対してアクションとUIを提供するときに
        /// システムを通じて、ユーザーが行ったアクションの通知を受けると呼び出されます。
        /// </summary>
        /// <param name="player">The BackgroundAudioPlayer</param>
        /// <param name="track">The track playing at the time of the user action</param>
        /// <param name="action">The action the user has requested</param>
        /// <param name="param">The data associated with the requested action.
        /// In the current version this parameter is only for use with the Seek action,
        /// to indicate the requested position of an audio track</param>
        /// <remarks>
        /// ユーザーのアクションが自動的にシステム状態の変更をしてはいけません。
        /// エージェントには、ユーザのアクションを実施するための責任があります。
        /// </remarks>
        protected override void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
        {
            base.OnUserAction(player, track, action, param);

            //TODO: UIを提供するアプリケーションを介して伝わったユーザのアクションを処理するコードを書きます

            NotifyComplete();
        }

        /// <summary>
        /// 再生中にエラーが発生したときに呼び出されます
        /// </summary>
        /// <param name="player">The BackgroundAudioPlayer</param>
        /// <param name="track">The track that had the error</param>
        /// <param name="error">The error that occured</param>
        /// <param name="isFatal">再生が続行できない場合はtrueを返します。trueの場合にはトラックの再生が停止します。</param>
        /// <remarks>
        /// このメソッドは、すべての場合に呼び出されることが保証されていません。
        /// たとえば、バックグラウンドエージェント自体が未処理の例外を持っている場合は、
        /// 独自のエラーを処理するコールバックされません。
        /// </remarks>
        protected override void OnError(BackgroundAudioPlayer player, AudioTrack track, Exception error, bool isFatal)
        {
            base.OnError(player, track, error, isFatal);

            //TODO: エラーを処理するコードをここに書きます

            NotifyComplete();
        }

        /// <summary>
        /// エージェントの要求をキャンセルされたときに呼び出されます
        /// </summary>
        protected override void OnCancel()
        {
            base.OnCancel();
            NotifyComplete();
        }
    }
}
Windows Phone Audio Streaming Agent

ストリーミング再生用のエージェント。

using Microsoft.Phone.BackgroundAudio;

namespace AudioStreamAgent1
{
    /// <summary>
    /// A background agent that performs per-track streaming for playback
    /// </summary>
    public class AudioTrackStreamer : AudioStreamingAgent
    {
        /// <summary>
        /// Called when a new track requires audio decoding
        /// (typically because it is about to start playing)
        /// </summary>
        /// <param name="track">
        /// The track that needs audio streaming
        /// </param>
        /// <param name="streamer">
        /// The AudioStreamer object to which a MediaStreamSource should be
        /// attached to commence playback
        /// </param>
        /// <remarks>
        /// To invoke this method for a track set the Source parameter of the AudioTrack to null
        /// before setting  into the Track property of the BackgroundAudioPlayer instance
        /// property set to true;
        /// otherwise it is assumed that the system will perform all streaming
        /// and decoding
        /// </remarks>
        protected override void OnBeginStreaming(AudioTrack track, AudioStreamer streamer)
        {
            //TODO: Set the SetSource property of streamer to a MSS source

            NotifyComplete();
        }

        /// <summary>
        /// Called when the agent request is getting cancelled
        /// </summary>
        protected override void OnCancel()
        {
            base.OnCancel();
            NotifyComplete();
        }
    }
}
Windows Phone Task Scheduler Agent

Location Serviceなどオーディオにまつわるもの以外を処理する為のスケジューラータスク・エージェントです。Windows Phone 7 Deep Diveの際に高橋忍さんが言っていた「バックグランド・エージェント」はおそらくこれ。

using Microsoft.Phone.Scheduler;

namespace SchedulerAgent1
{
    public class TaskScheduler : ScheduledTaskAgent
    {
        /// <summary>
        /// Agent that runs a scheduled task
        /// </summary>
        /// <param name="task">
        /// The invoked task
        /// </param>
        /// <remarks>
        /// 定期的にタスクが呼び出されたときに、このメソッドが呼び出されます
        /// </remarks>
        protected override void OnInvoke(ScheduledTask task)
        {
            //TODO: バックグラウンドで実行するコードをここに書きます

            NotifyComplete();
        }

        /// <summary>
        /// エージェントの要求をキャンセルされたときに呼び出されます
        /// </summary>
        protected override void OnCancel()
        {
            base.OnCancel();

            //TODO: このタスクがキャンセルされると、必要に応じてClean Upを行います
        }
    }
}