Best approach for playing a single tone audio file? - audio

Can anyone tell me the best approach to playing single-tone, audio (.mp3) files in a Windows Phone 8 app? Think of a piano app, where each key would represent a button, and each button would play a different tone.
I'm looking for the most efficient way to go about this - I've got 8 different buttons that need to play a different tone when tapped.
I tried using the MediaElement:
MediaElement me;
public MainPage()
{
InitializeComponent();
me = new MediaElement();
me.AutoPlay = false;
me.Source = new Uri("/Sounds/Sound1.mp3", UriKind.Relative);
btnPlay.Click += btnPlay_Click;
}
private void btnPlay_Click(object sender, EventArgs e)
{
me.Play();
}
But nothing happens, either in the emulator or on a device (testing w/ a Lumia 822). Am I doing something wrong here? It seems like it should be pretty simple. Or would using MediaElement even be the best thing to use for my scenario?
Would this fall under the Background Audio category? I've read through this example but it seems overkill for what I want to do.
I've also read about using XNA's SoundEffect to do the job, but then I'd have to convert my .mp3 files to .wav (which isn't necessarily a problem, but I'd rather not go through that if I don't need to).
Can anyone tell me either what I'm doing wrong in my example above or guide me to a better solution for playing quick <1s audio tones?

I had this problem before with MediaElement not playing audio files. After many attempts I found out that it only plays if it defined in the xaml and AutoPlay is set to true.
Try defining it in the xaml or you can just add it to your LayoutRoot.
var me = new MediaElement();
LayoutRoot.Children.Add(me);
me.AutoPlay = true;
me.Source = new Uri("Sound/1.mp3", UriKind.Relative);

I have had good luck just doing this piece of code in my app. But it may not work as well in your context, give it a whirl though.
mediaElement.Source = new Uri("/Audio/" + songID.ToString() + ".mp3", UriKind.Relative);
mediaElement.Play();

Related

Is it possible to set dynamic marq text and logo into a video stream recording using libvlcsharp?

Good day to you all,
I have a problem, maybe someone can provide some helpful ideas on how to implement or if it's even possible:
I want to record RTSP stream from an IP-cam and I would like to add some text information and logo into the recording so it might be viewed when played back.
To do so I have first created one MediaPlayer element to connect to the IP-cam, duplicate onto the display, and recast via UDP.
using (var stream01_view = new Media(libVLC, "rtsp://192.168.10.214:5554",FromType.FromLocation))
{
stream01_view.AddOption(
":sout=#duplicate{" +
"dst=display{noaudio}," +
"dst=std{access=udp,mux=ts,dst=:1234}");
stream01_view.AddOption(":sout-keep");
player.Play(stream01_view);
}
The second stream connects to the local UDP cast and outputs to file
using (var stream01_record = new Media(libVLC, "udp://#:1234", FromType.FromLocation))
{
stream01_record.AddOption(":sout=#transcode{sfilter=marq}:file{mux=ts,dst=VideoMarqLogo.mp4}");
stream01_record.AddOption(":sout-keep");
recorder.Play(stream01_record);
}
Calling class MediaPlayer methods SetMarqueeInt and SetMarqueeString don't give the expected result.
Thanks mfkl for pointing to the right direction.
So the thing that does the trick is:
stream01_record.AddOption(":sout=#transcode{ vcodec=h264, scale=0.75, " +
"sfilter=marq{file='marq.txt',position=9}," +
"vfilter=logo{file='logo.png',position=6}}" +
":file{mux=ts,dst=VideoMarqLogo.mp4}");
A bit of a warning though, this piece of code is CPU intensive.
I'm left wondering if there could be a way to do this using GPU encoding.

MediaPlayerElement vs MediaElement Which one to choose?

I have gone through the answer provided here for the difference. But I need to just play notification sound for like 2 seconds as an alert. No video or any other heavy loading.
This is the notification sound I am about to play.
ms-winsoundevent:Notification.SMS
The below is for MediaPlayerElement:
MediaPlayerElement mediaPlayerElement = new MediaPlayerElement();
mediaPlayerElement.SetMediaPlayer(new Windows.Media.Playback.MediaPlayer { AudioCategory = Windows.Media.Playback.MediaPlayerAudioCategory.Alerts});
mediaPlayerElement.MediaPlayer.AudioCategory = Windows.Media.Playback.MediaPlayerAudioCategory.Alerts;
mediaPlayerElement.Source = Windows.Media.Core.MediaSource.CreateFromUri(new Uri("ms-winsoundevent:Notification.Default"));
mediaPlayerElement.AutoPlay = false;
mediaPlayerElement.MediaPlayer.Play();
The below is for MediaElement:
MediaElement mediaElement = new MediaElement();
mediaElement.AudioCategory = AudioCategory.Alerts;
mediaElement.Source = new Uri("ms-winsoundevent:Notification.Default");
mediaElement.AutoPlay = false;
mediaElement.Play();
Can I use MediaElement since its a small audio or should I only use MediaPlayerElement as it is the one prescribed by Microsoft? which one is better to use in this case?
P.S.: I need to set audio category as Alerts in order to dim any background music.
Can I use MediaElement since its a small audio or should I only use MediaPlayerElement as it is the one prescribed by Microsoft? which one is better to use in this case?
Derive from official document,
In Windows 10, build 1607 and on we recommend that you use MediaPlayerElement in place of MediaElement. MediaPlayerElement has the same functionality as MediaElement, while also enabling more advanced media playback scenarios. Additionally, all future improvements in media playback will happen in MediaPlayerElement.
And it means that the new feature will be developed base on the MediaPlayerElement, we recommend using MediaPlayerElement that could make your app has longer life.

Play a sound in actionscript

I am currently trying to write code for actionscript that will play a sound. Normally this wouldn't be a problem, however I need to play the sound with the stipulation that it doesn't begin playing until after the sound is finished loading. Is there a way to make sure the sound is loaded before I play it?
You can do the following:
var sound:Sound = new Sound();
sound.addEventListener(Event.COMPLETE, onSoundLoaded);
sound.load(new URLRequest("mySound.mp3"));
function onSoundLoaded(event:Event):void
{
var loadedSound:Sound = event.target as Sound;
loadedSound.play();
}
This is taken from this adobe tutorial.
Good luck coding!

IMFMediaPlayer hangs during SetSourceFromByteStream

Background: I'm coding a metro-styled app for Win8. I need to be able to play music-file. Because of quality and space requirements we're using encoded audio (mp3/ogg).
I'm using XAudio2 to play sound effects (.wav files), but since I couldn't figure out a way to play encoded audio with it, I decided to play the music files with Media Foundation (IMFMediaPlayer interface).
I downloaded metro apps sample, and found out that the Media Engine Native C++ video playback sample was closest to what I needed.
Now that my app has MediaPlayer playing musics, I ran into a problem. If the device running the app is slow enough, MediaPlayer hangs. When I'm running the release-version of the app on my device, it's fine and I can hear the music just fine. But when I attach the debugger or run it on a slower device, it hangs when I'm setting bytestream for the MediaPlayer to play.
Here's some code, you'll find it pretty similiar to the sample:
StorageFolder^ installedLocation = Windows::ApplicationModel::Package::Current->InstalledLocation;
m_pickFileTask = Concurrency::task<StorageFile^>(installedLocation->GetFileAsync(filename)), m_tcs.get_token());
auto player = this;
m_pickFileTask.then([player](StorageFile^ fileHandle)
{
player->SetURL(fileHandle->Path);
Concurrency::task<IRandomAccessStream^> fOpenStreamTask = Concurrency::task<IRandomAccessStream^> (fileHandle->OpenAsync(Windows::Storage::FileAccessMode::Read));
fOpenStreamTask.then([player](IRandomAccessStream^ streamHandle)
{
MEDIA::ThrowIfFailed(
player->m_spMediaEngine->Pause()
);
MEDIA::GetMediaError(player->m_spMediaEngine);
player->SetBytestream(streamHandle);
if (player->m_spMediaEngine)
{
MEDIA::ThrowIfFailed(
player->m_spEngineEx->Play()
);
MEDIA::GetMediaError(player->m_spMediaEngine);
}
}
);
}
);
And here's the SetBytestream method:
SetBytestream(IRandomAccessStream^ streamHandle)
{
if(m_spMFByteStream != nullptr)
{
m_spMFByteStream->Close();
m_spMFByteStream = nullptr;
}
MEDIA::ThrowIfFailed(
MFCreateMFByteStreamOnStreamEx((IUnknown*)streamHandle, &m_spMFByteStream)
);
MEDIA::ThrowIfFailed(
m_spEngineEx->SetSourceFromByteStream(m_spMFByteStream.Get(), m_bstrURL)
);
MEDIA::GetMediaError(m_spEngineEx);
return;
}
The line where it hangs is:
m_spEngineEx->SetSourceFromByteStream(m_spMFByteStream.Get(), m_bstrURL)
When I'm debugging the app, I can press pause and see the stack. Well, not much of it, but atleast I can see it that it's indefinitely at
ntdll.dll!77b7f4dc()
Any ideas why my app would hang in such a way?
(OPTIONAL: If you know a better way to play mp3/ogg in a c++ metro-styled app, let me know)
Could not figure out why this is happening, but I managed to code a work-a-round:
IMFSourceReader can be used to decode MP3s and feed bytes into XAudio2SourceVoice.
XAudio2 audio stream effect sample contains good example how to do this.

Playing a sound in a Firefox add-on

I would like to create a simple add-on that would play a different MP3 recording every time the user double clicks a word in a webpage he is visiting and selects a special option from the context menu.
The MP3 files are located on a remote server. Normally I would use JavaScript+Flash to play the MP3 file. In a Firefox add-on, however, I'm unable to load external scripts for some reason (playing the sound works fine if it's the webpage itself that loads the scripts, but of course I need it to work with every website and not just the ones that include the script).
So what's the easiest way to play a remote MP3 file in a Firefox add-on using JavaScript?
This may not entirely solve your question, as I don't BELIEVE it plays MP3s, but I'm not certain.
Firefox has nsISound, which I KNOW can play remote WAV files, as I've tested and proved it.
You may want to test it for yourself and see if it leads you a little closer!
var ios = Components.classes['#mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);
var sound = ios.newURI("http://www.yoursite.com/snds/haha.wav", null, null);
var player = Components.classes["#mozilla.org/sound;1"].createInstance(Components.interfaces.nsISound);
player.play(sound);
Good luck, I hope this at least gets you close!
I know this is an old question, but if someone needs a way to do it:
let player = document.createElement("audio");
player.src = browser.runtime.getURL(SOUND_URL);
player.play();
There is one caveat: the user must have allowed autoplay on the website.
Here is a working code....
var sound = Components.classes["#mozilla.org/sound;1"].createInstance(Components.interfaces.nsISound);
var soundUri = Components.classes['#mozilla.org/network/standard-url;1'].createInstance(Components.interfaces.nsIURI);
soundUri.spec = "chrome://secchat/content/RING.WAV";
sound.play(soundUri);
var window = require('sdk/window/utils').getMostRecentBrowserWindow();
var audio = ('http://example.com/audio.mp3');
audio.play();

Resources