1

Here i am displaying all songs in on a listview ,i want to play that song based on onItemClick so suggest me how to do it based on my code here is my code to display list of audio files from SDcard

public class ListFileActivity extends ListActivity implements OnClickListener {

private String path;
MediaPlayer mda;
int current_index2 = 0;
String filename;
Button backToRecord, allvoices;
ToggleButton activateDelete;
ListView myList;
List values;
ArrayAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.soundrecords);
     setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    backToRecord = (Button) findViewById(R.id.buttonrecord);
    activateDelete = (ToggleButton) findViewById(R.id.buttonedit);
    allvoices = (Button) findViewById(R.id.buttoncontinuous);
    myList = (ListView) findViewById(android.R.id.list);
    backToRecord.setOnClickListener(this);
    activateDelete.setOnClickListener(this);
    allvoices.setOnClickListener(this);

    // Use the current directory as title
    path = "/sdcard/";
    if (getIntent().hasExtra("path")) {
        path = getIntent().getStringExtra("path");
    }
    setTitle(path);

    // Read all files sorted into the values-array
    values = new ArrayList();
    File dir = new File(path);
    if (!dir.canRead()) {
        setTitle(getTitle() + " (inaccessible)");
    }
    final String[] list = dir.list();
    if (list != null) {
        for (String file : list) {
            if (file.contains(".3gp")) {
                values.add(file);
            }
        }
    }
    Collections.sort(values);
    // Put the data into the list
    adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2,
            android.R.id.text1, values);
    setListAdapter(adapter);

    registerForContextMenu(myList);
}

1 Answers1

2

Use onItemClick Listener and use a MediaPlayer object the play the audio from the file from the clicked item's uri

mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // TODO Auto-generated method stub

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(path+mylist.getItemAtPosition(position).toString());
mp.prepare();
mp.play();

    }
});

}

insomniac
  • 11,146
  • 6
  • 44
  • 55
  • i Know this ,http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/ this explains my solutions,but could you check my code and update your ans –  Dec 21 '13 at 06:01
  • Yeah wait till i type this – insomniac Dec 21 '13 at 06:09
  • What is the Path? try to System.out.println(path+mylist.getItemAtPosition(position).toString()) and comment it – insomniac Dec 21 '13 at 06:38
  • Here whay is mylist? ,if i changed to myList then it is showing create a methos with name setDataResource. –  Dec 21 '13 at 06:56
  • myList is the name of your listView and i forgot to add mp.setDataSource(); – insomniac Dec 21 '13 at 07:01
  • When i changed mylist to myList then it is showing above error –  Dec 21 '13 at 07:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43654/discussion-between-insomniac-and-prabha) – insomniac Dec 21 '13 at 07:02