Scheduling an application action in wpf - c#

I am trying to create a WPF media application for running audio files using Media Element.
I succeeded in it . But i want to schedule playing a songs which i selected in some interval of time repeatedly. Say 10'o clock in every day or in every hour etc..
What is the best way to do that ?
Initially am think about doing it using a Timer. But that makes my code complex because i have to play multiple songs in different intervals set by user.
Recently i go to know about Task Scheduler and i run their sample code [open a notepad] and it works perfectly.
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Does something";
// Create a trigger that will fire the task at this time every other day
td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(#"Test", td);
// Remove the task we just created
ts.RootFolder.DeleteTask("Test");
}
How do i define an action which play a song on this context ? Currently they are providing ExecAction,SendEmail,ShowMessage and ComHandlerAction see this link
Or Can we invoke play method of my application by this Task Scheduler? Please help me by providing any idea .
Thanks

First, you would need to order your collection of times with the closest time first. Then, you would only need one DispatcherTimer and you could set its Interval value to the length of time between now and the first time from your collection:
private DispatcherTimer mediaPlayerTimer = null;
...
mediaPlayerTimer = new DispatcherTimer();
mediaPlayerTimer.Interval = YourFirstDateTime.Subtract(DateTime.Now);
mediaPlayerTimer.Tick += MediaPlayerTimer_Tick;
mediaPlayerTimer.Start();
...
private void MediaPlayerTimer_Tick(object sender, EventArgs e)
{
mediaPlayerTimer.Stop();
// Load next audio file and play
// Remove YourFirstDateTime from your collection
// Set YourFirstDateTime = the next item from the collection
mediaPlayerTimer.Interval = YourFirstDateTime.Subtract(DateTime.Now);
mediaPlayerTimer.Start();
}

Related

Running Background Audio in different pages of UWP App

I am making a UWP App where I run Background Audio in the MainPage on a Button Click event. When I move to another page, there's also a different Media to play in Background Audio Task there.
How can I stop the currently playing Task to run the other? Should I define something globally? Any help regarding this issue?
Edit
I am using this sample: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundAudio While the backgroundAudio of the first Page is running, I go to the second page and on a click event I set a new List with the following code:
// First update the persisted start track
ApplicationSettingsHelper.SaveSettingsValue(ApplicationSettingsConstants.TrackId, RadioFacade.mySongs[0].MediaUri.ToString()); //here
ApplicationSettingsHelper.SaveSettingsValue(ApplicationSettingsConstants.Position, new TimeSpan().ToString());
// Start task
StartBackgroundAudioTask();
But the new song takes more than the estimated time to run and enter the else of this method:
private void StartBackgroundAudioTask()
{
AddMediaPlayerEventHandlers();
var startResult = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
bool result = backgroundAudioTaskStarted.WaitOne(10000);
//Send message to initiate playback
if (result == true)
{
MessageService.SendMessageToBackground(new UpdatePlaylistMessage(RadioFacade.mySongs));
MessageService.SendMessageToBackground(new StartPlaybackMessage());
}
else
{
throw new Exception("Background Audio Task didn't start in expected time");
}
});
startResult.Completed = new AsyncActionCompletedHandler(BackgroundTaskInitializationCompleted);
}
and the old (first playing) song keeps playing.
I tried to Stop the current BackgroundMediaPlayer using BackgroundMediaPLayer.Shutdown() but it didn't work.
Any idea how to let the old song stop and the current song play?
You can control the background media player by sending messages to it from the foreground. For example,
From the foreground app:
BackgroundMediaPlayer.SendMessageToBackground(new ValueSet
{
{"playnew", "some value"}
});
In your background task:
public sealed class AudioPlayer : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;
...
...
}
private async void BackgroundMediaPlayer_MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e)
{
object value;
if (e.Data.TryGetValue("playnew", out value) && value != null)
{
// value will be whatever you pass from the foreground.
BackgroundMediaPlayer.Current.Pause();
BackgroundMediaPlayer.Current.Source = stream source;
BackgroundMediaPlayer.Current.Play();
}
}
...
}
"playnew" can be a global constant in your application. You can use the ValueSet to pass the live stream url to the background task as the value.

How to add background task to Windows Store App

using c#, VS 2013
Try to add some background task to my Store app (idea to show toast if some data in my Json file contains current date and time).
What was done:
1.Create Windows Runtime Component, that implement IBackgroundTask , add reference to my Windows Store App. Inside WRC create class that contains next code :
namespace BackgroundTask
{
public sealed class EventChecker: IBackgroundTask
{
ThreadPoolTimer _periodicTimer = null;
public void Run(IBackgroundTaskInstance taskInstance)
{
_periodicTimer
= ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler(PeriodicTimerCallback), TimeSpan.FromSeconds(30));
}
private void PeriodicTimerCallback(ThreadPoolTimer timer)
{
CheckEventAndShowToast();
}
....
}
2.Register task : In MainPage.xaml.cs add in method OnNavigatedTo registering of this background task. Code:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
navigationHelper.OnNavigatedTo(e);
var taskName = "EventCheckerTask";
if (BackgroundTaskRegistration.AllTasks.Count > 0)
{
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
if (cur.Value.Name != taskName)
{
BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
builder.Name = taskName;
builder.TaskEntryPoint = "BackgroundTask.EventChecker";
BackgroundTaskRegistration taskToRegister = builder.Register();
}
}
}
else
{
BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
builder.Name = taskName;
builder.TaskEntryPoint = "BackgroundTask.EventChecker";
BackgroundTaskRegistration taskToRegister = builder.Register();
}
}
Use MSDN1, MSDN2, MSDN3 links.
Also OnComplete not implement - because i don't need it (or I must to implement it anyway?)
3.Declare in manifest.
Set toast capable to "YES":
Declare background Task:
4.Check functionality of all method that i want to use for background - all Ok and work
Durring debugging all it's ok, no errors/ exceptions, but nothing happend. Try to debug step by step - looks like all it's ok, think i make some mistake in code.
So question: where i'm wrong, why i cant launch my background task that must to check data and do required action if some conditions are as required?
EDIT
Part 2 - Try to realize background task in new solution.
What was done:
Create new simple CRC :
namespace Tasks
{
public sealed class Tasks : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
//for checking place debug point
//TODO something
}
}
}
Also in main.xaml.cs placed next code:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
CheckTaskRegistration();
}
private void CheckTaskRegistration()
{
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == "Tasks")
{
isTaskRegistered = true;
break;
}
}
if (!isTaskRegistered)
{
RegisterBackgroundTask2("Tasks", "Tasks.Tasks");
}
}
private void RegisterBackgroundTask2(string name, string entrypoint)
{
BackgroundTaskBuilder btb = new BackgroundTaskBuilder();
btb.Name = name;
btb.TaskEntryPoint = entrypoint;
//IBackgroundTrigger everyMinuteTrigger = new TimeTrigger(1, false);
// btb.SetTrigger(everyMinuteTrigger);
btb.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false));
BackgroundTaskRegistration task = btb.Register();
}
As result got, that with this trigger btb.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false)); all works - i can go inside Run method, but if I try to use TimeTrigger like
//IBackgroundTrigger everyMinuteTrigger = new TimeTrigger(1, false);
// btb.SetTrigger(everyMinuteTrigger);
nothing happend - wait few minutes try few times (placed instead prev trigger registration).
Question - Why? Do i must to do something more?
Also old questions are without answers too...
Also try to use it with my App - all worksperfect, but only if i connect to lan... But why it's not work with time trigger?
A spend a little bit more time and found few root causes for my problem:
I must to use some trigger with my BackgroundTask if I want to use it and launch. problem here that ther is not exactly what i need exist (or maybe i need to read a little bit more about triggers).
So if I add some trigger, BackgroundTask can be launched after such event happend. Example:
//Time trigger
IBackgroundTrigger everyMinuteTrigger = new TimeTrigger(15, false);
btb.SetTrigger(everyMinuteTrigger);
//one of the standart tirgger
btb.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false));
If I want to use TimeTrigger, in EDIT i write code with TimeTrigger(1, false);, but after reading some more detailed documentation found "that the time trigger only accepts values greater than or equal to 15; smaller values fail during Register." MSDN
Also if I want to use TimeTrigger i must to add LockScreenNotification support to my App
Currently I can launch backgroundTask every 15 min, but it's not exactly what i want...
So, regarding this post quation - i found answer, but still need to read more deeply about BackgroundTask
Is your CheckEventAndShowToast(); calling anything asynchronously?
If that is the case then you should follow step 4 from the MSDN2 link you posted.
"4. If you run any asynchronous code in your background task, then your background task needs to use a deferral. If you don't use a deferral, then the background task process can terminate unexpectedly if the Run method completes before your asynchronous method call has completed."

Timer in AudioPlaybackAgent

I have an Internet radio app that uses BackgroundAudioPlayer.
I need a timer in the Audio Playback Agent that will update the track title of the currently playing track of the BAP that is pulled from the Internet radio station's API.
Adding a DispatcherTimer into the Audio Playback Agent gives me a cross-thread exception, and using:
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Code
});
Didn't work.
I need the code in here because if I put the update code in the app itself, when the user navigates away from the app the updates stop (much unlike Windows 8's behavior).
I can't use Scheduled Agents since they only run once every 30 minutes (IIRC).
Is this possible or can this not be done on Windows Phone?
Below is an excerpt from the MSDN documentation for Background Audio Player:
Sending messages between tasks:
There are times when you will want to communicate between the two processes of a background audio app. For example, you might want the background task to notify the foreground task when a new track starts playing, and then send the new song title to the foreground task to display on the screen. A simple communication mechanism raises events in both the foreground and background processes. The SendMessageToForeground and SendMessageToBackground methods each invoke events in the corresponding task. Data can be passed as an argument to the event handler in the receiving task. Pass data using a new class called ValueSet. This class is a dictionary that contains a string as a key and other value types as values. You can pass simple value types such as int, string, bool, and so on.
https://msdn.microsoft.com/en-US/library/windows/apps/xaml/dn642090
Hope this helps!
I found a question which could help you : How to run a timer on background in windows phone 8?
when you set a timer which is checking every x seconds if the "title" differs from last known title then you could send this info back to it.
This could be the Code for the Timer:
Declare these:
string _newValue = string.Empty;
string _currentValue = string.Empty;
AudioTrack _tempTrack = null;
and set this as Tick for the Timer
if (this.BackgroundAudioPlayer != null)
{
if (this.BackgroundAudioPlayer.Instance != null)
{
if (this.BackgroundAudioPlayer.Instance.Track != null)
{
this._newValue= yourAPI.GetTitleOfTrack();
try
{
/* First try to get the current Track as own Var */
this._tempTrack = this.BackgroundAudioPlayer.Instance.Track;
if (this._tempTrack != null)
{
/* Then Read the .Tag Value from it, save to _currentValue */
if (this._tempTrack.Tag != null)
{ this._currentValue = this._tempTrack.Tag.ToString(); }
else
{ this._currentValue = string.Empty; }
/* Compare */
if (this._currentValue != this._newValue)
{
/* Edit the Track Tag from your original BAP */
this.BackgroundAudioPlayer.Instance.Track.Tag = this._newValue;
}
}
}
catch(Exception ex)
{
/* if something Crashes you can save the exception error for protocol */
}
}
}
}
Remember: Change "yourAPI.GetTitleOfTrack()"-Function from this with real Function Call of your API.
Have you considered updating the information in the background audio player agent as below in the track tag.
string newTag = "whatever you need to show";
AudioTrack track = BackgroundAudioPlayer.Instance.Track;
track.BeginEdit();
track.Tag = newTag;
track.EndEdit();
and then reading that tag in the front end by your application when needed?

Link Media Element on WPF with Label and Slider

I have linked the import button with a media element so i can get the song to play.
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".txt";
dlg.Filter = "WAV Files (*.wav)|*.wav|MP3 Files (*.mp3)|*.mp3|MP4 Files (*.mp4)|*.mp4|WMA Files (*.wma)|*.wma|SWA (*.swa)|*.swa";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
meMedia1.Source = new Uri(dlg.FileName);
meMedia1.Play();
//txtFileLocation.Text = filename;
Now, the sound plays but what I want to do is link a slider so they can skip some of the song and also a label above the slider so that it read how long into the song it is. This is how my application looks now to give you an idea.
http://i.stack.imgur.com/sVtrd.png
Thank You.
EDIT : Got the seek to change the song position but I still cant get it manually moving to the time of the song, for example if I skip to the middle of the song, and let the song finish my slider will still be in the middle and I want it to be at the end.
One approach is to create a DispatcherTimer that ticks every 200-800ms (depending on your preference for update speed) that syncs the slider to the player's current Position. That code might look similar to this:
// In the class members area
private DispatcherTimer _timer = null;
// In your constructor/loaded method
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(500);
_timer.Tick += _timer_tick;
// Timer's tick method
void _timer_tick(object sender, EventArgs e)
{
// Convert duration to an integer percentage based on current position of
// playback and update the slider control
TimeSpan ts = meMedia1.NaturalDuration.TimeSpan;
int percent = int( meMedia1.Position / ts.Seconds * 100 );
mySliderControl.Value = percent;
}
Note that this assumes you have a Slider whose Min is 0 and Max is 100. You can bump it up to 0-1000 (and change the math accordingly) to get finer granularity. This also doesn't allow the slider to push user interaction back to the player, but gives you an idea of one way to get the opposite. You can add an event handler to the Slider such that when the user begins interacting, this _timer is stopped ( _timer.Stop() ) so updates to the media position stop updating the slider and instead start doing your slider -> media position updates instead. Then when the user lets go of the slider, turn the _timer back on ( _timer.Start() ).

Build time based bars using stock tick data

I'm trying to build stock market Bar (snapshot) data at run-time using tick data. My stock data provider provides access to tick level data where I have an event called OnTick that is triggered whenever a new tick is sent by the data provider. I'm hoping to do one of the two below, or if someone can suggest a good option:
Option 1:
In this option I maintain a Bar object and update it each time I get a tick. The OnBar() event can be attached to a timer elapsed event (1 minute for 1 minute bars etc).
//TickMsg = double price, DateTime dttm
public void OnTick(TickMsg newTick)
{
TaskFactory.StartNew(){UpdateBar(newTick)};//Syntax not specific
}
UpdateBar()
{
//nextBar is a Bar object thats intialized to Open = 0, High = 0, Low = 0, Close = 0
if(nextBar.Open==0)
nextBar.Open = newTick.price;
if(newTick.price>nextBar.High)
nextBar.High = newTick.price;
if(newTick.price<nextBar.Low)
nextBar.Low = newTick.price;
nextBar.Close = newTick.price;
}
public void OnBar(Bar bar)
{
//Process the bar..perform calculations etc
bar = new Bar(0,0,0,0);//Reset the bar
}
Option 2:
In this option I'm just adding the tick to a list of ticks and perform the calculations when OnBar is called. The OnBar() event can be attached to a timer elapsed event (1 minute for 1 minute bars etc).
List <TickMsg> TickList;
public void OnTick(TickMsg newTick)
{
TickList.Add(newTick);
}
public void OnBar()//called on a timer
{
var low = TickList.Min();
var high = TickList.Max();
var close = (from entry in TickList orderby entry.TickMsg.dttm ascending).Last();
var open = (from entry in TickList orderby entry.TickMsg.dttm ascending).First();
TickList.Empty();
}
Questions:
Which approach is more processing intensive?
Which approach requires more memory?
Again, if someone has a suggestion on an alternative approach, I'm all ears.
Don't you need to display or access the bar before it's completed? In case option 2 seems not to achieve that. Option 1 I can never imagine will be a performance clog. And it will use less memory, as you don't seem to save the tick data to any variable.
I think the best approach it is the second.
When you reset the bar on first approach, the lowest price will never be less than zero, so, the low price on bar will be always zero.
Trade link is a good example on how it is done. Their tutorials cover this. Also since it is open source you can have a peek into how it is done. The getting started tutorial is here.

Categories

Resources