からオブジェクトのプロパティを変更するときにように私は、私は私のアプリのMainPage
でインスタンス化PlaybackModel
クラスを持っている:間違ったスレッドの例外イベントのデリゲート
if(PlaybackModel.Current == null) new PlaybackModel();
は私が使用するオブジェクトへの参照を格納するためPlaybackModel
を使用私のアプリで。また、大多数のページのViewModelとしても機能します。私はメディアプレーヤーとキューで何が起こっているかを追跡するために、いくつかのイベントをサブスクライブしていPlaybackModel
のインスペクタで
public MediaPlayer player { get; set; }
public MediaPlaybackState playbackState { get; set; }
public RepeatMode repeatMode { get; set; }
public MediaPlaybackList queue { get; set; }
[AlsoNotifyFor("currentIndex", "currentTrack")]
public MediaPlaybackItem currentItem { get; set; }
public ObservableCollection<PlaybackItemViewModel> tracks { get; set; }
public int currentIndex {
get {
return (int)queue.CurrentItemIndex;
} set {
System.Diagnostics.Debug.WriteLine("CurrentIndex Setter called! Value: "+value);
if (value >= 0 && queue.Items.Count() > value && value != queue.CurrentItemIndex)
queue.MoveTo((uint)value);
}
}
public PlaybackItemViewModel currentTrack { get { if (currentIndex == -1) return null; if (tracks.Count() > currentIndex) return tracks[currentIndex]; else return null; } }
private TimeSpan _currentPosition;
public TimeSpan currentPosition { get { return _currentPosition; } set { _currentPosition = value; player.PlaybackSession.Position = _currentPosition; } }
public TimeSpan currentDuration { get; set; }
public Library library { get; set; }
public LastfmClient lastFm { get; set; }
をので、私は、後にそれを表示することができます:それは、とりわけ、これらのプロパティを定義しますUI。例:
player.PlaybackSession.PlaybackStateChanged += (o, ea) =>
{
playbackState = o.PlaybackState;
};
これは機能しません。 WRONG_THREAD
例外がスローされます。これらの他のすべてのイベントでも同じです。また、await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync()
メソッドを使用してUIスレッド上でコードを実行しようとしましたが、理解しているとおり、アプリがバックグラウンド再生モードになっているときは機能しません。バックグラウンド再生をサポートするために、どうやって他の方法でやりますか?今の
私はItemChanged
イベントのためにこれを使用することにしました:
queue.CurrentItemChanged += async (o, ea) =>
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>
{
var pos = currentPosition.Duration();
var bg = App.Ref.IsBackground;
Task<ScrobbleResponse> res = null;
currentPosition = TimeSpan.Zero;
currentItem = o.CurrentItem;
if (ea.OldItem != null)
{
var vm = ea.OldItem.Source.CustomProperties["vm"] as PlaybackItemViewModel;
if (pos.TotalSeconds >= ea.OldItem.Source.Duration.Value.TotalSeconds * 0.9)
{
res = lastFm.Scrobbler.ScrobbleAsync(new Scrobble(vm.Artist, vm.Album, vm.Title, DateTimeOffset.Now));
}
}
if (currentItem != null)
currentDuration = o.CurrentItem.Source.Duration.Value;
else currentDuration = TimeSpan.Zero;
});
};
はそれを行うための他の方法はありますか?
ありがとうございます!
心配しないで、私は問題を理解しました。 – Reynevan