Playlist Create and PLay in C# - c#

So I am trying to add a music player to my web browser so users can play music in it from a specific folder in it's app data folder ("\AppData\Roaming\MantouWeb360\MWP360")
I was looking around a came accross a solution here
The user can select files (multiple ones too) which is then added to a playlist and then played.
However I am not so experienced at C# and VS doesn't recognise the wmp.playlistCollection.newPlaylist("myplaylist"); part of the code (and the following code using the wmp. part). This must mean there was code outside the example defining wmp. but I do not know what it is. Any way to get this code to work so that users can play music from their selected files on my web browser or is there possibly a better way which copies the music to the folder I said above and plays them in order (loop at the end of the last file) so that the user does not need to keep selecting files?
WMPLib.IWMPPlaylist playlist = wmp.playlistCollection.newPlaylist("myplaylist");
WMPLib.IWMPMedia media;
if (ofdSong.ShowDialog() == DialogResult.OK)
{
foreach (string file in ofdSong.FileNames)
{
media = wmp.newMedia(file);
playlist.appendItem(media);
}
}
wmp.currentPlaylist = playlist;
wmp.Ctlcontrols.play();

Related

loop optimization in c#

I am working on a music player for pc using c# and all seems to go fine but, i have problem in loading all the music files from the music directory because it loads very slowly and this take time when opening the app, it could take 5 min depending on the amount of music files. I think this happens because i created a loop to loop through each music file and get the metadatas and also the picture to load on different picture boxes for each music file.
Please Help, i need it to be faster. Thank you.
the code is below...
public List<MusicDetails> Music_Library()
{
List<MusicDetails> files = new List<MusicDetails>();
string[] musicfolder = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic),"*mp3", SearchOption.AllDirectories);
for (int i = 0; i <musicfolder.Length; i++)
{
try {
files.Add(new MusicDetails
{
title = TagLib.File.Create(musicfolder[i]).Tag.Title,
genre = TagLib.File.Create(musicfolder[i]).Tag.FirstGenre,
artist = TagLib.File.Create(musicfolder[i]).Tag.FirstPerformer,
path = musicfolder[i],
CoverArt = OrganiseAlbums.SingleAlbumImage(musicfolder[i],true)
});
}catch(Exception)
{
// OMIT FILE
}
}
return files;
}
You could try replacing your loop with a parallel foreach loop using background threads - add each item to the UI as it is processed, and let .Net determine the most efficient way to process everything. If you do it right, your UI will remain responsive, and your users will start out with "enough to look at..." Here is a link to get you started:
https://msdn.microsoft.com/en-us/library/dd460720(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
If you have a ton of music files, though, you may not want to load them all into memory at once. I would look into some sort of a list control or layout control that allows virtualization so that you instantiate and display only the visible ones at any given time. Use a tutorial to see how to bind to your collection so that it is virtualized.
If you post a more specific question with examples of what you have tried, you will get more specific answers...

How to get the music folder path in xamarin.android?

I'm trying to save a mp3 file, into the device's music library folder.(I Want it to show up immediately as a playable song).
More clearly i'm looking for the path /storage/emulated/0/Music/, in each phone.Im not sure if this path changes, so i would rather not take the risk.
I have tried this paths:
System.Environment.SpecialFolder.CommonMusic -> Blank
System.Environment.SpecialFolder.MyMusic -> /data/user/0/App.App/files/Music
Android.OS.Environment.DirectoryMusic -> /Music
None of this offer, what i wanted.I tried writing to the /storage/emulated/0/Music/ which did the job, but i dont think this path is stable.
Anyone knows how can i get the music folder path programmatically ? Im coding in C# using xamarin.android.
I used this code to get access to my Music Folder
public void StartPlayer(String filePath)
{
string Mp3Path = Path.Combine("/sdcard/"+
Android.OS.Environment.DirectoryMusic,
filePath);
if (player == null)
{
player = new MediaPlayer();
}
player.Reset();
player.SetDataSource(Mp3Path);
player.Prepare();
player.Start();
}
The sdcard shortcut works great for me, hope it works for you too ^^

C# Duration or Length of a media file (Metro)

I'm trying to create a media player for the Windows Store. I am using MediaElement to play the file, but I would also like to be able to load multiple songs/videos in a playlist and to display the title of the media file and the length of the file in seconds.
Something like that:
TimeSpan duration = GetFileDuration(mFile.PathToFile);
or
int durationOfFileInSeconds = GetFileDuration(mFile.PathToFile);
How can I do that ? I tried a lot of methods but they don't work because I'm trying do develop for Windows Store. I tried:
System.Windows.Media.MediaPlayer
Microsoft.WindowsAPICodecPack
Shell() and GetDetailsOf()
many other solutions that I find online
I've also tried to create a new page with a MediaElement just to load files in the MediaElement but the MediaElement.MediaOpened or the MediaElement.MediaFailed events are never fired and while ((mPlayer.NaturalDuration.TimeSpan == new TimeSpan())) ; would run indefinitely.
Really need some help with this. I've seen media players on the Windows Store that can display the duration of the file, so it's possible. I just don't know how.

How to Get a Specific Song From the Phone's MediaLibrary? And optimising the code

In my application I have the following code, which get's the phone's MediaLibrary and filters out the correct chosen Song from the MediaLibrary.Songs and plays it:
using (MediaLibrary library = new MediaLibrary())
{
foreach (var item in library.Songs)
{
if (item.Name == songName)
{
FrameworkDispatcher.Update();
MediaPlayer.Play(item);
}
}
library.Dispose();
}
However this takes quite a while and results in a pause. Is there a faster/more-efficient way to access a specific Song from the phone's MediaLibrary.Songs?
Thanks for your help.
Maybe you have to store the values in a XML document after you read the directory, this can save you a lot of time if the library doesn't get changed (Check the size of the whole library). If it does then you can check the library again and update your XML.
Well this is not an efficient way at all.
To find any song in MediaLibrary use LINQ.
Example
MediaPlayer.Play(library.Songs.First(x=>x.Name == songName));

How do you clear a windows media player playlist programmatically c#

My program uses windows media player in C# so that I can play almost any sound file. Some sounds need to play in succession, one after the other. The only way I could think to do that was with playlists, but I can't figure out how to clear the playlist between calls to the windows media player.
How can I play sounds, one after the other, in windows media player, without running into problems from previous playlists?
Thanks
My code:
WMPLib.IWMPPlaylist playlist = axWindowsMediaPlayer1.playlistCollection.newPlaylist("myplaylist");
WMPLib.IWMPMedia media;
foreach (string question in questionURL)
{
media = axWindowsMediaPlayer1.newMedia(question);
playlist.appendItem(media);
media = axWindowsMediaPlayer1.newMedia(answer);
playlist.appendItem(media);
axWindowsMediaPlayer1.currentPlaylist = playlist;
axWindowsMediaPlayer1.Ctlcontrols.play();
axWindowsMediaPlayer1.playlistCollection.remove(playlist);\\one of my many failed attempts at clearing the playlist. It doesn't throw an error, it doesn't clear the playlist either.
}
See PlaylistCollection.remove().
So I think you could remove the playlist with:
axWindowsMediaPlayer1.playlistCollection.remove("myplaylist");
Then simply create it again:
playlist = axWindowsMediaPlayer1.playlistCollection.newPlaylist("myplaylist");
This is the exact solution if you want to clear your playlist.
playlist.clear();
axWindowsMediaPlayer1.currentPlaylist = playlist;

Categories