When a few days ago I started preparing for writing a new article for a
training series , I wanted to write a simple lesson on playing multimedia files. Taking the
MSDN material as a basis, I
translated it (it is not too complicated), but I wanted something more interesting. And here I come across the article
Windows Phone tip of the day: MediaLibrary , which caught my attention.
When I tried to play music in the manner described, I ran into a non-working example. I accidentally managed to get the example to work. Below is a free translation of the article with the solution to my problem.
Probably, not all developers know that, in the system supplied in the Windows 7 emulator, there are also several built-in music files besides the pictures. More precisely, they are three:
- Another Melody Song (00: 00: 31.3930000) by Windows Phone Artist [Pop]
- Melody Song (00: 00: 30.4640000) by Windows Phone Artist [Rock]
- Rhythm Variation (00: 00: 15.3710000) by Windows Phone Artist [R & B]
Small retreat
Since I use the
unlocked version of the emulator , it is easy for me to verify the validity of this statement. Simply select the
Music + Video hub and listen to all the tunes. I have plans to prepare an article about the possibilities that the unlocked version provides, including navigation through folders and the system registry.
')
Get a list of songs
But back to the music. We can independently get a list of music that is in the library, as well as additional information (duration of the song, the author, etc.). To do this, first add a link to Microsoft.Xna.Framework and then write the following code:
<code>
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
private void buttonGetSongs_Click (object sender, RoutedEventArgs e)
{
FrameworkDispatcher.Update ();
MediaLibrary lib = new MediaLibrary ();
foreach (Song s in lib.Songs)
{
Debug.WriteLine (s.Name + "" + s.Duration + "" + s.Artist);
}
}
</ code>
By running the project and pressing the button, we get a list of songs contained in the library, which is listed above. There were no problems. Go ahead.
Reproduction
Just get a list of music is not interesting, I want to listen to it. Let's use an instance of the MediaPlayer class.
<code>
// 1 option
// Background music
MediaPlayer.Play (lib.Songs [0]);
</ code>
Here I was waiting for the first surprise. When I pressed the button for playback, the program was closed with an error, but the music was played and sounded by itself in the background, without interfering with switching between other applications. A little later, you will understand how to avoid the appearance of an error.
Reproduction. Second way
The author of the original article also led the full paths to the files that are in the library.
- My Documents \ Zune \ Content \ 0100 \ 00 \ 11.wma (Melody Song)
- My Documents \ Zune \ Content \ 0100 \ 00 \ 15.wma (Another Melody Song)
- My Documents \ Zune \ Content \ 0100 \ 00 \ 17.wma (Rhythm Variation)
Then I decided to conduct independent research and play music by file names. Here other surprises awaited me. First, the code that looked right for me.
<code>
// Option 2
// List of available music
// My Documents \ Zune \ Content \ 0100 \ 00 \ 11.wma (Melody Song)
// My Documents \ Zune \ Content \ 0100 \ 00 \ 15.wma (Another Melody Song)
// My Documents \ Zune \ Content \ 0100 \ 00 \ 17.wma (Rhythm Variation)
MediaPlayerLauncher playerlauncher = new MediaPlayerLauncher ();
playerlauncher.Media = new Uri (@ "\ My Documents \ Zune \ Content \ 0100 \ 00 \ 17.wma", UriKind.RelativeOrAbsolute);
playerlauncher.Location = MediaLocationType.Install;
playerlauncher.Controls = MediaPlaybackControls.Pause | MediaPlaybackControls.Stop;
playerlauncher.Show ();
</ code>
The example is very simple. I wanted to call the built-in player through MediaPlayerLauncheer (I wrote about the tasks of execution
here ) and pass him the finished path to the file. The first thing that confused me was the absence of a backslash at the beginning of the journey from the author of the article. Having experience in developing for Windows Mobile, I remembered that all the paths began with a slash (\). My guess was right. Without a slash, the program fell out with an error. However, with the slash, the situation did not look much better. The error did not appear, but the music was not played. A little upset, I left the program and just started clicking on the icons of the programs installed in the emulator. And accidentally clicked on the icon of his program, and then on the play button. And, lo and behold, the music sounded! Thus, I first encountered the situation of different behavior of a program running from Visual Studio or from an emulator.
Then I went back to the first example and tried the same method. First, I launched the program through Visual Studio and immediately closed it. And then in the emulator I launched my program again. Everything worked.
Finally, I also wanted to try the option of playing music through MediaElement. Alas, to no avail. I give the code just in case. Maybe some of the professionals will be able to understand this problem.
<code>
// Uri u = new Uri (@ "\ My Documents \ Zune \ Content \ 0100 \ 00 \ 15.wma");
// MediaElement mymedia = new MediaElement ();
//mymedia.Source = u;
//mymedia.Play ();
</ code>
Happy programming!
PS Pictures will not, because there is nothing to show.