如何从ListView播放录制的音频文件

时间:2014-03-07 09:46:04

标签: android android-listview media-player audio-recording

我正在开发一个呼叫记录应用程序。它记录呼叫并将其保存在手机内存(文件管理器)中,保存音频文件后,我设法在列表视图中显示录制的文件。知道我什么时候单击列表上的文件来播放它给出准备失败的错误。我发布了代码。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.mylist);
            mp = new MediaPlayer();
    // this will show the recorded files into ListVIew
    myList = new ArrayList<String>();
    files = new File(Environment.getExternalStorageDirectory()
            + "/Audio File/");
    File list[] = files.listFiles();
    for (int i = 0; i < list.length; i++) {
        myList.add(list[i].getName());
    }

    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, myList);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            try {
                playSong(files + myList.get(position));
            } catch (Exception e) {
                Log.d("PlaySong", "Error in Playing song "+e.toString());
            }

            // just for checking the position of item in list
            Object listItem = listView.getItemAtPosition(position);
            Toast.makeText(MainActivity.this, "" + listItem,
                    Toast.LENGTH_SHORT).show();
        }

        private void playSong(String string) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {

            mp.reset();
            mp.setDataSource(string);
            mp.prepare();
            mp.start();
        }

    });
}

错误是

  03-07 14:40:25.866: I/SurfaceTextureClient(15355): [STC::queueBuffer] this:0x4f11d050,             api:1, last queue time elapsed :11282 ms
  03-07 14:40:25.866: I/SurfaceTextureClient(15355): [0x4f11d050] frames:2, duration:11.356000, fps:0.176113
  03-07 14:40:25.916: V/Provider/Setting(15355): from settings cache , name = sound_effects_enabled value = 0
  03-07 14:40:25.918: D/MediaPlayer(15355): mPlayerID = 48
  03-07 14:40:25.927: E/MediaPlayer(15355): error (1, -2147483648)
  03-07 14:40:25.927: D/PlaySong(15355): Error in Playing song java.io.IOException: Prepare failed.: status=0x1

1 个答案:

答案 0 :(得分:1)

我只建议您在上一个问题中编写此代码,因此下面可能是导致问题的主要问题。

尝试声明

files = new File(Environment.getExternalStorageDirectory()+ "/Audio File/");

作为

private static final String files = new String(
            Environment.getExternalStorageDirectory() + "/Audio File/");

您已将路径声明为文件,因此请使用String更改它,然后尝试。