简体   繁体   English

为什么openFd(filename)不能与String一起使用?

[英]Why openFd(filename) don't work with String?

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;

Why S tring in openFd(filename) don't work? 为什么tring in openFd(filename)不起作用? I got FileNotFoundException . 我得到FileNotFoundException If I type openFd("voice/song.mp3") all working, but it don't right for me. 如果我键入openFd("voice/song.mp3")都可以,但是它不适合我。

I would wager quite a bit that the string that you get from your textview isn't trimmed of whitespaces. 我敢打赌,您从textview中获得的字符串不会修剪空格。

Try 尝试

String words = mainDataTextView.getText().toString();
String trimmedWords = words.trim();
String con = "voice/"+trimmedWords+".mp3";

Also, if this is not correct, could you print what words contains? 另外,如果这是不正确的,您可以打印出包含哪些words吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM