I am working on an app that plays music. Now, I have a function in my main class that chooses a random new song and plays said song:
private void ChooseRandomNewSongAndPlay(bool songHasCompleted)
{
Random rnd = new Random();
int rndValue = rnd.Next(0, Mp3ObjectSmall.Count());
int currentPos = 0;
if (!songHasCompleted)
{
currentPos = mediaPlayer.CurrentPosition; // if song infact has completed, reset position to else save current position (when next has been pressed)
}
WriteSeekingToDataBase(currentPos, CurrentSongObject);
mediaPlayer.Stop();
if (Android.Net.Uri.Parse(CurrentSongObject.Mp3Uri) != null)
{
PhotoAlbumAdapter.OldSongUri = Android.Net.Uri.Parse(CurrentSongObject.Mp3Uri);
}
PhotoAlbumAdapter.NewSongUri = (Android.Net.Uri.Parse(Mp3ObjectSmall[rndValue].Mp3Uri));
PlayMusic((Android.Net.Uri.Parse(Mp3ObjectSmall[rndValue].Mp3Uri)));
}
But I also set up a broadcast receiver, so when the user is in his car and clicks on next song from the car stereo, I also want the above function to be played. But here is the problem:
I cannot make this above function public static
since it calls other non static functions. I would have to make those static too, but that would cause many, many other errors and is not a good solution at all I believe.
Also, I cannot create a new object of my main class in within the broadcast receiver as such: class xy = new class()
. I cannot do that, because that would also create another object of my mediaplayer object, but this object needs to be the same to skip to a next some. If it isnt, just anoither song is played on top of the first song which of course is also not good.
Lastly, I cannot just hand over the class as a parameter to the constructor of the broadcast receiver. I am getting told then that the braodcast receiver needs to have a "standart constructor" so I cannot alter the parameters.
Unfortunately, these 3 optiones are all I believe I have and neither seems to work. What I really, really do not want to do is to copy paste all functions from my main class into the broadcast receiver for obvious reasons.
Can you guys help me out here? Thank you!