Windows Phone 7 : Silverlight and playing Music - silverlight

I have built a simple game in WP7 and I am trying to add background music to it using MediaPlayer. The problem is it just bombs with
{"An unexpected error has occurred."} System.Exception {System.InvalidOperationException}
Code
try
{
MediaPlayer.Stop();
// Timer to run the XNA internals (MediaPlayer is from XNA)
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(33);
dt.Tick += delegate { try { FrameworkDispatcher.Update(); } catch { } };
dt.Start();
Uri pathToFile = new Uri("Audio/music.m4a", UriKind.Relative);
Song playingSong = Song.FromUri("Music", pathToFile);
MediaPlayer.Play(playingSong);
}
catch (Exception e)
{
musicFailed = true;
Console.WriteLine("Exception: {0}", e.ToString());
MessageBox.Show("Warning, music failed to play however you can still continue to play your game.");
}
}
I tried a few tweaks, converting file to mp3, different paths etc. The file is marked for copy always and content type I also tried removing the Dispatcher as dont know what that is for.

If you can convert your audio files to WAV format, you can try using the XNA SoundEffect and SoundEffectInstance classes:
SoundEffect se = SoundEffect.FromStream(isolatedStorageFileStream);
SoundEffectInstance sei = se.CreateInstance();
sei.Play();
For this to work, you will need to reference the XNA library (Microsoft.XNA.Framework) and initialize the framework in this way:
App.xaml:
<Application>
<Application.ApplicationLifetimeObjects>
<local:XNAFrameworkDispatcherService />
...
</Application.ApplicationLifetimeObjects>
</Application>
And create this class somewhere in the app namespace ("local" in the previous xaml references this namespace):
public class XNAFrameworkDispatcherService : IApplicationService
{
private DispatcherTimer frameworkDispatcherTimer;
public XNAFrameworkDispatcherService()
{
this.frameworkDispatcherTimer = new DispatcherTimer();
this.frameworkDispatcherTimer.Interval = TimeSpan.FromTicks(333333);
this.frameworkDispatcherTimer.Tick += frameworkDispatcherTimer_Tick;
FrameworkDispatcher.Update();
}
void frameworkDispatcherTimer_Tick(object sender, EventArgs e) { FrameworkDispatcher.Update(); }
void IApplicationService.StartService(ApplicationServiceContext context) { this.frameworkDispatcherTimer.Start(); }
void IApplicationService.StopService() { this.frameworkDispatcherTimer.Stop(); }
}

Your problem is that your timer maybe not fired the first tick and FrameworkDispatcher.Update() not ran. Then Play throws a System.InvalidOperationException.
Better dispatch XNA Events global in your Application.
Complete Guide: XNAAsyncDispatcher

Related

ShowInputAsync is not showed sometimes

I realized an interface in WPF Page and I want to call ShowInputAsync in the MainWindow, which is where the Page is. In order to do that, I raise event in Page and block function with AutoResetEvent:
public AutoResetEvent OnMessageReceived;
public void MessageReceived(object sender, PageReturnMessageEventArgs e)
{
try
{
if (e.ToString() == "Cancel" || string.IsNullOrEmpty(e.ToString()))
{
throw new Exception("Exception of parsing of hours");
}
}
catch (Exception ex)
{
// log it
}
this.OnMessageReceived.Set();
}
private void Page_OnNextPageClick(object sender, EventArgs e)
{
// ...
MessageShow("Additional information", "How much time will this SuperProcess take?", "HOURS");
OnMessageReceived.WaitOne();
OnMessageReceived.Reset();
// ...
}
If I try to show ShowInputAsync from any function in MainWindow it works:
MetroDialogSettings s = new MetroDialogSettings();
s.AffirmativeButtonText = #"Create";
s.NegativeButtonText = #"Cancel";
s.AnimateShow = true;
var result = await this.ShowInputAsync("Test", "TestMessage", s);
if (result == null)
{ return; }
If I try to show this dialog from event handler, it doesn't work. After line
var result = await this.ShowInputAsync("Test", "TestMessage", s);
code returns to Page without any exception, executes line
OnMessageReceived.WaitOne();
and shows the window without any dialog, all Controls are showing and enabled, but I can't press them.
I also tried to put the dialog in a separate function, and call it with/without await, nothing changes.
To use AutoResetEvent after calling the Dialog to block further execution of code, also didn't help.
private AutoResetEvent OnMessageReturning;
var result = await this.ShowInputAsync(e.Title, e.Message, s);
OnMessageReturning.WaitOne();
Also tried to call the separate function in this way without result:
CancellationToken token;
TaskScheduler uiSched = TaskScheduler.FromCurrentSynchronizationContext();
await Task.Factory.StartNew(SeparateFunction, token, TaskCreationOptions.None, uiSched);
How can I call ShowInputAsync correctly in event handler? Or how can I call ShowInputAsync from Page in MainWindow?
P.S. There is TabControl in MainWindow, Page is placed in Frame of one TabItem
Using:
MahApps.Metro v1.4.1 (NuGet package)
Windows OS 7
Visual Studio Express 2015
.NET Framework 4.5
UPD: Easy sample is https://github.com/awg21/MahAppsShowInputAsyncFromPage
I found a solution here I'm using:
TryFindParent<> is an extension method defined in MahApps.Metro.Controls.TreeHelper, and ShowMessageAsync<> is defined in MahApps.Metro.Controls.Dialogs.DialogManager

Windows Phone - MediaElement not working anymore after navigation

I have a problem with the MediaElement in my windows phone (8) application. It's working fine until I navigate to some other page and come back on the page containing the MediaElement.
I have the following code to play a stream coming from Bing Translator APIs:
private void TranslationService_SpeakComplete(object sender, SpeakCompleteEventArgs e)
{
var stream = e.Stream;
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
try
{
MediaElement.Stop();
MediaElement.Source = null;
string filename = "FlipNLearnItAudio";
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
bool fileExists = isf.FileExists(filename);
if (fileExists)
{
isf.DeleteFile(filename);
}
var isfs = isf.CreateFile(filename);
using (isfs)
{
Helpers.SaveFile(stream, isfs);
isfs.Position = 0;
MediaElement.AutoPlay = true;
MediaElement.SetSource(isfs);
}
}
}
catch (Exception ex)
{
//TODO: log exception
}
});
}
The biggest problem is that there's no exception.... the "MediaElement.SetSource(isfs);" is called normally without any exception!
Do you have any idea of what can happen?
Thanks for any help!
Bastien
Ok, I find a solution to my problem. I put the MediaElement in the App.xaml resources and now it's working fine...
But it doesn't explain why it was not working with the MediaElement in the MainPage.xaml.
If somebody has the answer, I would be very interested to hear it ;-).

ChannelFactory method call increse memory

I have an winform application which consumes windows service, i user ChannelFactory
to connect to service, problem is when i call service method using channel the memory usage increase and after
method execute memory not go down(even after form close), i call GC.Collect but no change
channel Create class
public class Channel1
{
List<ChannelFactory> chanelList = new List<ChannelFactory>();
ISales salesObj;
public ISales Sales
{
get
{
if (salesObj == null)
{
ChannelFactory<ISales> saleschannel = new ChannelFactory<ISales>("SalesEndPoint");
chanelList.Add(saleschannel);
salesObj = saleschannel.CreateChannel();
}
return salesObj;
}
}
public void CloseAllChannels()
{
foreach (ChannelFactory chFac in chanelList)
{
chFac.Abort();
((IDisposable)chFac).Dispose();
}
salesObj = null;
}
}
base class
public class Base:Form
{
public Channel1 channelService = new Channel1();
public Channel1 CHANNEL
{
get
{
return channelService;
}
}
}
winform class
Form1:Base
private void btnView_Click(object sender, EventArgs e)
{
DataTable _dt = new DataTable();
try
{
gvAccounts.AutoGenerateColumns = false;
_dt = CHANNEL.Sales.GetDatatable();
gvAccounts.DataSource = _dt;
}
catch (Exception ex)
{
MessageBox.Show("Error Occurred while processing...\n" + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
CHANNEL.CloseAllChannels();
_dt.Dispose();
//GC.Collect();
}
}
You're on the right track in terms of using ChannelFactory<T>, but your implementation is a bit off.
ChannelFactory<T> creates a factory for generating channels of type T. This is a relatively expensive operation (as compared to just creating a channel from the existing factory), and is generally done once per life of the application (usually at start). You can then use that factory instance to create as many channels as your application needs.
Generally, once I've created the factory and cached it, when I need to make a call to the service I get a channel from the factory, make the call, and then close/abort the channel.
Using your posted code as a starting point, I would do something like this:
public class Channel1
{
ChannelFactory<ISales> salesChannel;
public ISales Sales
{
get
{
if (salesChannel == null)
{
salesChannel = new ChannelFactory<ISales>("SalesEndPoint");
}
return salesChannel.CreateChannel();
}
}
}
Note that I've replaced the salesObj with salesChannel (the factory). This will create the factory the first time it's called, and create a new channel from the factory every time.
Unless you have a particular requirement to do so, I wouldn't keep track of the different channels, especially if follow the open/do method/close approach.
In your form, it'd look something like this:
private void btnView_Click(object sender, EventArgs e)
{
DataTable _dt = new DataTable();
try
{
gvAccounts.AutoGenerateColumns = false;
ISales client = CHANNEL.Sales
_dt = client.GetDatatable();
gvAccounts.DataSource = _dt;
((ICommunicationObject)client).Close();
}
catch (Exception ex)
{
((ICommunicationObject)client).Abort();
MessageBox.Show("Error Occurred while processing...\n" + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
The code above gets a new ISales channel from the factory in CHANNEL, executes the call, and then closes the channel. If an exception happens, the channel is aborted in the catch block.
I would avoid using Dispose() out of the box on the channels, as the implementation in the framework is flawed and will throw an error if the channel is in a faulted state. If you really want to use Dispose() and force the garbage collection, you can - but you'll have to work around the WCF dispose issue. Google will give you a number of workarounds (google WCF Using for a start).

WPF : I got problem in draw something on canvas using thread

I try to draw something on canvas every 1 second .Then, I create a thread to call a method to draw after sleep 1 second. The problem is calling Canvas object to draw. The program tell me error message
The calling thread cannot access this object because a different thread owns it.
Code
private void drawTimeTick() {
...
Thread iThread = new Thread(new ThreadStart(tickThread));
iThread.Start();
}
private void tickThread(){
try
{
Thread.Sleep(1000);
...
Canvas.SetLeft(tick, 700);
Canvas.SetTop(tick, 30);
}catch(Exception ex){
MessageBox.Show("Exception tickThread : "+ex.Message);
}
}
How to access Canvas in Thread to draw?
Have a look at the DispatcherTimer class. It is designed for exactly this scenario, as can be seen in the MSDN examples.
You can call
this.Dispatcher.Invoke((Action)(() =>
{
Canvas.SetLeft(tick, 700);
Canvas.SetTop(tick, 30);
}));
(I'm assuming that this is some UI control. If not you can use Application.Current.Dispatcher instead). It will execute your canvas operations on the UI thread.
I have try DispatcherTimer, It's work.
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += tickThread;
timer.Start();
}
private void tickThread(object sender, EventArgs e)
{
try
{
tick.thisTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
double TickPixel = GetPixel(ClassTimeline.Width, startTime, endTime, tick.thisTime);
Canvas.SetLeft(tick, TickPixel);
Canvas.SetTop(tick, 30);
}catch(Exception ex){
MessageBox.Show("Exception tickThread : "+ex.Message);
}
}
You can't access graphical objects from a different thread from the ones that created it.

MainWindow.Closing event not always raised in Silverlight 4 OOB app

I've made a rather complex Silverlight 4 out-of-browser application. One of my main view models adds an event handler to the Application.Current.MainWindow.Closing event.
This works fine when the application is initially run. It is able to cancel the close operation.
However, sometimes after performing operations like showing and closing a ChildWindow, the MainWindow's Closing event is no longer calling my handler.
In the debugger, I added a watch to the MainWindow's underlying closing event delegate. It's not null before showing the ChildWindow. Then sometimes after the ChildWindow is closed the delegate is null. This is explains why my handler is not called any more. But why is this delegate getting nulled? And why is it only happening occasionally?
My application is not unbinding my event handler at any point.
This is the delegate I'm watching:
System.Windows.Application.Current.MainWindow.m_closingEvent
Other stuff: I'm using Caliburn Micro
I had the exact same problem. We have a large silverlight application running OOB.
For some reason the m_ClosingEvent was nulled after running for a while. I have not been able to find the cause of this issue but I think it may have something to do with us changing the root visual or all the child windows we show.
I´m using a class ApplicationWrapper.
public class ApplicationWrapper : IApplicationWrapper
{
public void Initialize()
{
HookCloseEvent(true);
}
private void HookCloseEvent(bool hook)
{
if (hook && IsRunningOutOfBrowser)
{
Application.Current.MainWindow.Closing += OnClosing;
}
else
{
if (IsRunningOutOfBrowser)
{
Application.Current.MainWindow.Closing -= OnClosing;
}
}
}
private void OnClosing(object sender, ClosingEventArgs e)
{
InvokeClosing(e);
}
... etc..
}
And the InvokeClosing method was never called. But when I changed it to
public class ApplicationWrapper : IApplicationWrapper
{
private Window _mainWindow;
public void Initialize()
{
if(IsRunningOutOfBrowser)
{
_mainWindow = Application.Current.MainWindow;
}
HookCloseEvent(true);
}
private void HookCloseEvent(bool hook)
{
if (hook && IsRunningOutOfBrowser)
{
_mainWindow.Closing += OnClosing;
}
else
{
if (IsRunningOutOfBrowser)
{
_mainWindow.Closing -= OnClosing;
}
}
}
private void OnClosing(object sender, ClosingEventArgs e)
{
InvokeClosing(e);
}
... etc...
}
The m_ClosingEvent isn´t nulled.
So, try to just store the "initial" MainWindow in a field and check if that solves your problem.
Instead of hooking to the event, why not register a service instead? Create a class that implements IApplicationService and IApplicationLifetimeAware. The latter gives you an "onexiting" and "onexited" pair of events. You place the service in the application by pointing to it in a section called in your App.xaml. I've used this for many projects and never had an issue with the exiting methods not being called.
Ok, after pulling out my hair and many false starts I finally found the answer - it seems to be a known bug with the Closing event, OOB and ChildWindows open/closes...
The trick is to store a static reference to the Main Window:
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
//you have to store this to work around the bug
//http://forums.silverlight.net/forums/p/185664/424174.aspx
_mainWindow = App.GetApp.MainWindow;
App.GetApp.MainWindow.Closing += (s, e1) =>
{
if (UIUtilities.ShowMessage("Would you like to exit AMT Mobile?", "Exit Application", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
{
e1.Cancel = true;
}
};
}

Resources