This is a simplified snippet of a larger WPF project to illustrate the problem.
When the play button is pressed, this code plays the mp3's "1, 2, 3, 4, 5" then stops.
When the play button is pressed, the dispatch timer starts. On every Tick, the Metronome_Run event is called. The Metronome_Run event assigns a mp3 to play by indexing an array.
The problem is, it works fine the first time you press play, you get 1,2,3,4,5, but when pressing play again, you get 2,4,5, then pressing play again, you get 3,5! As if the indexing of the files seems to be offset each time play is pressed somehow.
When stepping through with the debugger, it runs perfect every time, yet without any breakpoints set, (like in the build), it works correctly only the first time and does not work correctly on further play button presses.
Any help is appreciated!
public partial class MainWindow : Window
{
private MediaPlayer mediaPlayer = new MediaPlayer();
List<int> NoteValArray = new List<int>() { 1,2,3,4,5 };
private DispatcherTimer timer1;
int numVal = 0;
int numIndex = 0;
private float tempo = 35f;
public MainWindow()
{
InitializeComponent();
timer1 = new DispatcherTimer();
}
private void Metronome_Run(object sender, EventArgs e)
{
if (NoteValArray.Count > numIndex) // if we are not outside the list bounds
{
numVal = NoteValArray[numIndex]; // get the value at this list index and place it in numVal
numIndex ++; // increment
}
else { timer1.Stop(); return; }
// Load the mp3 indicated by numVal
if (numVal == 1) { mediaPlayer.Open(new Uri("1.mp3", UriKind.Relative)); }
else if (numVal == 2) { mediaPlayer.Open(new Uri("2.mp3", UriKind.Relative)); }
else if (numVal == 3) { mediaPlayer.Open(new Uri("3.mp3", UriKind.Relative)); }
else if (numVal == 4) { mediaPlayer.Open(new Uri("4.mp3", UriKind.Relative)); }
else if (numVal == 5) { mediaPlayer.Open(new Uri("5.mp3", UriKind.Relative)); }
mediaPlayer.Play(); // play whats loaded
}
private void Start_Button_Click(object sender, RoutedEventArgs e)
{
numIndex = 0;
timer1.Tick += Metronome_Run; // run this at each tick
timer1.Interval = TimeSpan.FromMilliseconds(10000 / (tempo / 6));
timer1.Start();
}
}
}
Solved!
I moved "timer1 = new DispatcherTimer();" from the mainwindow function, and placed it in the "Start_Button_Click" event with the other timer code.
My guess is I was starting more than 1 timer with each play press, and that was causing the bug. Thanks to all who took time to look at my problem.
Related
I have a listener that captures the location every 10 seconds or 100 meters or so. I am using
xam.plugin.geolocator
to implement the listener. My problem is the location listener is not working(meaning the changes in location were not capturing or saved in the location cache) when my application is minimized or the application is opened but the phone is locked.
Here is my code:
async Task StartListening()
{
if (!CrossGeolocator.Current.IsListening)
{
var defaultgpsaccuracy = Convert.ToDouble(Preferences.Get("gpsaccuracy", String.Empty, "private_prefs"));
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(10), defaultgpsaccuracy, false, new Plugin.Geolocator.Abstractions.ListenerSettings
{
ActivityType = Plugin.Geolocator.Abstractions.ActivityType.Other,
AllowBackgroundUpdates = true,
DeferLocationUpdates = true,
DeferralDistanceMeters = 1,
DeferralTime = TimeSpan.FromSeconds(1),
ListenForSignificantChanges = true,
PauseLocationUpdatesAutomatically = false
});
}
}
I place this code in the first view/page of my application in my login.xaml.cs
Here are my questions:
How can I implement the listener properly so that when the application minimized or the phone/device is locked it still captures the changes of location?
What is the best GPS settings I need to capture the changes in location faster and accurately? Right now, my current settings are capturing the location every 10 seconds or 100 meters.
First you need to init StartListening then create event handlers for position changes and error handling
public Position CurrentPosition { get; set; }
public event EventHandler PositionChanged;
Don't forget to init it in your constructor :
CurrentPosition = new Position();
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(20), 10, true);
CrossGeolocator.Current.PositionChanged += PositionChanging;
CrossGeolocator.Current.PositionError += PositionError;
Functions :
`private void PositionChanging(object sender, PositionEventArgs e)
{
CurrentPosition = e.Position;
if (PositionChanged != null)
{
PositionChanged(this, null);
}
}
private void PositionError(object sender, PositionErrorEventArgs e)
{
Debug.WriteLine(e.Error);
}`
You can now call PositionChanged when ever you want the latest position
Don't forget to stop listening :
`public async Task StopListeningAsync()
{
if (!CrossGeolocator.Current.IsListening)
return;
await CrossGeolocator.Current.StopListeningAsync();
CrossGeolocator.Current.PositionChanged -= PositionChanging;
CrossGeolocator.Current.PositionError -= PositionError;
}`
I am trying to get my animation to cycle through two images when a directional key is pressed. Currently it switches images on each key press. I have been looking at tutorials and understand that I need some kind of timer to measure each frame time but everything I have tried to implement in to my code so far has failed so I am just posting my code that works at the moment.
Could anyone explain to me how to go about implementing this please?
void Frog::up(sf::Event event)
{
sf::IntRect frogUpAnimation[iNumFrames];
frogUpAnimation[0] = sf::IntRect(13, 362, 21, 23);
frogUpAnimation[1] = sf::IntRect(46, 367, 21, 23);
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
{
audio.frogjumpsound();
frogSprite.move(0.0f, -55.0f);
iScoreCounter = iScoreCounter + 10;
iCurrentFrame++;
if (iCurrentFrame >= iNumFrames) iCurrentFrame = 0;
frogSprite.setTextureRect(frogUpAnimation[iCurrentFrame]);
}
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 800), "Frogger");
window.setFramerateLimit(60);
sf::Clock timer;
float fFrameTime = 1.0f / 60.0f;
float fElapsedTime;
sf::Event event;
Game game;
Frog frog;
Timer countdown;
Text text;
Audio gameaudio;
gameaudio.music();
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
if (event.type == sf::Event::KeyPressed)
{
game.processKeyPress(event.key.code);
}
frog.up(event);
frog.down(event);
frog.left(event);
frog.right(event);
} // Event loop
countdown.gametime();
fElapsedTime = timer.getElapsedTime().asSeconds();
if (fElapsedTime > fFrameTime)
{
timer.restart();
}
//Update
game.checkPads(&frog);
game.checkWin();
game.gameOver();
frog.scorecounter(&countdown);
frog.update(fElapsedTime);
game.update(fElapsedTime);
text.update(fElapsedTime);
countdown.update(fElapsedTime);
game.collision(&frog);
// Drawing
window.clear();
window.draw(game);
window.draw(frog);
window.draw(countdown);
window.draw(text);
window.display();
} // main loop
}
First, I would only call 'frog.up()', 'frog.left()', etc. when the proper key is pressed. Otherwise, you're calling potentially 4 functions each iteration which do absolutely nothing.
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) {
frog.right();
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) {
frog.left();
}
As for your timer issues, your fElapsedTime and fFrameTime look a little funky. Take a look at https://en.sfml-dev.org/forums/index.php?topic=10913.0 but I really recommend this guide: https://github.com/SFML/SFML/wiki/Source:-AnimatedSprite
I working on an UWP (Win10) App with a simple location picker function. The user can drag the map on the wanted location. A basic Pushpin thats always in the center of the Map window acts as the location indicator. It works just like the free location pick in WhatsApp.
To give the user feedback that he is moving the center pin, I want to raise the pin when the user is moving the map and lower it again on release.
Here the simple code to raise the pin (and manipulate the shadow):
private void MyMap_MapHolding(MapControl sender, MapInputEventArgs args)
{
iconSwitch = true;
if(iconSwitch == true) {
centerPin.Margin = new Thickness(0, 0, 0, 60);
centerPinShadow.Opacity = 0.3;
centerPinShadow.Width = 25;
}
But this event doesn't seem to be affected on click & hold or tap & hold. Am I missing something?
FYI: I tried this out with the MyMap_MapTapped(...) method, and it worked just fine, but I need it when the map is dragged not just tapped.
Chees!
I've tested and debugged, MapHolding event can't work by me either. For your purpose, CenterChangedLink event maybe helpful, I've tested it too.
Here is part of my sample code:
RandomAccessStreamReference mapIconStreamReference;
public Maptest()
{
this.InitializeComponent();
myMap.Loaded += MyMap_Loaded;
myMap.MapTapped += MyMap_MapTapped;
myMap.MapHolding += MyMap_MapHolding;
myMap.CenterChanged += MyMap_CenterChanged;
mapIconStreamReference = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/MapPin.png"));
}
private void MyMap_Loaded(object sender, RoutedEventArgs e)
{
myMap.Center =
new Geopoint(new BasicGeoposition()
{
//Geopoint for Seattle
Latitude = 47.604,
Longitude = -122.329
});
myMap.ZoomLevel = 12;
}
private void MyMap_MapTapped(Windows.UI.Xaml.Controls.Maps.MapControl sender, Windows.UI.Xaml.Controls.Maps.MapInputEventArgs args)
{
var tappedGeoPosition = args.Location.Position;
string status = "MapTapped at \nLatitude:" + tappedGeoPosition.Latitude + "\nLongitude: " + tappedGeoPosition.Longitude;
rootPage.NotifyUser( status, NotifyType.StatusMessage);
}
private void MyMap_MapHolding(Windows.UI.Xaml.Controls.Maps.MapControl sender, Windows.UI.Xaml.Controls.Maps.MapInputEventArgs args)
{
var holdingGeoPosition = args.Location.Position;
string status = "MapHolding at \nLatitude:" + holdingGeoPosition.Latitude + "\nLongitude: " + holdingGeoPosition.Longitude;
rootPage.NotifyUser(status, NotifyType.StatusMessage);
}
private void MyMap_CenterChanged(Windows.UI.Xaml.Controls.Maps.MapControl sender, object obj)
{
MapIcon mapIcon = new MapIcon();
mapIcon.Location = myMap.Center;
mapIcon.NormalizedAnchorPoint = new Point(0.5, 1.0);
mapIcon.Title = "Here";
mapIcon.Image = mapIconStreamReference;
mapIcon.ZIndex = 0;
myMap.MapElements.Add(mapIcon);
}
At first I thought, even when the MapHoling event can't work, the Tapped action before holding should handled by MapTapped event, but it is seems this action is ignored. So remember, if a user hold the Map but not move it, nothing will happen.
I am developing an application to play online radio via streaming. I have used MediaElement. But the problem is the player doesn't play in background. I mean as soon as I click on "start" or "back" button on the phone, the streaming as well as the audio stops. I have not tested it on any device, so please inform me if it does happen in simulator but not device. Here is my code..
private void Play()
{
if (mediaElement == null || mediaElement.CurrentState != MediaElementState.Playing)
{
if (SystemTray.ProgressIndicator == null)
SystemTray.ProgressIndicator = new ProgressIndicator();
SystemTray.ProgressIndicator.IsIndeterminate = true;
SystemTray.ProgressIndicator.IsVisible = true;
SystemTray.ProgressIndicator.Text = "Connecting to *********...";
mediaStream = new ********.RadioStream(uri);
mediaStream.StreamSetupComplete += (o, e) =>
{
Dispatcher.BeginInvoke(() =>
{
if (mediaElement != null)
{
LayoutRoot.Children.Remove(mediaElement);
}
mediaElement = new MediaElement();
mediaElement.Volume = 1.0;
LayoutRoot.Children.Add(mediaElement);
mediaElement.SetSource(mediaStream);
SystemTray.ProgressIndicator.IsVisible = false;
});
};
}
}
I want to know the steps to enable this to play in background. Atleast when the user presses "start" button, the audio streaming should not stop.
Also one more problem I have is I have added an ApplicationBarMenu in which I have an "Exit" button. As soon as the user clicks this button the streaming should stop and application should close itself. I am unable to close the application programmatically. Code is give below..
void exit_Click(object sender, EventArgs e)
{
if (playing)
{
MessageBoxResult Choice;
Choice = MessageBox.Show("Media is currently playing, do you want to stop it?", "Stop Player", MessageBoxButton.OKCancel);
if (Choice == MessageBoxResult.OK)
{
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(#"Images/play.png", UriKind.Relative));
play.Background = brush;
Stop();
playing = false;
try
{
// if (NavigationService.CanGoBack)
// {
// while (NavigationService.RemoveBackEntry() != null)
// {
// NavigationService.RemoveBackEntry();
// }
// }
}
catch
{
}
}
else
{
}
}
}
Please help me with the proper code. Even if there is any other way to stream media in background other than MediaElement, please suggest that too..
Hoping a reply soon. Thanks to all in advance.
You must use BackgroundAudioPlayer for this.
You should take a look at Microsoft.Phone.BackgroundAudio Namespace too.
Hello everyone I have been following this tutorial here http://www.gogo-robot.com/2011/05/30/xna-skinned-model-animations/ and so far its great got the animations playing and everything, but now I want to expand it and stop the continuous loops say for instance i press the a key to make the model jump when i release the a key i want him to stop jumping but if i hold the a key i want him to keep jumping. Here what i have tried so far
and none of it works.
I am stumped here on how to do this thanks for any help with this.
private void HandleInput(GameTime gameTime)
{
currentGamePadState = GamePad.GetState(PlayerIndex.One);
// Check for changing anims
//SkinningData skinningData = model.Tag as SkinningData;
SkinningData sd = jumper.model.Tag as SkinningData;
if (currentGamePadState.Buttons.A == ButtonState.Pressed)
{
if (jumper.animationPlayer.CurrentClip.Name != "Fire")
jumper.animationPlayer.StartClip(sd.AnimationClips["Fire"]);
}
if (currentGamePadState.Buttons.X == ButtonState.Pressed)
{
if (jumper.animationPlayer.CurrentClip.Name != "DieF")
jumper.animationPlayer.StartClip(sd.AnimationClips["DieF"]);
}
//does not work
if (currentGamePadState.Buttons.X == ButtonState.Released)
{
if (jumper.animationPlayer.CurrentClip.Name == "DieF")
jumper.animationPlayer.StartClip(sd.AnimationClips["Idel"]);
}
if (currentGamePadState.Buttons.Y == ButtonState.Pressed)
{
if (jumper.animationPlayer.CurrentClip.Name != "Idel")
jumper.animationPlayer.StartClip(sd.AnimationClips["Idle"]);
}
//does not work
if (jumper.animationPlayer.CurrentTime == jumper.animationPlayer.CurrentClip.Duration)
{
//set him back to idel
jumper.animationPlayer.StartClip(sd.AnimationClips["Idle"]);
}
I have tried these configuration with no luck in the game
// Starts playing the entirety of the given clip
public void StartClip(string clip, bool loop)
{
AnimationClip clipVal = skinningData.AnimationClips[clip];
StartClip(clip, TimeSpan.FromSeconds(0), clipVal.Duration, loop);
}
// Plays a specific portion of the given clip, from one frame
// index to another
public void StartClip(string clip, int startFrame, int endFrame, bool loop)
{
AnimationClip clipVal = skinningData.AnimationClips[clip];
StartClip(clip, clipVal.Keyframes[startFrame].Time,
clipVal.Keyframes[endFrame].Time, loop);
}
// Plays a specific portion of the given clip, from one time
// to another
public void StartClip(string clip, TimeSpan StartTime, TimeSpan EndTime, bool loop)
{
CurrentClip = skinningData.AnimationClips[clip];
currentTime = TimeSpan.FromSeconds(0);
currentKeyframe = 0;
Done = false;
this.startTime = StartTime;
this.endTime = EndTime;
this.loop = loop;
// Copy the bind pose to the bone transforms array to reset the animation
skinningData.BindPose.CopyTo(BoneTransforms, 0);
}
Can you not attach a bool on the animation clip to tell it to play only once, or an active variable that can be called.