Play audio in background by use BackgroundCapableMedia - windows-8

In the windows8 Developer preview we can use this code to play audio in background:
mediaElement.AudioCategory = AudioCategory.Media;
In the windows8 Customer perview, It seems that we should use AudioCategory.BackgroundCapableMedia instead of AudioCategory.Media
mediaElement.AudioCategory=AudioCategory.BackgroundCapableMedia;
and I also Declare a background task in appxmanifest
<Extension Category="windows.backgroundTasks" EntryPoint="TestApp.App">
<BackgroundTasks>
<Task Type="audio" />
</BackgroundTasks>
</Extension>
but it didn't work for me and the MediaElement will throw an "MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED“ exception in MediaFailed EventHandler
How should I do?

You also need to set up these event handlers:
using Windows.Media;
MediaControl.PlayPressed += MediaControl_PlayPressed;
MediaControl.PausePressed += MediaControl_PausePressed;
MediaControl.PlayPauseTogglePressed += MediaControl_PlayPauseTogglePressed;
MediaControl.StopPressed += MediaControl_StopPressed;
-
void MediaControl_StopPressed(object sender, object e)
{
myMediaPlayer.Stop();
}
void MediaControl_PlayPauseTogglePressed(object sender, object e)
{
}
void MediaControl_PausePressed(object sender, object e)
{
myMediaPlayer.Pause();
}
void MediaControl_PlayPressed(object sender, object e)
{
myMediaPlayer.Play();
}
I think that should have it working.

Related

ObjectDisposedException on scrollview renderer

I am firing the following event in my Xamarin application:
AppEvents.Instance.UI.RiseSearchStringTypingEvent(this, new EventArgs());
The HomePage.xaml.cs codebehind subscriber-event performs an auto scroll to top every time the event is being fired (on every input on the search field)
void OnSearchStringTyping(object sender, EventArgs e)
{
scrollView.ScrollToAsync(0.0, 0.0, true);
}
when the searchstring is complete and the user hits the submit-button, the Oncompleted() method invokes on the SearchHeaderView.xaml.cs which navigates to the SearchPage
void OnCompleted(object sender, EventArgs e)
{
var tag = this + ".OnCompleted";
try
{
if (string.Compare(LastSearchText, SearchEntry.Text) == 0) return
if (Device.RuntimePlatform == Device.Android)
{
SearchRequest();
}
else if (Device.RuntimePlatform == Device.iOS)
{
var task = new Task(SearchRequest);
task.Start();
}
LastSearchText = SearchEntry.Text;
}
catch (Exception ex)
{
Track.Exception(tag, ex);
}
}
This procedure works perfect on iOS platforms...But in android, when i submit my search string, the app crashes with an ObjectDisposedException on the ScrollView
I think the problem is that the application tries to scroll up the view again after it navigates away from the homepage. The SearchRequest() method on Android is not invoked in a new Thread, but iOS is. How can i fix this? I can not just make the method being invoked asynchronously, because the Search doesn't work then.

Webcam motion Detection using AForge C#

I am using code for motion detection webcam using Aforge. When running the program, it can run perfectly, but when edit libel1 "motion Detection" and libel2 "no motion Detection" it cannot running. Why?
The code I'm using for motion detection:
public Form1()
{
InitializeComponent();
}
FilterInfoCollection fic;
VideoCaptureDevice Device;
MotionDetector motionDetector;
float f;
private void Form1_Load(object sender, EventArgs e)
{
motionDetector = new MotionDetector(new TwoFramesDifferenceDetector(), new MotionAreaHighlighting());
fic = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo item in fic)
{
comboBoxDevice.Items.Add(item.Name);
}
comboBoxDevice.SelectedIndex = 0;
}
private void BtnStart_Click(object sender, EventArgs e)
{
Device = new VideoCaptureDevice(fic[comboBoxDevice.SelectedIndex].MonikerString);
videoSourcePlayer1.VideoSource = Device;
videoSourcePlayer1.Start();
}
private void BtnStop_Click(object sender, EventArgs e)
{
videoSourcePlayer1.Stop();
}
private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
{
if (motionDetector == null) return;
f = motionDetector.ProcessFrame(image);
if (f >0)
{
label1.ForeColor = Color.Red;
label1.Text = "Motion Detected";
}
else
{
label1.ForeColor = Color.Green;
label1.Text = "No Motion Detected";
}
}
private void timer_Tick(object sender, EventArgs e)
{
label3.Text = "Value: " + f.ToString();
}
}
}
I think you want to change a label in your UI when you detect a motion. If you want to change a label in UI, you can't run the change function in the same thread. So, you can change it by defining another thread. Just make sure you prevent race conditions.
// Changing UI Labels (Make thread)
Label1.Invoke((MethodInvoker)delegate {
// What do you want to change?
Label1.Text = "Detecting Motions";
});

background worker dont work

I want to read a text file from the Internet and I want while reading the file a picturebox, that is a gif animation, show and after the reading is finished picturebox hide.
I use background worker. I have a lable that shows the state, but when I click BtnCheck Button bg doesn't work and the lable doesn't change.
My code:
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
}
private void BtnCheck_Click(object sender, EventArgs e)
{
PbLoading.Visible = true;
if (backgroundWorker1.IsBusy != true)
{
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
LbleState.Text = "Reading txt File...";
webClient1 = new WebClient();
if (CheckForInternetConnection())
{
try
{
Stream stream = webClient1.OpenRead(TxtWebAdrss);
StreamReader reader = new StreamReader(stream);
String content = reader.ReadToEnd();
reader.Close();
LbleState.Text = "Reading Finished .";
}
catch
{
LbleState.Text = "Error reading";
}
}
else LbleState.Text = "Internet not connected!";
}
You may just need to do a bit more research into this class. You should perform UI changes on the UI thread.
There are three event handlers that you can use and these are,
backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
The following link should help,
http://msdn.microsoft.com/en-us/library/System.ComponentModel.BackgroundWorker(v=vs.110).aspx

Error in Windows 8 xaml Modern app with thread

I have following two methods. When user clicks on start button from ui, the step geoLocator_PositionChanged in geoLocator_PositionChanged method is fired and calls the other method geoLocator_PositionChanged.But when it comes to try block while executing the first statement it throws the following error:
"The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))"
private async void btnStartStop_Click_1(object sender, RoutedEventArgs e)
{
geoLocator.PositionChanged += geoLocator_PositionChanged;
}
async void geoLocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
MessageDialog msgdlg = null;
bool bDisplayDialog = false;
try
{
lblAltValue.Text = args.Position.Coordinate.Altitude.ToString();
}
catch
{
}
}
Any help how can I fix this issue ?
You try to access the UI-Thread from another one.
Try something like ths
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync
(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
//HERE GOES THE UI ACCESS LIKE this.textbox.text = "MY AWESOME TEXT";
});

DOTMsn is not firing the SingedIn event

I have just started building a app using “XihSolutions.DotMSN.dll” version: 2.0.0.40909,
My problem is that it is not firing the “Nameserver_SignedIn” event. Not sure if I am doing something wrong.
your help will be really helpful.
void Nameserver_SignedIn(object sender, EventArgs e)
{
throw new Exception("User Signed In");
}
private string message = string.Empty;
void NameserverProcessor_ConnectionEstablished(object sender, EventArgs e)
{
message = "Connected";
SetMessage();
}
void SetMessage()
{
if (tbMessage.InvokeRequired)
tbMessage. Invoke(new ThreadStart(SetMessage));
else
tbMessage.Text += Environment.NewLine+ message;
}
private void btnSingIn_Click(object sender, EventArgs e)
{
if (messenger.Connected)
{
// SetStatus("Disconnecting from server");
messenger.Disconnect();
}
// set the credentials, this is ofcourse something every DotMSN program will need to
// implement.
messenger.Credentials.Account = tbUserName.Text;
messenger.Credentials.Password = tbPwd.Text;
// inform the user what is happening and try to connecto to the messenger network.
//SetStatus("Connecting to server");
messenger.Connect();
}
You might use MSNPSharp instead - DotMSN is old and may not support the current MSN protocol. There link is here:
http://code.google.com/p/msnp-sharp/