0

我正在创建一个记录语音并将其存储在 SD 卡中的应用程序。然后我打开
这个文件并想通过单击相应的文件来播放或其他选项。
但是,当我单击该列表项时,它不会播放。我不明白问题出在哪里。这是我的代码。

 @Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.listofaudiorecord);
      init_phone_music_grid();
}

private void init_phone_music_grid() {
      System.gc();
      String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
      musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
 proj, null, null, null);
      count = musiccursor.getCount();
      musiclist = (ListView) findViewById(R.id.PhoneMusicList);
      musiclist.setAdapter(new MusicAdapter(getApplicationContext()));

      musiclist.setOnItemClickListener(musicgridlistener);
      mMediaPlayer = new MediaPlayer();
  }

   private OnItemClickListener musicgridlistener = new OnItemClickListener() {
      public void onItemClick(AdapterView parent, View v, int position,
          long id) {
            System.gc();
            music_column_index = musiccursor
        .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
            musiccursor.moveToPosition(position);
            String filename = musiccursor.getString(music_column_index);

                  try {
                       if (mMediaPlayer.isPlaying()) {
                            mMediaPlayer.reset();
                       }
                       mMediaPlayer.setDataSource(filename);
                       mMediaPlayer.prepare();
                       mMediaPlayer.start();
                 } catch (Exception e) {

                 }
           }
        };

        public class MusicAdapter extends BaseAdapter {
          private Context mContext;

          public MusicAdapter(Context c) {
                mContext = c;
          }

          public int getCount() {
                return count;
          }

          public Object getItem(int position) {
                return position;
          }

          public long getItemId(int position) {
                return position;
          }

          public View getView(int position, View convertView, ViewGroup parent) {
                System.gc();
                TextView tv = new TextView(mContext.getApplicationContext());
                String id = null;
                if (convertView == null) {
                      music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                      musiccursor.moveToPosition(position);
                      id = musiccursor.getString(music_column_index);
                      music_column_index = musiccursor
                 .getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
                      musiccursor.moveToPosition(position);
                      id += " Size(KB):" + musiccursor.getString(music_column_index);
                      tv.setText(id);
                } else
                      tv = (TextView) convertView;
                return tv;
          }
    }
}  

请给我任何提示。提前致谢..

4

1 回答 1

0

换两个怎么样

musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();

对此

mMediaPlayer = new MediaPlayer();
musiclist.setOnItemClickListener(musicgridlistener);

或将 MediaPlayer 引用放在onItemClick()

于 2012-08-03T09:58:37.180 回答