How can I use mp3 files in Windows Phone 8? - c#

in my windows phone gaming apps, I have to use lots of sound. Now I have seen that Windows phone does not support mp3 files. So I need to use the wav files. Any mp3 file which is just 500 kb size in mp3 format, when convert that to ".wav" it becomes min 2.5MB. It's actually eating up my apps size and unnecessarily the size of my apps is getting bigger.
Anyone know how can I use the mp3 file? In my solution I have a Asset folder and inside this folder all the ".wav" files are located.
How I am doing this let me write a code
SoundEffect effect;
Iinside constructor-
{
...
var soundFile = "Assets/Jump.wav";
Stream stream = TitleContainer.OpenStream(soundFile);
effect = SoundEffect.FromStream(stream);
And in the code
effect.Play();
Is there any better approach. In some thread I come to know that doing this is not a better way coding as it creates object and used up the system space. Please suggest what to do, how do I add mp3 files and write better code for working with sound files.

you can use BackgroundAudioPlayer to play your wav and mp3 files. SoundEffect class cannot play mp3 data
Go through this it's an entire app on it's own.
Background Audio WP

To use an MP3 file you would have to decode the MP3 into PCM in memory, and use that resulting stream in the SoundEffect.FromStream method.
Another thing you could try is encoding the wav files as ADPCM. This usually compresses the wave file at ratio of 4:1. If you can't use the stream directly, decoding of ADPCM is much more straightforward than decoding an mp3 file.
And one more thing you could try to save space is converting the uncompressed wave files into mono, and a lower sampling rate. You can perform listening tests on the sounds after the conversion to make sure that the loss in quality is acceptable or not.

Related

C# Play WAV Soundfile - Error: "The wave header is corrupt."

I have the following issue.
I want to play a WAV audio file in my C# program, which I converted from MP3 to WAV before. I can't just play it as a MP3 as C# needs the Media Player library added first, but I don't want to use any other library that increases file size or adds another library dll to my export.
So when I use Audacity to convert the file from MP3 to WAV, playing the file in C# works fine, but the WAV file just gets very big, which I want to avoid (from 3 MB MP3 to 25 MB WAV).
Accordingly, I switched to Adobe Premiere Pro because this offers more options to adjust the end size of the file.
Now there are 5 different audio codecs in Adobe Premiere Pro:
Uncompressed
IMA ADPCM
Microsoft ADPCM
CCITT A-Law
GSM 6.10
With the uncompressed mode, the WAV can be played in C# without any problems. The file size is still very large in this case.
All other audio codecs allow me to reduce the file size up to 80% without audible audio loss, but with each of them I get the error message: "System.InvalidOperationException: "The wave header is corrupt." at runtime.
I tried to fix that with previous threads saying I should try reloading the stream or changing the position of the stream to 0 but that didn't work for me, I still got the same error message.
I don't know if there is even a way to fix it as it might be an issue with the compression?
I am able to play those WAV files with Media Player tho.
Is there any workaround?

Play video file being written

I developed a socket based p2p system where i stream mp4 video from a client to a server. The video data are transferred using byte[].
The transfer works, however i am struggling to figure out how i could play, in continuous, a mp4 file being transferred to another PC? The PC application, that would play the file, is in WPF.
I tried using a MediaElement to play the file, being written to the disk, but for some reason it doesnt play at all. I can play it with VideoLan tho.
Any hint of where to look? I am lost from here!
Thanks in advance!
Its going to be different with every player. Mp4 is probably the worst possible container for this application. The container is designed to be used with random access media, such has a hard disk. The player has the ability, and sometimes requirement to seek around within the file, and not just plat start to finish. Either you need to make the protocol mp4 aware (parse the file, and download the block that will most like be needed next), or use a streamable container such as TS, or flv.
Its is also possible that the player knows the file is incomplete. The atoms sizes are encoded within the file. If the file size doest match, the combined atom sizes, the player could assume a corrupt file.If this is what is happening, you can try to pre allocate the file. it may trick the player enough to play

WP 8.1 Trimming MP3 FILES

Is there a way to trim a mp3 file?
I did some research and every search was leading me to NAudio.
However NAudio doesn't support WP8.1. Actually I don't think it support any version of windows phone.
Is there any other way to trim a mp3 file? MP3s are made of frames and ID3 tags.
Is there a helper that could read mp3 frames and then copy them into a new file?
An MP3 file is a collection of MPEG frames that you can manipulate fairly easily. If you read the NAudio source code (specifically the Mp3Frame class) you'll find a fairly good set of C# code for reading the individual frames. From there you can index the frames, figure out their positions in time and copy out only the ones you're interested in to the output file.
It may be a bit more complex than that, but have a look at Mark's code in and around the Mp3Frame class for some more information on how it works.
Oh, and don't forget to credit him if you use his code.

Windows Forms dotnet .wav playback - need to preload multiple wav files to memory for fast playback

I need to play back 30 second audio clips, 1 per second, in winforms dotnet.
I am currently loading/playing the wav files from the filesystem, which works fine on a notebook, but is causing problems on a netbook. Can I pre-load all sound files into memory, if so how?
If you use the SoundPlayer to play your files you can preload the file with SoundPlayer.Load.
SoundPlayer sp = new SoundPlayer("filename");
sp.Load(); // preload
sp.Play();
Edit:
As noted by the documentation you may also use SoundPlayer.LoadAsync to load the sound in the background.
I'm inclined to say that you would load the file into a system.io.memorystream of some sort. Hopefully the libraries that play your file, will take a memorystream or memorystream can be converted into the data structure that this library takes.
Here's a recent example that creates a .wav file (a sine) in memory entirely from scratch and plays it. What you're trying to do should be much simpler, and you should be able to derive it from the sample posted.
Real low level sound generation in C#?

Converting mp3 bitrates using lame_enc.dll or similar

I need to programatically convert mp3's of any bitrate to a standard bitrate for streaming audio using c#.
Currently a buffer is populated with mp3 data from disk and then send out to the "listeners" at what should be a constant speed (the broadcast), but the mp3's could be of any bitrate. This makes timing extremely difficult and should rather be streamed at a standard bitrate instead of a bitrate dictated by the mp3 itself.
Lame seems to be the right encoder for the job, but any documentation or sample code only seems to be concerned with converting from wav samples to mp3. Not mp3 to mp3. The exe wrapper can do the bitrate conversion, but completely without any clue as to what gets passed to beEncodeChunk().
Has anyone had any experience in doing this kind of thing with lame or any similar encoder?
Do i need to decode to wav then encode back to mp3 to achieve what i'm after?
I welcome any links or advice with open arms.
Thanks
you have to decode the mp3 to wav, then re-encode it to the new bitrate

Categories

Resources