Windows Phone How to listen to volume/muisc control events - events

Is it possible to listen to system's volume and music control events (Next/Prev)? I am playing a song from MediaLibrary using MediaPlayer but how to capture Next\Prev button press event? Thanks!

Volume Events
It's not possible to listen for (or subscribe to) the system volume events. You cannot find out the current system volume or know when the volume keys on the phone are pressed.
If your app is playing music using a MediaElement, for example, you simply play the sounds or music blindly not knowing what the system volume is.
Developers have voted to improve this API which will hopefully happen in a future release.
Music Events
If your app is playing music using the BackgroundAudioPlayer, you can subscribe to the PlayStateChanged event. You can detect if music is playing using the MediaPlayer class. Although this is an XNA library, it works in WP7 and WP8.
If the user starts music in your app, it will stop whatever music the system is playing and take over. The opposite is also true. If your app is playing music, and the user goes to another music app, your BackgroundAudioPlayer will receive PlayState.Stopped.

Related

Is there a way to detect video/audio playback on Windows and Mac?

Is there a Win32 API for Windows or a Cocoa API for Mac which detects video/audio playback?
I'm developing a desktop application that needs to be aware of the user's activity on the computer. When the app is running, if the user is actively using his computer, the app should stay dormant. If the user is away from the computer for a certain period of time, then the app should run some logic.
There is a way to detect user activity via keyboard/mouse interaction. However, if the user is passively engaged (e.g. watching a video or listening to music) without any keyboard/mouse action, then I have no way of knowing.
There is a similar question but no answer.
Video playback detection with Win32 API?
Any help would be much appreciated. Thank you!

WP7 background audio resources are no longer available

I'm using BackgroundAudioPlayer for my Windows Phone 7 music & video application. After I play some music, I play video using MediaPlayerLauncher, then press Back to return to my app. There whenever I use BackgroundAudioPlayer.Instance. I receive error "The background audio resources are no longer available".
Someone on MSDN suggests using try/catch, but this is not a good idea, and can slow down the app.
Other suggests call BackgroundAudioPlayer.Instance.Close() before launch MediaPlayer. However, when I play music, the agent load .dll again, which takes very much time.
How to fix this ?
If you play a video after your audio the OS will definitely "terminate" your Background Audio Player. From your question it seems this is reproducible 100% of the time which would confirm this. Your only option is to restart the background audio player again after you have called BackgroundAudioPlayer.Instance.Close(), and then played your video. Which as you said will require reloading your player DLL when you start the BAP.
Update following up from comments
If you aren't implementing a streaming audio agent but only an AudioPlayer agent there isn't a process for you to kill anyway. The OS spins up a process as and when it needs to get you to process an action (e.g.: user action, track ended, shutdown).
BackgroundAudioPlayer.Instance.Close() just makes sure that the OS releases those resources cleanly in a scenario such as the OP has.
To restart background audio, just call BackgroundAudioPlayer.Instance.Play() again.

how to code to stop/pause zune music in windowsphone7

Does anyone knows, how to code in c# to stop/pause the zune music in windowsphone7. I used background music in my application, if i launch my application, zune music stops. how to handle this? i use windowsphone7 version, so cant able to use BackgroundAudioPlayer instance.
You shouldn't stop the users background music if it's already playing and you're not going to play music yourself.
If you wish to play music in your app then (after confirming with the user that it's ok to stop the currently plaing music) just start playing your music and this will then cause the music that was playing to be stopped.
There is no way to automatically resume the musc that was playing prevviously when you stop playing your music.

Accessing MediaPlayer in Background Agent in Windows Phone 7

I am trying to capture the current playing track in the Zune music player, in my Background Agent app. Looking at the Unsupported APIs for Background Agents page on MSDN, all of XNA is not supported - which means I can't use XNA's Media.MediaPlayer. Is there any workaround or solution for this?
Nope.
There is currently no way to access anything other than the tracks which you are playing in a BackgroundAudioPlayer agent.
Actually, there is no way to access anything other than THE track (not tracks) that the background audio player is playing. There is no concept of a playlist exposed by the BAP singleton, which apparently is the source for much frustration with this component. You have to resort to IsolatedStorage or some other hack to feed the BAP a playlist.

Mute other applications' sound when my application is playing a sound

I would like to mute other applications sounds when my application is playing a sound. I know that this is possible in Windows 7 because it allows sound control on a per-application basis.
The specific scenario is my app needs to have its sound play exclusively; if other applications (eg Winamp, Media Player Classic etc) are playing a sound, they should be muted for the duration of the sound played by my application.
I would like to know how it can be done using Delphi? Which library/system call?
I doubt this is easily achieved.
What if the other apps took the same view? Suppose another app decided that it wanted its sound to play and mute all other apps. Which app would win?
On Vista & above you can do this by using CoreAudio/WASAPI & an exclusive mode stream.
Mumble is doing this, you can look at source code.

Resources