Now That 39;s What I Call Music Vol. 9

1 view
Skip to first unread message

Destiny Olatunji

unread,
Jul 25, 2024, 3:44:24 AM7/25/24
to PostGIS Users

Back in the old days of MS-DOS, programmers could implement a crude form of task-switching with a technique called Terminate and Stay Resident (also known as TSR). TSR programs installed hooks into keyboard interrupts or other OS mechanisms and then terminated, leaving the program in memory ready to kick into action when the user pressed a particular key combination or something else of interest happened.

now that 39;s what i call music vol. 9


Download Zip --->>> https://byltly.com/2zNlrG



Included with the downloadable code for this article is a Visual Studio solution named SimpleBackgroundAudio. For purposes of clarity, this program contains just about the minimum amount of code necessary to get background audio to work. I created the solution from the New Project dialog box by specifying Windows Phone Application, and then Windows Phone 7.1. (The 7.1 designation is used internally within the Windows Phone OS and Windows Phone applications, but it means the same thing as the more common 7.5 designation.)

The BackgroundAudioPlayer class is somewhat similar to the MediaElement or MediaPlayer classes that play audio files in the foreground. BackgroundAudioPlayer has no constructor; instead you obtain the only instance with the static Instance property. In this code, the Track property is set to the AudioTrack object just created.

Very important: Create a reference from the application to this DLL! To do this, I right-clicked the References section under the SimpleBackgroundAudio project, selected Add Reference, and then in the dialog box I selected the Projects tab and then the SimpleAudioPlaybackAgent project.

Look at the OnPlayStateChanged override first. This method is called when the PlayState property of the BackgroundAudioPlayer changes. The first argument is the same BackgroundAudioPlayer referenced in the program code; the last argument is a member of the PlayState enumeration.

When the program sets the Track property of the BackgroundAudioPlayer in the Click event handler, the BackgroundAudioPlayer accesses the music file over the Internet; the SimpleAudioPlaybackAgent DLL is loaded and the OnPlayStateChanged method eventually gets a call with the playState argument set to PlayState.TrackReady. It is the responsibility of OnPlayStateChanged to call the Play method of the BackgroundAudioPlayer object to start playing that track.

The AudioPlayer class handles these Pause and Play commands in the OnUserAction override shown in Figure 1. The UserAction argument indicates the particular button pressed by the user. OnUserAction responds by calling the appropriate method in the BackgroundAudioPlayer object.

When the track finishes playing, OnPlayStateChanged gets a call with PlayState.TrackEnded. The method responds by setting the Track property of the BackgroundAudioPlayer to null, which removes the item from the UVC. If you want, you can go back into the application and start the music playing again.

This simple fact has a profound implication: If you need this AudioPlayer class to maintain information between calls to OnPlayStateChanged and OnUserAction, you must keep that information in static fields or properties.

Figure 2shows the OnPlayStateChanged and OnUserAction overrides. In OnPlayStateChanged, when one track ends, the next track is set to the Track property of the BackgroundAudioPlayer. The OnUserAction override handles the Previous Track and Next Track commands from the UVC by setting the Track property to the previous or next AudioTrack in the playlist.

The logic to enable and disable the application bar buttons is fairly simple: the Previous Track and Next Track buttons are enabled based on the PlayerControls property of the current AudioTrack; thus they should be consistent with the UVC. The Pause button is enabled if the player is playing; the Play button is enabled if the player is paused. If the current track is null, Play is enabled and all the other buttons are disabled.

4a15465005
Reply all
Reply to author
Forward
0 new messages