使用Window 7的新特性开发的mp3播放器

Player.png

Introduction

In this article, we will see how we can develop an MP3 player, using C#.NET, which will use some of the new features of Windows 7.

We will show the progress of the song being played using the "Progress bar" in Taskbar. We will also show the Album art of the song as the "Thumbnail" and we will also have a Thumbnail Toolbar which has the Play/Pause, Next and Previous buttons.

Prerequisites

  1. To play MP3 songs, we will be using the BASS Audio Library.
  2. We will also need Bass.net which is the .NET API for BASS.
  3. TagLib Sharp: We will use this library to read tagging information about the song being played.
  4. Windows® API Code Pack for Microsoft® .NET Framework

Developing the Application

The Player

  1. Create a new project in Visual Studio
  2. Add the existing projects Shell and Core which can be found under the WindowsAPICodePack/shell and WindowsAPICodePack/core folders
  3. Add a reference to the above projects and import the following namespaces:
    • Microsoft.WindowsAPICodePack.Dialogs
    • Microsoft.WindowsAPICodePack.Taskbar
    • Microsoft.WindowsAPICodePack.Shell

The first part of the development is developing the player which will have the basic functionality like Play/Pause, Stop, next Previous/Next and Add/Remove songs Buttons.  The main form in our application has 7 Buttons, A listbox, Label and a Picture Box.

We will use the Picture Box to display the Album Art and we will use the Label to display the seconds remaining while a song is being played.

Note: Make sure you have added references for Core, Shell, Bass.net and taglib-sharp. Also add bass.dll to your project (Make sure you have set "Copy to Output Directory" as "Copy Always".)

Player Class: This class has all the methods which we will be using to play MP3 files. The first step is to initialize an output device. We use Bass.BASS_Init() for this. Following are the parameters:

  1. -1 denotes default device.
  2. 44100 is the frequency.
  3. BASSInit.BASS_DEVICE_DEFAULT denotes th default device to use
  4. System.IntPtr.Zero denotes the application's main window which is the current foreground window.

Before playing a song, we must first create a stream. We use BASS_StreamCreateFile() to create a stream from a file. We then use BASS_ChanelPlay() to play the stream which we created in the LoadSong() function.

Song Length and Album Art

Once the player is ready, we will use Taglib sharp to get the length of the song and album art. We will use the picture box to display the album art. We will also use this picture box's location and size to create the Thumbnail. 

Once we create the TagLib file from a file (in this case, the selected item in the playlist listbox), we will use Properties.Duration.TotalSeconds to get the length of the song in seconds. We will be using this value to populate the Label and the set the progressbar value.

We will also be using taglib sharp to get the album art of the song which is being currently played. Some songs may not have an Album art so we will use TagLibfile.Tag.Pictures.Length to get the numbers of pictures associated with the song. If it is more than zero, then we load the picture box with the album art.

Progressbar, Thumbnail Toolbar and Player Buttons

Create Taskbar buttons and instance of TaskbarManager.

This is the main function which will play the song. First we call the SetAlbumArt() function which will set the album art. We use SetTaskBarthumbnail() function to the set the thumbnail. Explanation for this function comes later in this article. We will use GetSongLength() and SetTimerforPlay() to get the song length and activate the timer respectively.

 

 

In the below function, we first check whether the song is paused. We use the value of playState for this. Value zero means the song is paused and we use the PlaySong() function to continue playing the song. If the value is not zero, then it denotes that the song should be played from the beginning. In this case, we load the song and then play it.

 

 

This function is called just before playing the song to enable the timer and set the progress bar style. TaskbarProgressBarState.Normal denotes that the progress will be normal. Timer's interval is set as 1000 ms.

This is similar to SetTimerforPlay() but we will disable the timer to stop the progress in the progress bar. At the same time, we will also set its style to TaskbarProgressBarState.Error so that the color is set as Red. This function is called when the song is paused.

We will be using TabbedThumbnail.SetThumbnailClip to set the thumbnail for our MP3 player. For this, we create a new Rectangle selection inside the main form and then set it as the thumbnail clip. Our motive is to use the Album art which we have already loaded in the Picture Box. Note: I had to make some adjustments in the Height and Width of the rectangle selection to get a proper thumbnail.

Thumbnail Toolbar should be created during the Form's Shown event. ThumbnailToolbarButton accepts two parameters:

  1. Icon: The Icon to be used for the button.
  2. Tooltip: The tool tip for the button.

We also add an Event Handler for each of the Button's Click events.

Thumbnail Toolbars : Play/Pause, Next and Previous Buttons

 

Progressbar

Timer's interval is set to 1000ms and it will be enabled as soon as the play button is pressed. We use the SongLength to set the maximum value of the progressbar. and increment the progress value every second. Timer will be disabled as soon as the pause button is pressed.

Player in Action

Play

playing.png

Thumbnail

thumbnail.png

Thumbnail while Paused

ThumbnailPaused.png

Thumbnail while Playing

ThumbnailPlay.png

Progressbar while Paused

paused.png

Conclusion

This is a basic MP3 player which was developed in few hours. The idea was to use progress bar differently. Comments and suggestions are always welcome.

 

本文转自codeproject. 希望大家转载时把出处注明。

References and Useful Links

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值