Amendment: This appears to be a problem with the particular audio file I was using. Other applications on the droid such as the Astro file manager also fail to play it, and if I replace it in the package with an AAC file, it plays it without error. I encoded the problematic audio file to MP3 format from a WAV file, using LAME on ubuntu. It would be good to know the limitations of the android media player. mplayer seems to have no problem playing the file on ubuntu. I suppose I should submit a bug report.
Original question: The following code crashes when I try to play it on my droid. The error message given in the log is "Command PLAYER_INIT completed with an error or info PVMFErrNoResources.
" Then an IOException is raised on the mp.prepare()
line. There is a file res/raw/bell.mp3
in my project directory, which I assume corresponds to R.raw.bell
in the code below. I am building with "ant debug
". In case it's relevant, when I created the project directory with "android create
", I set the target number to 4, corresponding in my system to "android 2.0."
What am I doing wrong, here?
imp开发者_如何学Goort java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.util.Log;
public class testapp extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try {
MediaPlayer mp = MediaPlayer.create(this, R.raw.bell);
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
}
You do not need to call prepare()
if you use the static create()
method to get the MediaPlayer
. It does the prepare()
step for you. You only need to call prepare()
if you either use the regular MediaPlayer
constructor or if you are trying to reset the clip back to the beginning to play back from the existing MediaPlayer
object.
Here is a sample project for playing back sounds (in my case, an Ogg clip).
精彩评论