Basically, I am using this sample.
http://social.msdn.microsoft.com/Forums/en-US/43295c90-43e8-4b08-8a25-958a1c3d0a0b/explanation-on-windowsuixamlmediaxamlrenderingbackgroundtask
An XAMLrenderingBackgroundTask which updates my live tile with album art, text, etc. Works great - however I can't get it to run when the song changes. I have no clue how I can trigger this task when the song changes.
I have a separate BackgroundTask that is continuously running and receives events for Song changes, etc. But I don't know of a way to trigger this XAMLrenderingBackgroundTask myself without adding triggers like System time changed, etc. It must be possible, I see other apps updating images on tiles when a song changes.
I tried implementing the XAMLrenderingBGTask with the Media BackgroundTask and attempting to update the live tile when the media changed but it crashes due to some call being marshalled from another thread which I kind of expected.
Now I am stuck. Here is my backgroundtask for media player, MediaOpened event should trigger tile change for new song, but I can't..
public sealed class BackgroundAudioTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
systemmediatransportcontrol = SystemMediaTransportControls.GetForCurrentView();
systemmediatransportcontrol.ButtonPressed += systemmediatransportcontrol_ButtonPressed;
systemmediatransportcontrol.PropertyChanged += systemmediatransportcontrol_PropertyChanged;
systemmediatransportcontrol.IsEnabled = true;
systemmediatransportcontrol.IsPauseEnabled = true;
systemmediatransportcontrol.IsPlayEnabled = true;
systemmediatransportcontrol.IsNextEnabled = true;
systemmediatransportcontrol.IsPreviousEnabled = true;
systemmediatransportcontrol.IsFastForwardEnabled = true;
systemmediatransportcontrol.IsRewindEnabled = true;
//Add handlers for MediaPlayer
BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged;
BackgroundMediaPlayer.Current.MediaOpened += MediaPlayer_MediaOpened;
BackgroundMediaPlayer.Current.MediaEnded += MediaPlayer_MediaEnded;
BackgroundMediaPlayer.Current.MediaFailed += mediaPlayer_MediaFailed;
BackgroundMediaPlayer.Current.VolumeChanged += Current_VolumeChanged;
BackgroundTaskStarted.Set();
backgroundtaskrunning = true;
}
I know it's a little bit late :D
you can use This to update tiles .
scenario 2 I think can help you .
in MediaOpened event you can update tile
Related
I want to add a media player control to SystemMediaControls. But, I have an issue here. When you pressed it, it occurs twice. Please see code below:
using Windows.Media;
using . . .
public class Main{
public static SystemMediaTransportControls systemMediaControls;
public Main(){
this.InitializeComponent();
systemMediaControls = SystemMediaTransportControls.GetForCurrentView();
int num = 0;
systemMediaControls.ButtonPressed += async (SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs e) =>
{
num++;
Debug.WriteLine($"Event number: {num}");
};
}
}
I proved it with debug logs, and the debug logs are:
Event number: 1
Event number: 1
Event number: 2
Event number: 2
Event number: 3
Event number: 3
I have no idea where's the problem is.
This is my fault. I put event on MainPage main function. The mistake is, I call MainPage more than once in the App.cs. For example:
void LaunchApp() {
new MainPage();
...
rootFrame.Navigate(typeof(MainPage));
}
So it initializes twice. That's why the event also fires twice. I have resolved my issue.
SystemMediaControls.ButtonPressed event fires twice
I have tested with official code sample here. Unfortunately, we can't reproduce this problem, Derive official tutorial, if you want to custom SystemMediaTransportControls, we need to sync playback status To MediaPlayerState at fist. and control the media player in button pressed event.
switch (mediaPlayer.PlaybackSession.PlaybackState)
{
case MediaPlaybackState.None:
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Closed;
break;
case MediaPlaybackState.Opening:
// This state is when new media is being loaded to the MediaPlayer [ie.
// Source]. For this sample the design is to maintain the previous playing/pause
// state before the new media is being loaded. So we'll leave the PlaybackStatus alone
// during loading. This keeps the system UI from flickering between displaying a "Play"
// vs "Pause" software button during the transition to a new media item.
break;
case MediaPlaybackState.Buffering:
// No updates in MediaPlaybackStatus necessary--buffering is just
// a transitional state where the system is still working to get
// media to start or to continue playing.
break;
case MediaPlaybackState.Paused:
if (mediaPlayer.PlaybackSession.Position == TimeSpan.Zero)
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
else
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Paused;
break;
case MediaPlaybackState.Playing:
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Playing;
break;
}
For more detail please refer Manual control of the System Media Transport Controls
I have a XAML based Windows 10 UWP application which uses a MediaElement to play videos. For analytics purpose, when a user plays a video, I want to track the start and end positions of a seek operation.
The only event I can find is SeekCompleted, in which I can get the end position using mediaElement.Position but I can't see any way to identify the start position. Also CurrentStateChanged event is not fired when the seek operation starts. How do I get the start position?
Code that I am using:
mediaElement.CurrentStateChanged += MediaElement_StateChanged;
mediaElement.SeekCompleted += MediaElement_SeekCompleted;
//Play the video
var mediaSource = MediaSource.CreateFromUri(new Uri(streamUrl));
var mediaPlaybackItem = new MediaPlaybackItem(mediaSource);
mediaElement.SetPlaybackSource(mediaSource);
public void MediaElement_StateChanged(object sender, RoutedEventArgs e)
{
var mediaElement = sender as MediaElement;
var state = mediaElement.CurrentState;
var position = mediaElement.Position;
}
public void MediaElement_SeekCompleted(object sender, RoutedEventArgs e)
{
var position = mediaElement.Position;
}
Both methods get called only after the seek operation is completed and so I can get only the end position. I need the start position as well.
The default transport controls do not raise any event when you start seeking. To get this you could modify them so that such an event is raised.
There's documentation on customizing the transport controls at https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/custom-transport-controls
These show how to get programmatic access to the Slider used for seeking. You could add a handler for a suitable event (perhaps DragStarting, Holding, or ManipulationStarted?) and then record the current position then.
I am writing a custom audio player in c# using the MediaPlayer class. I have implemented a scroll bar so the user can seek through a track and this is where I am having the problem.
WHen the user selects an audio track (loaded from an xml playlist) the app calculates the length in seconds of the track and sets this as the max value for the scroll bar. This all works fine except the NaturalDuration.TimeSpan property sometimes returns 0 rather than the amount. I have proved this by adding a loop that exits when NaturalDuration.HasTimeSpan is true then returns the NaturalDuration.TimeSpan value.
My question is how can I just get the NaturalDuration.TimeSpan when the NaturalDuration.HasTimeSpan is changed to true?
The correct way to do this is to handle the MediaPlayer.MediaOpened event:
mediaPlayer = new MediaPlayer();
mediaPlayer.MediaOpened += MediaPlayer_MediaOpened;
mediaPlayer.Open(new Uri(mediaFilePath, UriKind.Absolute));
...
private void MediaPlayer_MediaOpened(object sender, EventArgs e)
{
if (mediaPlayer.NaturalDuration.HasTimeSpan)
{
SliderMaximum = mediaPlayer.NaturalDuration.TimeSpan.TotalSeconds;
mediaPlayer.Play();
}
}
I want to make hovering button in my game. Because when my cursor touch the button it will go to another screen immediately. I don't like this so much. I use xna 4.0 with visual studio 2010 to make this project. (use kinect without wpf)
How to use timer in this case ? Please help me
if (Hand.contian(Button) && holdtime == targetHoldtime)
{
}
You have to manage time by yourself based in elapsed time per frame:
ft = GameTime.Elapsed.TotalSeconds; // Xna
ft= 1/30f; // 30fps
And can be done in similar way to this:
class Button {
public float Duration = 1; // One second
public Rectangle Bounds; // Button boundaries
public float Progress { get{ return Elapsed/Duration; } }
float Elapsed = 0;
public void Update(float ft) {
if (Bounds.Contains( HandPosition ))
{
if (Elapsed<Duration) {
Elapsed += ft;
if (Elapsed>Duration) {
Elapsed = Duration;
OnClick();
}
}
} else {
Elapsed = 0;
}
}
}
I would first suggest that you look through the SDK documentation and the built in KinectInteraction controls. They may provide you with what you are looking for. Most notably SDK 1.7 removed that "HoverDwell" button in favor of a "press" action, which is a more natural interaction in a gesture system. You may want to look at using that motion instead.
If you truly desire a "click on hover" type action, you can look at the code in SDK 1.6 for an example. Several examples are available online at the Kinect for Windows CodePlex repository. The specific control example you are looking for is in the "BasicInteraction-WPF" project, and is called HoverDwellButton.
The "button" is actually a ContentControl which means you can place any content in there to make it a button. It can be a simple image, or a complex Grid. It has all the hooks to fire events when the timer on your hover goes off.
There is a decent amount of complexity in this control, which is what makes it work for a wide range of applications. At the core of the interaction is a simple DispatcherTimer.
private void OnPreviewHandEnter(object sender, HandInputEventArgs args)
{
if (this.trackedHandHovers.FirstOrDefault(t => t.Hand.Equals(args.Hand)) == null)
{
// additional logic removed for answer sanity
var timer = new HandHoverTimer(DispatcherPriority.Normal, this.Dispatcher);
timer.Hand = args.Hand;
timer.Interval = TimeSpan.FromMilliseconds(Settings.Default.SelectionTime);
timer.Tick += (o, s) => { this.InvokeHoverClick(args.Hand); };
this.trackedHandHovers.Add(timer);
timer.Start();
}
args.Handled = true;
}
Notice that the Tick event is calling InvokeHoverClick, which (in part) reads as follows:
public void InvokeHoverClick(HandPosition hand)
{
// additional logic removed for answer sanity
var t = new DispatcherTimer();
t.Interval = TimeSpan.FromSeconds(0.6);
t.Tick += (o, s) =>
{
t.Stop();
var clickArgs = new HandInputEventArgs(HoverClickEvent, this, hand);
this.RaiseEvent(clickArgs);
this.IsSelected = false;
};
t.Start();
}
This now fires an event after a set amount of time. This event can be capture and acted upon to your liking.
Again, I first recommend looking at the newer interactions in SDK 1.7. If you still want a timed hover click action, check out the links above. I used the HoverDwellButton to great effect in several different areas.
My problem is that the program speaks before the form gets displayed.
Here's the load block:
/********************
* *
* Start Game *
* *
********************/
private void Battleship_Load(object sender, EventArgs e)
{
// Interface housekeeping
lblStatus.Font = new Font("HandelGotDLig", 18);
// fill computer board
game.buildBoards();
human = game.Human;
computer = game.Computer;
shot = game.Initialize(ref human, ref shot);
//set up displays
gbComputerHistory.Visible = false;
gbHumanHistory.Visible = false;
gbShot.Visible = false;
lblStatus.Text = "Choose who starts";
gbStart.Visible = true;
// display human board
DisplayBoard(picHuman, human, false);
// display computer ships
DisplayBoard(picComputer, computer, false);
this.Refresh();
#if SPEECH
Say("Welcome to BATTLESHIP! Prepare to Lose!");
Say("Choose who starts first.");
#endif
}
I suspect I could bury it in the paint event, but then I'd have to keep track of whether it has spoken already.
I don't want it speaking every time the form is repainted.
Try adding the speech code to the Form_Shown event. This event is raised whenever the form is shown to the user (when they can actually see it). From MSDN:
The Shown event is only raised the first time a form is displayed; subsequently minimizing, maximizing, restoring, hiding, showing, or invalidating and repainting will not raise this event.