how to play specific song BackgroundAudioPlayer windows phone? - c#

Hi i'm trying to play a specifc song for example if i have a list with 100 songs i want to play the song #20
to do this i use the following code
private void playSong(int n)
{
IsolatedStorageSettings.ApplicationSettings["currentSong"] = n;
if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing)
{
BackgroundAudioPlayer.Instance.Close();
BackgroundAudioPlayer.Instance.Play();
}
else
BackgroundAudioPlayer.Instance.Play();
}
And then in my AudioPlayer.cs i get the song
//get the song
currentSong = (int)IsolatedStorageSettings.ApplicationSettings["currentSong"];
It works fine but it takes too much time to load the song.
so please is there any other way to do this?
thanks.

Related

How to play previously played songs in a list?

I use the Fisher–Yates shuffle for my music-player, however when you play the previous song it's also a random song from the list. So, I added a list that saves the played songs PlayedSongs. It remembers all songs that were played. How do I play these songs backwards and forwards? If I would go back 3 songs, and then go forward again, it would still be the song that was on number 2. Yet thinking of this algorithm gives me a headache and I can't get it right. Here's what I have.
Again to be clear: I want a shuffle that can play forwards and backwards. When playing backwards all the songs had to be played recently in order. (It's temporary so this list gets cleared on exit).
This if-statement is from another method that normally plays songs and checks if Shuffle is enabled:
//after song was played
if (ShuffleIsOn)
{
ShuffledSongIndexes.Remove(ShuffledSongIndexes.FirstOrDefault());
PlayedSongs.Add(song);
}
Then:
public List<Song> PlayedSongs = new List<Song>();
public int CurrentShuffleIndex = 0;
public void PlayPreviousSongs()
{
PlayedSongs.Reverse();
CurrentShuffleIndex++;
try
{
MediaElement.Source = new Uri(PlayedSongs[CurrentShuffleIndex].FullFileName);
}
catch (ArgumentOutOfRangeException)
{
MediaElement.Source = new Uri(PlayedSongs[CurrentShuffleIndex - 1].FullFileName);
}
MediaElement.Play();
PlayedSongs.Reverse();
}
public void PlayNextSongs()
{
CurrentShuffleIndex++;
MediaElement.Source = new Uri(PlayedSongs[CurrentShuffleIndex].FullFileName);
MediaElement.Play();
}
I haven't found anything on the internet about this, does anyone know how to do this?
Thanks
There is no need to reverse the list. Add a bool like: PlayForward, and if it's true then index will increment by 1, otherwise decrement by 1, like:
CurrentIndex += (PlayForward ? 1 : -1)
Also, there is a way u can make ur life easier. Don't change the song whenever u change the index. Instead, combine the process like:
int _CurrentIndex;
public int CurrentIndex
{
get => _CurrentIndex;
set
{
if (value != _CurrentIndex)
{
_CurrentIndex = value;
MediaElement.Source = new Uri(PlayedSongs[CurrentIndex].FullFileName);
MediaElement.Play();
}
}
}
Don't forget to check the bounds too!

How can i prevent a stackoverflow exception in my software?

I have an audio player that loads a configuration file with a list of files to be played at certain intervals. Like 5 files should be looped between 09:30 and 11:30 - it is an application for libraries where they need the background music.
When im outside of an interval, i loop through all my time intervals (there might be 2 hours before next interval starts) and then if i find that nothing is to be played right now - i sleep for a second and then try again.
I have a problem where i get a stack overflow after around 3-4 hours of checking for these time intervals (often at nights, because the clients are on, but no music should be playing at night).
private void PlayNextFile()
{
Application.DoEvents();
if (currentState == PlayerState.Started)
{
//if we find the next slide then play it
AudioSlide slideToPlay = FindNextSlide();
if(slideToPlay != null)
{
PlaySlide(slideToPlay);
}
else
{
MedianLog.Log.Instance.LogDebug("Did not find a slide to play now, will check again in a second");
System.Threading.Thread.Sleep(1000);
PlayNextFile();
}
}
}
Any idea on how to get around this exception?
You just filling up your stack with not controlled recursive calls.
A while loop would be a better solution.
Something like that should do it correctly:
private async void PlayNextFile()
{
while (true)
{
if (currentState == PlayerState.Started)
{
//if we find the next slide then play it
AudioSlide slideToPlay = FindNextSlide();
if (slideToPlay != null)
{
PlaySlide(slideToPlay);
return;
}
MedianLog.Log.Instance.LogDebug("Did not find a slide to play now, will check again in a second");
await Task.Delay(1000);
}
else
{
return;
}
}
}

Playing a list of songs in combobox one by one in C#

I have a list of songs in a combobox. I am playing the song based on the selection if user. Here is my code.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
wplayer.controls.pause();
song = comboBox1.SelectedItem.ToString();
index1 = comboBox1.SelectedIndex;
pictureBox4.Image=Image.FromFile(Application.StartupPath + "\\Input\\zeneszek.gif");
pictureBox4.SizeMode=PictureBoxSizeMode.StretchImage;
pictureBox4.Visible=true;
wplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wplayer_PlayStateChange);
wplayer.URL = Application.StartupPath + "\\Input\\" + song + ".mp3";
wplayer.controls.play();
WMPLib.WMPPlayState playstate = WMPLib.WMPPlayState.wmppsMediaEnded;
label4.Text = "Playing" + song;
}
public void wplayer_PlayStateChange(int newstate)
{
if (newstate == (int)WMPLib.WMPPlayState.wmppsMediaEnded)
{
playstate = WMPLib.WMPPlayState.wmppsMediaEnded;
song = comboBox1.Items[index1++].ToString();
}
}
After the end of the song I wanted to play the next song which is the next one in the combobox selection. Can someone help on this? I tried playstate change event also. Still I am not able to play the next song. I increased the index of the combobox when the play state is wmppsMediaEnded. When do I need to play that song? I tried playing the song when the state is wmppsStopped and wmmppsTransitioning but in both the cases I am able to play only two songs continuously. after the end of the 2nd song I am not able to play the remaining.
Use the PlayStateChangeEvent to check for newstate showing that the media has ended, then move the selectedindex + 1 and play the new song.
https://msdn.microsoft.com/en-us/library/windows/desktop/dd564079%28v=vs.85%29.aspx
This also might be some help to you: http://stereoclood.codeplex.com/SourceControl/latest#SteroMood/MediaPlayers/WmpMediaPlayer.cs

Adding two music sources in WP7 app

In my WP7 gaming app , I want two music files to run. One is the background music and another follows the user action , say, user kills the enemy. I am using MediaElement to do this. I am facing two issues.
1) How to loop background music ?
2) As soon as second music starts, the first music stops and does not start back when the second music stops. I do not want background music to stop, they should overlap. How to do this ?
I am using silverlight.
XAML
<MediaElement x:Name="stroke" AutoPlay="False" />
<MediaElement x:Name="bmusic" AutoPlay="True" />
C#
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains("bm"))
{
string hy=(string)settings["bm"];
//check if user has disabled music play
if (hy == "1")
{
bmplay = 1;
// play background music
bmusic.Source = new Uri("bmusic.mp3", UriKind.Relative);
bmusic.Play();
}
else
{
bmplay = 0;
}
}
else
{
bmplay = 1;
// play b music
bmusic.Source = new Uri("bmusic.mp3", UriKind.Relative);
bmusic.Play();
}
if (NavigationContext.QueryString.TryGetValue("msg", out msg))
{
// textBox1.Text +=" "+ msg;
find_move();
}
}
For repeating music you can use this:
Song s = Song.FromUri("song", new Uri(path));
FrameworkDispatcher.Update();
MediaPlayer.IsRepeating = repeat;
MediaPlayer.Play(s);
And try to use SoundEffect for sounds, they can be played simultaneously with background music.
Sound effect in windows phone 7

Stop playing sound called via SoundEffect.FromStream

I have some Windows Phone 7 code that starts playing a sound using SoundEffect.FromStream. I am using this instead of a normal media object as I need multiple audio clips to be on the one page.
However, based on certain external events I want to stop a particular sound playing. As sounds played via Open Stream are "play and forget" I cannot work out how to reference this playing sound and stop it.
public void PlaySoundCircus(object sender, RoutedEventArgs e)
{
if (audioPlaying == false)
{
using (var stream = TitleContainer.OpenStream("circus.wav"))
{
var effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
}
audioPlaying = true;
}
}
You need to create a SoundEffectInstance which will store a reference to your SoundEffect. The SoundEffectInstance has a Stop method which can be called.
SoundEffectInstance seiCircus;
using (var stream = TitleContainer.OpenStream("circus.wav"))
{
var effect = SoundEffect.FromStream(stream);
//create the instance
seiCircus = effect.CreateInstance();
FrameworkDispatcher.Update();
//play sound via the instance
seiCircus.Play();
}
//some event called to stop sound
seiCircus.Stop();

Categories

Resources