Playing wave files in Android
I am trying to play a wave file(VoiceMail) in Android. The file is downloaded from a remote server and written to sdcard.
I use the following code to play the Wave file usign the MediaPlayer class bundled with the SDK:
FileDescri开发者_Python百科ptor fd = new FileInputStream(songFile).getFD();
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.reset();
try{
mMediaPlayer.setDataSource(fd);
}
catch(IllegalStateException e){
mMediaPlayer.reset();
mMediaPlayer.setDataSource(fd);
}
mMediaPlayer.prepare();
mMediaPlayer.start();
However when I run my code, I get the following error: Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported error (1, -4).
Note that when I pull this file on my desktop machine and copy it to the Music folder of my HTC hero device, the Music player of the device can play this file so I am guessing the file format is compatible with Android.
Not sure of what the problem is; please help.
Thanks.
Another issue may be that the file has been corrupted during download somehow... I recently had this exact same issue with images that I downloaded from a google images search... for some reason the files kept getting corrupted... I still haven't found a solution. I think that somehow the HTTP client is losing packets.
Probably the Audio Codec is not supported. Try using ffmpeg to determine the codec
ffmpeg -i audio.wav
And compare with this page http://developer.android.com/guide/appendix/media-formats.html
You'll see that not every possible WAV type will work on Android. I ran into this problem before with webcams that have audio. I had to convert from a IMA-ADPCM wav file to a PCM wave file.
精彩评论