Information in this document is subject to change without notice. The example companies, organizations, products, people, and events depicted herein are fictitious. No association with any real company, organization, product, person or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarked, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.
© Microsoft Corporation. All rights reserved.
Microsoft, MS-DOS, MS, Windows, Windows NT, MSDN, Active Directory, BizTalk, SQL Server, SharePoint, Outlook, PowerPoint, FrontPage, Visual Basic, Visual C++, Visual J++, Visual InterDev, Visual SourceSafe, Visual C#, Visual J#, and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.
Other product and company names herein may be the trademarks of their respective owners.
This source code is subject to terms and conditions of the Apache License, Version 2.0. A copy of the license can be found in the License.html file at the root of this distribution. If you cannot locate the Apache License, Version 2.0, please send an email to ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound by the terms of the Apache License, Version 2.0.
IronTunes
A music player built in IronPython
Contributor: Brad Dodson
Prerequisites
The prerequisites to successfully run IronTunes are:
Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1 can be used in place of the
Visual Studio 2008 installation.
IronTunes is a music player built upon the IronPython framework and utilizes several Windows libraries to play music files. For example, COM Interop is used to connect to the DirectShow API which in turn plays the actual files. This simply means that we do not have to implement the code to convert audio files to actual sounds ourselves. Additionally, IronTunes uses a Windows Presentation Foundation based graphical user interface. One of the major benefits of using WPF from IronPython is that it makes writing GUIs very easy.
IronTunes comes with two separate GUIs to showcase that you
can design UI independently from the code. To see this, save a copy of
CurrentSkin.xaml, and copy NewSkin.xaml over CurrentSkin.xaml. Then
re-launch IronTunes. NewSkin.xaml adds a seek slider and images for the
buttons. Use the thinner plus-sign button to add an individual file and
the thicker plus-sign button to add an entire directory.
To start IronTunes, run "ipy.exe IronTunes.py" from
the IronTunes directory. Add files by clicking the "Add File" or "Add
Dir" buttons. The files you add will show up in the library view below.
You can select a file and click "Play/Pause" to start playing it.
Typing in the search box in the top right will filter the library as
you type.
The other features work pretty much as expected: Back, Forward, and
Volume Control. Information about the currently playing track is
displayed at the top in the center.
IronTunes uses quartz.dll (in the system32 directory) by
generating an interop assembly with the tlbimp.exe tool. See the
IronPython tutorial for more info. The Player object (player.py)
constructs a FilegraphManagerClass object from quartz.dll via the interop
assembly QuartzTypeLib.dll. Player calls into the FilegraphManagerClass
object to play the audio files. The Player object calls the RenderFile method
with a filename to setup the filter graph to render the file. Then it
can call the Run (to play), Stop, and Pause methods. The Player object also registers
the program's main window with the filter graph manager to send
notification messages. In order to handle these there is an extra wndproc hooked into the main window which sends notifications to the
player object when it needs to respond to filter graph events (it calls
the ProcessEvents method). This allows the program to advance to the
next item on the playlist when an item finishes playing.
For this example
to run, it is
required that you create the quartz (i.e., DirectShow) interop assembly
on your own by following these simple
steps:
Open the "Visual Studio 2008 Command Prompt" from the start menu.
Change the current directory to Samples\IronTunes.
Use the tlbimp tool to generate the interop assembly for quartz (tlbimp writes to the current directory):
tlbimp
%SystemRoot%\System32\quartz.dll
C:\IronPython\Samples\IronTunes>tlbimp
%SystemRoot%\System32\quartz.dll
Microsoft (R) .NET Framework Type
Library to Assembly Converter
2.0.50727.42
Copyright (C) Microsoft
Corporation. All rights reserved.
Type library imported to
QuartzTypeLib.dll
IronTunes builds a new WPF window and loads the XAML file CurrentSkin.xaml. It then sets the window's DataContext to be the application's dataModel, so that the bindings in the XAML file can allow the user interface to reflect the program's state. It also creates an IronTunesWindow object (IronTunesWindow.py) and uses its enliven method to attach event handlers to the buttons.
The Library class (library.py) manages the current music library. It implements the functionality of adding files and directories (recursively) to the current library, and allows the current library view to be filtered and sorted (by a SearchFilter object implemented in SearchFilter.py).