private void startSound(String filename) throws IOException{
AssetFileDescriptor afd = getAssets().openFd(filename);
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.prepare();
player.start();
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.addForLearning:
break;
case R.id.music:
String words = mainDataTextView.getText().toString();
String con = "voice/"+words+".mp3";
try {
startSound(con);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
为什么S tring in openFd(filename)
不起作用?我得到了FileNotFoundException
。如果我输入openFd("voice/song.mp3")
全部正常工作,但它不适合我。
答案 0 :(得分:0)
我会下注很多,你从textview获得的字符串不会被修剪掉空格。
尝试
String words = mainDataTextView.getText().toString();
String trimmedWords = words.trim();
String con = "voice/"+trimmedWords+".mp3";
另外,如果这不正确,您可以打印words
包含的内容吗?