Audio file is not playing

174 views Asked by At

I am creating one application which record voice and store it in SD card. then i open
this file and want to play or some other option by clicking on that respective file.
But when i click on that list item it'd not playing. I don't understand where is the problem. Here is my code.

 @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;
          }
    }
}  

Please give me any hint. Thanks in Advance..

1

There are 1 answers

0
Lawrence Gimenez On BEST ANSWER

How about you switch the two

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

to this

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

or put the MediaPlayer reference inside the onItemClick()