简体   繁体   English

无法在ListView中播放音频文件

[英]Unable to play audio files in ListView

I need help figuring out what is wrong in this code. 我需要帮助找出此代码中的错误。 I've searched how to implement the ListView OnItemClickListeners, but I can't do it. 我已经搜索了如何实现ListView OnItemClickListeners,但是我做不到。 When I click on listitem the app crash and media player does not work. 当我单击listitem时,应用程序崩溃且媒体播放器不起作用。

File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/Record" );
File list[] = file.listFiles();

for( int i=0; i<5; i++)
{


         myList.add( list[i].getName() );

}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, myList);
listView.setAdapter(adapter); //Set all the file in the list.


listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        final String res = new String(
                Environment.getExternalStorageDirectory() + "/Record/");

        playSong(res+myList.get(position));
    }
    private void playSong(String songPath) {
        try {
            mp.reset();
            mp.setDataSource(songPath);
            mp.prepare();
            mp.start();

        } catch (IOException e) {
            Log.v(getString(R.string.app_name), e.getMessage());
        }

    }

    });

}
}

您可以在这里查看我的音频播放器教程项目: https : //github.com/GIGAMOLE/NEON

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

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