Could someone tell me what I'm doing wrong here? I'm trying to use one icon in the Windows Phone Application Bar to enable a user to tap it to play and pause background audio that I've inserted as a MediaElement in the XAML. This is my code:
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
{
BackgroundAudioPlayer.Instance.Pause();
}
else if (PlayState.Paused == BackgroundAudioPlayer.Instance.PlayerState)
{
BackgroundAudioPlayer.Instance.Play();
}
It makes sense reading it through and does not throw up any errors when building, however when I tap it whilst testing on my device, the music does not pause and carries on playing (I've set AutoPlay = true to achieve this) Could someone tell me what I should do to enable one button to control play and pause functionality?
Also if I remove the "else if" and make the code into a simple "else" so it looks like this:
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
{
BackgroundAudioPlayer.Instance.Pause();
}
else
{
BackgroundAudioPlayer.Instance.Play();
}
}
it pauses, but does not resume to play!
Here is the step by step implementation to achieve play/pause functionality
click and get solution
"I've inserted as a MediaElement in the XAML."
If you are using a MediaElement, I think you are playing the audio in the foreground. Try closing out of your app after the audio starts. Does it keep playing? To pause and play a media element you can just use the play and pause methods on the media element.
nameOfMediaElement.Pause();
nameOfMediaElement.Play();
To use the event handlers that use the BackgroundAudioPlayer.Instance.PlayerState, you have to setup the initial audio playing back in the background. For this you need to start the audio playback via the BackgroundAudioPlayer.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.backgroundaudio.backgroundaudioplayer(v=vs.105).aspx
Try this.
if(mediaelement.CurrentState == MediaElementState.Paused)
{
mediaelement.Play();
}
else
{
mediaelement.Pause();
}
Hope it works for you.
Related
Would like to be able to play a sound from the navigation helper class (after back button handling). Can play from a page, but can't seem to figure the logic on playing from a class with no visual elements.
Use the the following code in the page where the user would press back button:
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
In the event make the checks and play the sound and make
e.Handled=true;
this prevents the back navigation(to prevent the navigation while the sound is still playing)
When the audio has finished playing remove the handler
HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
and invoke
Frame.GoBack();
Hope it helps :)
I have an application that allows the user to take photos and save to isolated storage for upload to a server.
I am trying to play a shutter sound when the user clicks the software capture button provided. All documentation I can find indicates that soundeffect.play is asynchronous so the sound should keep playing in the background while logic continues, however in my case the sound either only partially plays or does not have time to play unless I put a breakpoint just after play() or add a Thread.Sleep(x) just after the call to play().
private void BtnCaptureClick(object Sender, RoutedEventArgs E)
{
if (_PhotoCamera != null)
{
PlayShutter();
_FileName = Guid.NewGuid().ToString(); //A breakpoint here will allow sound to play
_PhotoCamera.CaptureImage();
}
}
private void PlayShutter()
{
var Info = App.GetResourceStream(new Uri("Assets/camera.wav", UriKind.Relative));
try
{
_Effect = SoundEffect.FromStream((Info.Stream));
FrameworkDispatcher.Update();
using (_Effect)
{
if (_Effect.Play())
{
//Thread.Sleep(1000); //uncommenting this will allow sound to play if duration < 1000
}
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
I have tried using a soundeffectinstance explicitly, however behaviour does not change.
I have tried explicitly calling Play() asynchronously, no change in behaviour.
I have tried calling the following code asynchronously, no change in behaviour.
I have tried calling FrameworkDispatcher.Update frequently, no change in behaviour.
I have tried setting the .wav file in the project to “Resource” but this causes a nullreferenceexception when I call _Effect = SoundEffect.FromStream((Info.Stream)); because Info is null.(even with an absolute path)
This behaviour is present using both the emulator and an actual device via usb connection to pc to debug.
Can anybody help me resolve this frustrating issue please?
Answering my own issue now, it was the using block causing this behaviour, as the instance was being disposed. No idea why I was using that!
I'm currently making a game and everything is going fine except for one simple thing. I'm having a problem with my app sound. When I touch the "Start" Button to play the game, it presents a new View Controller with the game itself and it plays a song. I have another view controller with its settings. Inside it, I have a UISwith that mutes the App. My problem is that when I dismiss that view controller it does not save its state. I tried NSUserDefauts and could not get it working. Maybe I'm not doing it right... If you could help me I would be very appreciated!
Also, I have multiple a AVAudioPlayer (one for the game, another for when the user wins or loses) is there a way to just completely mute the entire app instead of muting every single AVAudioPlayer one at a time?
Thank you so much!
Regarding muting the sound:
- (IBAction)speakerOnOff:(id)sender
{
static BOOL muted = NO;
if (muted) {
[player setVolume:1.0];
} else {
[player setVolume:0.0];
}
muted = !muted;
}
If you have multiple AVAudioPlayer just access the properties of all and setVolume:0.0.
I'm trying to write a WP8 app that plays a short sound when a button is pressed, but I cannot seem to figure out how to play the sound. Here's a quick example of my code:
XAML
<Rectangle x:Name="Rect1" Grid.Row="0" Grid.Column="0" Tap="RectTapped" Fill="White" />
App.cs
private void RectTapped(object sender, System.Windows.Input.GestureEventArgs e)
{
MediaElement sound = new MediaElement();
sound.AutoPlay = false;
sound.Source = new Uri("Assets\\Sounds\\bark-1.wav", UriKind.Relative);
sound.Play();
}
When testing on my Nokie 820 device no sound plays. I can't understand why.
Is there something I'm doing wrong? The .wav is in my resources.
I've read that MediaElement shouldn't be used for this task. I've tried using the SoundEffect class in Xna.Framework.Audio; following the example from MSDN but that also fails because I couldn't use Content.Load as Load was not an available method of the Content class.
I've also looked at XAudio2, but as I do not know C++ I can't get my head around the examples.
You need to add MediaElement to your XAML tree
this.LayoutRoot.Children.Add(sound);
Instead of a Rectangle, use a Button. Also, MediaElement is good for playing short sounds in Silverlight applications. Make sure that the control is a part of your visual tree (add it in XAML). Then bind to the button Click event handler:
private void YourClickHandler(object sender, RoutedEventArgs e)
{
myMediaElement.Source = new Uri("/Assets/Sounds/bark-1.wav", UriKind.Relative);
myMediaElement.Play();
}
You should be using the XNA SoundEffect class instead of the MediaElement because, well, you're playing sound effects.
The documentation is bad for this area, but this is how you do it:
effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
so to play a soundeffect from your app package:
var stream = Application.GetResourceStream(filepath);
effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
The advantage over MediaElement is that the SoundEffect does not need to be in the visual tree (and also it doesn't screw up the background audio player). The disadvantage is that you have none of the (sometimes useful) events that you have on MediaElement.
For Windows Phone 8.1 Silverlight, take the first two answers (from #DenDelimarsky and #onmyway133) to play the sound.
Without adding MediaElement to XAML sound not played in my case.
Use below code to play sound:
private void YourClickHandler(object sender, RoutedEventArgs e)
{
MediaElement myMediaElement = new MediaElement();
myMediaElement.Source = new Uri("/MP3/AirHornTwoBlows.mp3", UriKind.Relative);
LayoutRoot.Children.Add(myMediaElement);
myMediaElement.Play();
}
Remove the line
sound.AutoPlay=false;
or change it to:
sound.AutoPlay = true;
it will work then.
I create a record audio application based on red5 0.9 version.
Below is my flex client code snippets. My red5 server work fine.
private var playStream:NetStream;
private function playRecord(event:MouseEvent):void{
if(isPausing){
playStream.togglePause();
}else{
playStream=getStream();
playStream.bufferTime=15;
playStream.play(streamName);
}
}
private function pauseRecord(event:MouseEvent):void{
playStream.togglePause();
}
I press pause button fine, but when i press play button, the audio always play form the begining.
Why? do i need to clean up NetStream Buffer?
I found solution myself.
playStream.maxPauseBufferTime=0;