all. I am trying to develop an application for Windows Phone 7 using Visual Studio 2010. It is a music player that is supposed to be able to play music based on the current event.
I managed to extract the event but when I tried to combine it with the player, the entire player would just crash. Here are the codes.
void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e)
{
try
{
AppointmentResultsDataLINQ.DataContext =
from Appointment appt in e.Results
where appt.IsAllDayEvent == false
select appt;
}
catch (System.Exception)
{
//No results
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if ((((Appointment)(AppointmentResultsDataLINQ.DataContext)).Subject).Equals("Meeting"))
{
mediaElement1.Source = new Uri("http://www.opendrive.com/files/NV8zNTMwNDYwX2hxRXZR/Crystallize.mp3", UriKind.Absolute);
}
else
{
mediaElement1.Source = new Uri("https://www.opendrive.com/files/NV8zMjAxODY0X0VBNDJY/Hetken%20tie%20on%20kevyt%20(piano%20cover)%20-%20YouTube.mp3", UriKind.Absolute);
}
mediaElement1.Play();
}
The problem is the cast. You are trying to cast the AppointmentResultsDataLINQ.DataContext to an Appointment. This does not make sense. You need to select one concrete appointment from using LINQ (similar to the code in your Appointments_SearchCompleted that imho does nothing)
Related
I am trying to develop a pivot application for Windows Phone 7 using Visual Studio 2010. I got the data from the calendar with the following code.
void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e)
{
try
{
AppointmentResultsDataLINQ.DataContext =
from Appointment appt in e.Results
where appt.IsAllDayEvent == false
select appt;
}
catch (System.Exception)
{
//No results
}
}
The problem comes when I tried to connect to the next button.
private void button2_Click(object sender, RoutedEventArgs e)
{
if (AppointmentResultsDataLINQ.DataContext.ToString() == "Meeting")
{
mediaElement1.Source = new Uri("http://www.opendrive.com/files/NV8zNTMwNDYwX2hxRXZR/Crystallize.mp3", UriKind.Absolute);
}
else
{
mediaElement1.Source = new Uri("https://www.opendrive.com/files/NV8zMjAxODY0X0VBNDJY/Hetken%20tie%20on%20kevyt%20(piano%20cover)%20-%20YouTube.mp3", UriKind.Absolute);
}
mediaElement1.Play();
How can I convert the data to string so that it can play the two songs correctly? Because right now, even though I set the event to "Meeting" on the calendar, it still plays the second song.
You have binded the ListBox using the Appointment object, so the DataContext needs to be typecasted into the same. And by setting the event as Meeting if it means setting the Subject field of Appointment class as "Meeting" then this code should work fine.
if ((((Appointment)(AppointmentResultsDataLINQ.DataContext)).Subject).Equals("Meeting")))
{
mediaElement1.Source = new Uri("http://www.opendrive.com/files/NV8zNTMwNDYwX2hxRXZR/Crystallize.mp3", UriKind.Absolute);
}
When you are doing AppointmentResultsDataLINQ.DataContext.ToString() you are converting the DataContext object's address value to string and not getting the required string value. Also "==" doesn't work for string comparisons.
In my WP7 app, I want 2 music files to run in background.I am using MediaElement to do this. I am facing two issues.
how to play in background?
How to loop background music?
This is how I do it. Add the following:
<MediaElement x:Name="meSong" />
This is added in the constructor:
meSong.MediaEnded += new RoutedEventHandler(meSong_MediaEnded);
This is how I loop the song once it has ended:
private void meSong_MediaEnded(object sender, RoutedEventArgs e)
{
meSong.Position = TimeSpan.Zero;
meSong.Play();
}
This is how I set my song:
private void SetSong(string selectedSong)
{
if (ViewModel.IsMusicOn)
{
try
{
meSong.Stop();
meSong.Source = new Uri(string.Format("Media/Sounds/{0}.wav", selectedSong), UriKind.Relative);
meSong.Position = new TimeSpan(0);
meSong.Volume = 0.5;
}
catch (Exception)
{
// nothing for now
}
}
}
And this is how you obviously start your music:
meSong.Play();
i need to play a sound on the touch of screen, and it should remain in play state until user get his hands off from the screen. here is the code of mine,
private void OnMouseDown(object sender, Moenter code hereuseButtonEventArgs e)
{
clicked = true;
ColoringSound.Source = new Uri("Sounds/drawing.mp3", UriKind.Relative);
ColoringSound.Play();
}
private void OnMouseUp(object sender, MouseButtonEventArgs e)
{
clicked = false;
}
There is an MSDN article that describes how to loop MediaElements, all you have to do to stop a looped sound is invoke the Stop method.
In my windows phone 7 app I'm using the following code to navigate to a page
private void button2_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Views/Game.xaml, UriKind.RelativeOrAbsolute));
}
In the OnNavigatedTo event of Game.xaml.cs I use the following code to play two wav files one after another. In order for me to prevent both audio play at the same time, I use while loop to wait until the first audio is finish before the second file is played. I believe this is what causing the first audio to play before the game page is displayed. Is there a better way of doing this?
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
playAudio("intro.wav");
while (MySoundEffect.State == SoundState.Playing)
{
}
playAudio("firstQuestion.wav");
base.OnNavigatedTo(e);
}
The following is the code for playAudio.
protected void playAudio(string fileName)
{
Stream stream = TitleContainer.OpenStream("audio/" + fileName);
SoundEffect effect = SoundEffect.FromStream(stream);
MySoundEffect = effect.CreateInstance();
FrameworkDispatcher.Update();
MySoundEffect.Play();
}
MySoundEffect is a property of the Game class.
private SoundEffectInstance _MySoundEffect;
public SoundEffectInstance MySoundEffect
{
get { return _MySoundEffect; }
set { _MySoundEffect = value; }
}
Whenever user click the button, the sound
To answer your question in the title, you could try the same with the
Loaded
event handler instead of the OnNavigateTo. This way, your audio files will start playing
only after the page is loaded.
i am trying to create a simple demo application that does the following: i have a button at MainPage.xaml (with Name="btnCamera") and an image control (with Name="photo") and when i press the button i want to start the camera task, capture a photo and display it on the image control. The problem is that my code works on the emulator but not on a real device. The device i have is updated to the latest update(7740). Do you have an explanation for that or any change to my code to make it work? That is my code:
public partial class MainPage : PhoneApplicationPage
{
CameraCaptureTask _cameraCapture;
public MainPage()
{
InitializeComponent()
_cameraCapture = new CameraCaptureTask();
_cameraCapture.Completed += new EventHandler(_cameraCapture_Completed);
}
private void btnCamera_Click(object sender, RoutedEventArgs e)
{
try
{
_cameraCapture.Show();
}
catch (Exception)
{
MessageBox.Show("Error occured");
}
}
void _cameraCapture_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage image = new BitmapImage();
image.SetSource(e.ChosenPhoto);
photo.Source = image;
}
}
}
You need to make sure Zune is not running. The code looks fine and should work if you unplug the phone from the PC. If you want to debug whilst plugged into the PC, use WPConnect instead of Zune.