所以我一直在尝试为android制作一个简单的音效应用程序。以下是相关代码:
public static final String LOG_TAG = "BCA";
public MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.v(LOG_TAG, "creating");
super.onCreate(savedInstanceState);
setContentView(R.layout.main_list);
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.v(LOG_TAG, "set stream type");
playSound();
}
public void playSound()
{
try {
mp.setDataSource("R.raw.sound1");
Log.v(LOG_TAG, "set data source");
mp.setOnPreparedListener(this);
mp.setOnErrorListener(this);
mp.prepareAsync();
Log.v(LOG_TAG, "preparing");
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalStateException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void onPrepared(MediaPlayer mediaPlayer)
{
Log.v(LOG_TAG, "finished preparing; starting");
mp.start();
Log.v(LOG_TAG, "started music");
}
public boolean onError(MediaPlayer mp, int e, int f)
{
Log.v(LOG_TAG, "There was an error");
Log.v(LOG_TAG, mp + " " + e + " " + f);
mp.reset();
return true;
}
基本上它会到达设置的“设置数据源”标签,但从未完成准备。错误代码是 (1, 4) 1 显然是未知错误。我使用了多个声音文件,我知道其中一个声音文件在仅使用 mp.create(etc...) 时就像播放器一样工作
我不确定这里发生了什么
提前致谢
编辑:所以我按照亚历克西斯卡地亚给出的链接示例,现在没有错误。但是, FileinputStream 永远不会完成。该程序似乎停滞不前。这是新代码:
public void playMusic()
{
File file = new File("R.raw.music1");
Log.v(LOG_TAG, "set file");
try {
Log.v(LOG_TAG, "in try block");
FileInputStream is = new FileInputStream(file);
Log.v(LOG_TAG, "set file input stream");
mp.setDataSource(is.getFD());
Log.v(LOG_TAG, "set data source");
mp.setOnPreparedListener(this);
mp.setOnErrorListener(this);
Log.v(LOG_TAG, "set on prepared/error listeners");
mp.prepareAsync();
Log.v(LOG_TAG, "preparing");
}