At the moment in my app I pause any music the phone is playing and use a MediaElement to play any sounds. After the sound plays the music continues. Everything works fine but now I would like to lower the music volume rather than stop it.
1) Can this be done on wp7?
2) If so how? as I tried changing the volume like so.
void PauseMusic()
{
FrameworkDispatcher.Update();
// Pause the Zune player if it is already playing music.
if (!MediaPlayer.GameHasControl)
{
//MediaPlayer.Pause();
MediaPlayer.Volume = 0.7f;
resumeMediaPlayerAfterDone = true;
}
}
Audio is played like so.
void PlayAudioCue(string path)
{
PauseMusic();
AudioPlayer.Stop();
AudioPlayer.Source = new Uri("/Audio/" + path, UriKind.Relative);
}
void AudioPlayer_MediaOpened(object sender, RoutedEventArgs e)
{
AudioPlayer.Position = new TimeSpan(0);
AudioPlayer.Volume = 1;
AudioPlayer.Play();
}
First off, I bet you want to publish your app to the marketplace. It will fail the certification process. Look at 6.5.1 paragraph – Initial Launch Functionality
When the user is already playing music on the phone when the application is launched, the application must not pause, resume, or
stop the active music in the phone MediaQueue by calling the
Microsoft.Xna.Framework.Media.MediaPlayer class.
If the application plays its own background music or adjusts
background music volume, it must ask the user for consent to stop
playing/adjust the background music (e.g. message dialog or settings
menu). This prompt must occur each time the application launches,
unless there is an opt-in setting provided to the user and the user
has used this setting to opt-in.
More details
I'm not 100% sure, but you cannot lower the volume of user's music.
Related
I am trying to play a video after a certain event is triggered.
Currently the video starts playing but it stays behind the form (You can see it playing in the opacity of the toolbar). I have tried to bring it to the front, refresh it, select it, update it but none of it seems to work. Also, if I manually open the windows media player program and close it the video "jumps" to the front of the screen.
This is the code used to start playing the video
wmp.settings.autoStart = true;
wmp.uiMode = "none";
wmp.Visible = true;
wmp.URL = #"C:\folder\video.mp4";
wmp.Update();
I also check if the video is still playing using the Status_Change event to set it to full screen and try to bring it to front
private void wmp_StatusChange(object sender, EventArgs e)
{
if (wmp.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
wmp.fullScreen = true;
wmp.BringToFront();
wmp.Update();
}
}
Despite these efforts the video still plays behind the form. Any suggestions would be appreciated!
I have a WPF application which dynamically loads video or image files(depending on user's choice, to a MediaElement control.
It is working fine when it is a video and gets the MediaEnded event fired on ending the video.
But when I load an image, the MediaEndedevent is fired within 5 seconds.
Is it a default value? or can I change it programmatically?
Is there any property to change this interval or disable such an option?
Is it possible to make it paused until a specific action?
I have set the following properties as follows
MediaControl1.LoadedBehavior = MediaState.Manual;
MediaControl1.UnloadedBehavior = MediaState.Manual;
MediaElement is a (very thin) wrapper around Windows Media Player (or rather - uses the same framework which is used by Windows Media Player). If you open an image in Windows Media Player - you will see it will "play" it like a slideshow (even for 1 image), for about 5 seconds. That's why you get MediaEnded event in 5 seconds - Windows Media Player plays slideshow with your image for that duration. I doubt there is a way to change this from WPF (because it's behavior of external program\framework, not related to MediaElement itself) and I'm not aware of the way to change this for Windows Media Player (and even if there is such a way - it will have global effect and you probably don't want to modify your clients computer in such a way).
To solve your problem - just don't use MediaElement for displaying images - use something like Image control. If you have really strong reasons to do that - you can pause MediaElement with Pause method after your "slideshow" has been loaded, then it will not fire MediaEnded event. All in all - I cannot imagine any use case where you really have to use MediaElement for images.
You can repeat the media when you load image. Handle MediaEnded event like this:
void me_MediaEnded(object sender, EventArgs e)
{
//play video again
mediaElement.Position = new TimeSpan(0, 0, 1);
mediaElement.Play();
}
Additionally see this. It may help:
https://stackoverflow.com/a/3406857/5675763
I am using this implementation of an Audio Player in Xamarin
https://www.codeproject.com/Articles/1088094/Playing-audio-mp-File-in-Xamarin-Forms
I have a button in my code that, when pressed, plays a certain, short (1-2 seconds) tone.
The player in the link works well, but for some reason if I repeatedly press my button like 5 or 6 times the audio player doesnt work anymore. Even if I leave the page and come back the audio player still doesn't work. What could be causing this? I'm fearing that it might be a device security thing on Android, because why else would it play the first few times?
Here is my code for clicking the button
void PlayChordClicked(object sender, EventArgs e)
{
DependencyService.Get<IAudio>().PlayAudioFile(myMp3);
}
Everything else is exactly as copied from the tutorial.
Thanks!
With help from Gusman in the comments, I figured out that I was most likely crashing the app by not disposing of the MediaPlayer after using it.
I added these few lines in the Android "PlayAudioFile" method and it did the trick
player.Completion += delegate
{
player.Release();
player.Dispose();
};
I'm writing a UWP application which contains the MediaPlayer object. I am wanting to program a stop button which stops the media and sets the media to black however I can't find an approach to do it short of loading a short 'black' clip as an internal asset or hiding the element via the compositor.
The MediaPlayer does not have a function for stop, only play/pause and further, setting the media source to null simply pauses playback and freezes on the last frame shown rather than dropping to black.
Any suggestions would be much appreciated.
I recently had a related problem that boiled down to unloading media from a MediaPlayerElement.
If you navigate away from a page when playback is active, the MediaPlayerElement would not suspend or unload a playback in progress so the user would keep hearing the audio. If you re-open player's page, a new instance of a player control will be created and user may hear multiple audio tracks at once (although this requires different media sources). So it was essential to unload the media at some place like player's Unloaded event handler, page Unloaded handler, or OnNavigatingFrom/OnNavigatedFrom overrides.
Anyway, the code below stops and unloads a video/clip/media from the MediaPlayerElement.
MyMediaPlayerElement.MediaPlayer.Pause();
MyMediaPlayerElement.Source = null;
Actually, a null assignment line works just fine as a stop-and-unload, but since as of now (3/19/2018), MS documentation doesn't seem to cover how nullifying the source supposed to work, I prefer to keep an otherwise unnecessary call to Pause() here as well.
The MediaPlayer does not have a function for stop, only play/pause
As you known, MediaPlayerElement doesn't have stop method at default. Since MediaPlayerElement is newly for version 1607, for earlier version of Windows 10 we use MediaElement instead, so you can simply use MediaElement instead. MediaElement has Stop method, and after you invoke the stop method, the MediaElement will show black and set the media to begin which is just what you want. Code as follows:
<MediaElement x:Name="mediaelement" AreTransportControlsEnabled="True" Height="400" Width="400" AutoPlay="True" Source="http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4"></MediaElement>
<Button x:Name="btnstop" Click='btnstop_Click' Content="stop"></Button>
Code behind:
private void btnstop_Click(object sender, RoutedEventArgs e)
{
mediaelement.Stop();
}
How can I write or draw controls to the Windows 7 preview area using C#? For an example of what I am talking about, open Windows Media Player in Windows 7 and play a song. While the song is playing, minimize Windows Media player, then hover your mouse over the Windows Media Player icon and you will see a pause, rewind and fast forward button just below the actual Media Player preview window. How can I duplicate this kind of behavior in C#?
You're looking for Windows 7 Thumbnail Toolbars:
Thumbnail toolbars provide a mini
"remote-control" opportunity for a
window from its thumbnail. For
example, to switch to the next song in
Windows Media Player you don't need to
use the clumsy Media Player desk band
or to switch to the Media Player
application. Instead, you can use the
thumbnail toolbar directly to perform
this task, without interrupting your
work flow by jumping to another
application.
Copied shamelessly from that MSDN article:
//In your window procedure:
switch (msg) {
case g_wmTBC://TaskbarButtonCreated
THUMBBUTTON buttons[2];
buttons[0].dwMask = THB_ICON|THB_TOOLTIP|THB_FLAGS;
buttons[0].iId = 0;
buttons[0].hIcon = GetIconForButton(0);
wcscpy(buttons[0].szTip, L"Tooltip 1");
buttons[0].dwFlags = THBF_ENABLED;
buttons[1].dwMask = THB_ICON|THB_TOOLTIP|THB_FLAGS;
buttons[1].iId = 1;
buttons[1].hIcon = GetIconForButton(1);
wcscpy(buttons[0].szTip, L"Tooltip 2");
buttons[1].dwFlags = THBF_ENABLED;
VERIFY(ptl->ThumbBarAddButtons(hWnd, 2,buttons));
break;
case WM_COMMAND:
if (HIWORD(wParam) == THBN_CLICKED)
{
if (LOWORD(wParam) == 0)
MessageBox(L"Button 0 clicked", ...);
if (LOWORD(wParam) == 1)
MessageBox(L"Button 1 clicked", ...);
}
break;
}
Since this had the C# tag I'm guessing you would like to do this in managed code. Take a look at the Windows API Code Pack which includes samples of live thumbnails, thumbnail buttons, clipped thumbnails, tabbed thumbnails etc. It is thumbnail buttons that you are looking for and two or three lines of code will take care of it.
BTW, the preview area is what you get in Windows explorer when you select say a .txt file and can see the content to the right. Most office files have previewers and you can write your own too.