Android Native Play Sound using /dev/msm_pcm_out

Following my previous article, where I presented that it is possible to write a native C application to directly extract the screen’s framebuffer data and save it as a BMP, for the purpose of making screen captures, I will give another example of using the NDK to compile a C code that would play sounds on your Android, by directly interfacing the PCM device: /dev/msm_pcm_out

There is an old sample in the Google Android code that does just that and more: playwav.c . It can be compiled using the NDK and Cygwin, as presented here:
android native play sound

Simply copy the binary to your Android, and play a wave file by calling:

./playsound -play learn.wav

Easy, and a nice surprise to hear sounds coming from your Android, without having any top level interfaces running for that purpose.
The project files:
NativePlayWav

This article has 6 Comments

  1. Hey Radu,

    I don’t have rwx access in /data/data, I only have rw- in /sdcard :D.
    I tried using abd root, but I get an error: adbd cannot run as root in production builds

    Any hints on how I could test your app ?

    10x

  2. Hi Victor, thanks for dropping by.

    I don’t think you will be able to get this work , if you don’t have root access, since the little native code needs access to /dev/msm_pcm_out

    By any chance can you try it on a different phone, or ROOT you own?

  3. Hi Radu,
    Thanks for the quick answer and sorry for my delayed one :).
    I wanted to write an app that would work on standard issue Android devices. I guess I’ll have to use the latest NDK for low-latency sound playback and target phones having at least v2.3.
    Thanks again for everything!

  4. Hi Radu,

    I just came across this, and though I am new to the environment, was able to get through most of the above steps. I wish to test this on the android emulator, and then add some processing code to the file before getting it onto a phone. However on the emulator, when i get to the “chmod +x playwav” step i get the error “bad mode”. And if i try “./playwav -play learn.wav” anway, i get the message “pcm_play: cannot open audio device: No such file or directory”. Could you help me run this on the emulator, please?

    Thanks a lot.

  5. I have same problem like you “cannot open audio device: No such file or directory” is because i have old android (2.2 froyo)? any body confirm if /dev/msm_pcm_out is found in froyo ?
    i am using “samsung galaxy S”.

    Thanks.

  6. Hi,
    I have /dev/msm_pcm_out, but playwav does not play anything at all.
    I get no errors, it just fills buffers and then gets stuck trying to fill more buffers, but obviously they never empty.
    I’m using an Htc Desire Z.
    Thanks

Leave a Reply