[Cf. Skills]Play audio files of embedded Resources
Abstract: This article describes how to play audio files that sneak into resources on Windows Mobile.
Keywords
Playsound, Windows Mobile, embedded resources, P/invoke
What should I do if I want to play the audio file of embedded resources on Windows Mobile? Obviously, reflection is needed. I checked msdn. Fortunately, getmanifestresourcestream is available for mobile devices.
First, we need a class to use P/invoke to help us implement the playing sound function:
[Flags]
Enum Soundflags
{
Alias = Zero X 00010000 ,
Filename = Zero X 00020000 ,
Synchronous = Zero X 00000000 ,
Asynchronous = Zero X 00000001 ,
Memory = Zero X 00000004 ,
Loop = Zero X 00000008 ,
Nostop = Zero X 00000010
}
Class Playnativeref
{
[Dllimport ( " Coredll. dll " , Entrypoint = " Playsound " , Setlasterror = True )]
Public Extern Static Int Playsound ( Byte [] Szsound, intptr hmod, soundflags flags );
}
When calling, we must first extract the sound resource fromProgramLoad the set to memory:
Memorystream MS = (Memorystream) Assembly. getexecutingassembly (). getmanifestresourcestream ( " Playembeddedaudio.resources.yuyinlangdu.wav " );
TipsThe Resource Name here must be correct. If you are not sure, you can use the getmanifestresourcenames method to view the Resource Name.
Then, we can call the playsound method to play the video:
Playnativeref. playsound (
Ms. getbuffer (),
Intptr. Zero,
Soundflags. synchronous | Soundflags. Memory );
Note that the soundflag must add memory to indicate that the first parameter is to point to the audio file image in the memory. Load resources from memory during playback.
However, it should be noted that the embedded sound file should not be too many, otherwise it will make the Assembly very large.
CodeDownload from here
Enjoy it!
Yellow winter <fox23>
2008/5/18