How to reuse BackgroundAudioPlayer after Close method called - windows-phone-7

I’m using MediaElement for playing videos and BackgroundAudioPlayer for playing audio.
Here is a case.
I’m playing remote audio via BackgroundAudioPlayer.
Then I want to play video and before MediaElement begins playing video I’m calling BackgroundAudioPlayer.Close as suggested in BackgroundAudioPlayer best practices.
MediaElement and the BackgroundAudioPlayer
Care must be taken when mixing BackgroundAudioPlayer and MediaElement for audio playback.
1. Close() must be called before switching to MediaElement playback.
2. There is only one media queue. Your application cannot pause background audio, play something with MediaElement then resume the background audio stream.
But after video is playing I want to play audio again.
// Play audio result
BackgroundAudioPlayer.Instance.Track = new AudioTrack(new Uri(audioSearchResult.Url, UriKind.Absolute), audioSearchResult.Title, null, null, null,
AudioPlayer.TrackStateBuffering, EnabledPlayerControls.All);
BackgroundAudioPlayer.Instance.Play();
I’m getting InvalidOperationException on first line of code saying “The background audio resources are no longer available”.
So how can I reuse BackgroundAudioPlayer in my app after using MediaElement?
EDIT:
If use MediaPlayerLauncher instead of MediaElement it works second time audio being played cause app is being tombstoned when MediaPlayerLauncher launches. But is it possible to mix MediaElement and BackgroundAudioPlayer in one app!?!?!
Seems to be another nightmare from MS:(

It looks like for you to be able to continue using the background audio player after you used the MediaElement to play video you need to call BackgroundAudioPlayer.Instance.Close() again after the video end and before using any other BackgroundAudioPlayer methods.
Your example should look like that:
// Play audio result
BackgroundAudioPlayer.Instance.Close();
BackgroundAudioPlayer.Instance.Track = new AudioTrack(new Uri(audioSearchResult.Url, UriKind.Absolute), audioSearchResult.Title, null, null, null, AudioPlayer.TrackStateBuffering, EnabledPlayerControls.All);
BackgroundAudioPlayer.Instance.Play();

You must call BackgroundAudioPlayer.Instance.Close() BEFORE you start playing the media element. I've tried this in both WP7.1 and WP8 emulators with a simple Background Audio agent (not streaming). Without this call I consistently see InvalidOperationExceptions. With it things behave much better.
For instance:
private void ButtonPlayMediaElement(object sender, RoutedEventArgs e)
{
BackgroundAudioPlayer.Instance.Close();
mediaElement.Source = new Uri("http://wpdevpodcast.episodes.s3.amazonaws.com/Episode_093_Were_All_Stickmen.mp3", UriKind.Absolute);
mediaElement.Play();
}
Also:
You are adding a track from your UI, you should really do this in your GetNextTrack in the background audio agent.

If you want to use both audio and video media content in your application do not mix MediaElement with BackgroundAudioPlayer!
Use MediaLauncher with BackgroundAudioPlayer and of course do not forget to call BackgroundAudioPlayer.Instance.Close() before MediaLauncher.Show()

Related

How do I extract and record audio in webRTC on Firefox?

I implemented one to one webRTC video chat (Both audio and video)
navigator.getUserMedia({
audio: true,
video:true
}, function (stream) {
}, function(err){
})
But i want to record only audio of the chat session. In chrome i am able to record by using RecordRTC, But in Firefox i am getting video+audio file (webM).
How do I extract audio in Firefox from audio+video stream?
you have one of two ways to do it, when you make new recording, you do new RecordRTC(stream, config):
currently the stream you are passing must be videostream, you can change it to audiostream as stream.getAudioTracks()[0], I have not tried this, but guessing that it should work.
or as part of config, add an attribute recorderType: window.StereoRecorder, now it would use StereoRecorder and not firefox's own MediaStreamRecorder.
p.s: have you tried recording remote stream in chrome, because chrome supports recording only stereo mode and webrtc provides audio in mono mode.

Windows Phone leave background music playing

My Windows Phone app was rejected because when I play a sound in the browser in my app it stops existing background music from playing (rejection issue 6.5.1).
How do I update my application to not stop background music?
My JavaScript is something like this:
var mySound = new Audio(soundpath);
Sound.play(mySound);
I could not find anything in Microsoft.Xna.Framework.Media that handles how the app handles browser sound, but maybe I missed something?
What you are looking for is from the xna soundeffect
Here is what I use and it works for me great for all my sound effects.
(You cannot use music with a 'soundeffect' object, as it will not pass the windows store qualifications. You have to use the MediaElement for music)
I can’t remember which you need but I have all these includes
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
and this function
private static void LoadSoundEffect(string fileName, SoundEffectInstance mySound)
{
var stream = TitleContainer.OpenStream("Sounds/" + fileName);
var effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
mySound = effect.CreateInstance();
}
If you use a SoundEffectInstance, you can start,pause,stop the soundeffect etc. without all the problems.
SoundEffect works as well, but I prefer the SoundEffectInstance.
Edit: If you use soundeffect, it will not effect the music being played.
I'm assuming that you are building a native Windows Phone app (.XAP) and then using a browser control to display your app content. If that is the case, you have checks that you will need to perform when the app is first opened. Below is a description of how to resolve that issue.
You will need to add this piece of code to your app (I suggest you check this every time your app loads/opens). Add a reference with the using statement as well for the Media.
if (Microsoft.Xna.Framework.Media.MediaPlayer.State == MediaState.Playing)
{
MessageBoxResult Choice;
Choice = MessageBox.Show("Media is currently playing, do you want to stop it?", "Stop Player", MessageBoxButton.OKCancel);
if (Choice == MessageBoxResult.OK)
mediaElement1.Play();
}
If the user allows or disallows the media to be played you must put checks in your JavaScript for this. You will fail certification again if the user says "Cancel" and you play music anyway.
There is more information on the topic here.

Extrading Metadata of an audio streaming file (MediaElement)

I just started to develop for the Windows Phone (7.1/8) platform and am still not really familiar with it.
My plan is an internet radio app which streams the audio file from a server. I used MediaElement to set the source property to the streaming URL.
It works and the app starts playing the music but I can't read any metadata about the song which is coming from the server such as artist name/title or any string which I can use to know about the song itself.
I've been searching around and tried the MediaReached event as well, but it never gets fired as well?
So any idea what should I do?
and My Code Behind Sample:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
MyMedia.Source = new Uri("MyURL");
}
I hate to tell you this but it is because the MediaElement.Attributes are not supported for Windows Phone 7 - all the limitations can be found here: http://msdn.microsoft.com/en-us/library/ff426928(VS.95).aspx

Play multiple audio streams in WP7 PhoneGap

I new to wp7 phonegap environment and I am having issues playing audio files. I am able to play one audio file as many times possible but cant play another audio of different instance, which is a requirement for my on-going project.
Any help?
you can use a phonegap native sound system
the javascript code for it is
src1="sounds/cartoon001.mp3"
src2="sounds/cartoon002.mp3"
var soundObj1 = new Media(src1,onSuccess,onError);
var soundObj2 = new Media(src2,onSuccess,onError);
soundObj1.play();
soundObj2.play();
also kindly check that mp3 files are supported in mobile.
you have to write a overriding functions for onSuccess and onnError.
javascript function to play:
playAudioFile("spellsound");
function playAudioFile(id){
var s=document.getElementById(id);
s.play();
}
html page<audio id='m' src='audio/baby/M.mp3'></audio>
or u can load src to audio tag by id in DOM

Is a .mp4 video file recorded in the WP Emulator ready for outside storage?

MS have released some code examples where a video is recorded in the WP Emulator and then saved in isolated storage. Is this .mp4 file ready to export away from the WP Emulator and play in other applications? Or is it needed to format it in some way or the other? If so, how to do it?
http://channel9.msdn.com/Shows/Inside+Windows+Phone/Inside-Windows-Phone-16-Mango-Camera-APIs
Source code for video recording is located in the WP project is called CameraUpload:
https://skydrive.live.com/?cid=bc58fec5c97e307a&sc=documents&id=BC58FEC5C97E307A%21295
http://msdn.microsoft.com/en-us/library/hh394041(v=vs.92).aspx
Edit:
I am trying to upload a video recorded in the WP Emulator to Azure blob.
A file does get uploaded, but I am not able to play that file in Zune.
I would like to be able to play the video file recorded in the WP Emulator on Zune, what to do to enable this?
The method in the Azure WCF service role, which saves the video looks like this:
(Please forgive if the method parameters have slightly misdirecting and confusing names.)
bool SaveImage(int salesItemId, string contentType, byte[] photo);
The video is saved to a blob container named "firstmay".
The code in the phone client saving the video looks like this:
client.SaveImageAsync(77, "mp4", GetPhotoBytes(m_capturedFileName));
public byte[] GetPhotoBytes(string fileName)
{
using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream isoStream = appStorage.OpenFile(m_capturedFileName, FileMode.Open);
byte[] buffer = new byte[isoStream.Length];
isoStream.Read(buffer, 0, (int)isoStream.Length); isoStream.Close();
return buffer;
}
}
When uploading a video from the WP client application, one first records a video and then uploads it by clicing "Save", it all goes on in the MainPage.
The client and server application can be downloaded from skydrive:
https://skydrive.live.com/redir?resid=159250F5CE7FE134!118
That'll all depend on what you want to be able to play back the content on. PCs with Zune would be fine, and I'd expect other video capable mobiles to be fine, but older mobiles, or standard Vista installs would require additional software, or transcoding of the video to be playable.

Resources