Reputation: 253
I'm very new to Java and Android development and can't seem to find what I'm after. I have a basic list set up, each list item has a different title, description and icon. What I'd like is when a list item is clicked, a sound is played, this sound would be different for each list item.
Here's my code so far:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class TestingList extends ListActivity {
private static final String ICON_KEY = "icon";
private static final String TITLE_KEY = "title";
private static final String DETAIL_KEY = "detail";
private static final int[] ICONS = new int[] { R.drawable.lista,
R.drawable.listb, R.drawable.listc, R.drawable.listd };
private static final String[] TITLES = new String[] { "List 1", "List 2",
"List 3", "List 4" };
private static final String[] DETAILS = new String[] {
"List 1 description",
"List 2 description",
"List 3 description",
"List 4 description" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
for (int i = 0; i < ICONS.length; i++) {
rows.add(createListItemMap(ICONS[i], TITLES[i],
DETAILS[i]));
}
String[] fromKeys = new String[] { ICON_KEY, TITLE_KEY, DETAIL_KEY };
int[] toIds = new int[] { R.id.icon, R.id.text1, R.id.text2 };
setListAdapter(new SimpleAdapter(this, rows,
R.layout.icon_detail_list_item, fromKeys, toIds));
}
public static Map<String, Object> createListItemMap(int icon,
CharSequence title, CharSequence detail) {
Map<String, Object> row = new HashMap<String, Object>();
row.put(ICON_KEY, icon);
row.put(TITLE_KEY, title);
row.put(DETAIL_KEY, detail);
return row;
}
public static void showView(Context c) {
c.startActivity(new Intent(c, TestingList.class));
}
}
Help much appreciated, thank you.
Upvotes: 1
Views: 3730
Reputation: 1059
You are already half way the only thing you have to add is as follows.
1.You need to chose where to put your audio files you want to play , you have two option to embed your audio file in the application as a resource or to put audio files on external file storage.
2.Override onListItemClick event as shown below
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
//Place your code here
}
3.Option one create raw folder in your resource directory and import all your audio to this directory, and you will get the as a resource like "R.raw.audio1" . and place this code in the event above.
MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.audio1);
mPlayer .start();
4.Option two create folder on your external storage and put your audio files and you will get your audio file by path using environment variable.
File externalStorage= Environment.getExternalStorageDirectory();
String path=externalStorage.getAbsolutePath() + "/your_audio_directory/adio1.mp3";
MediaPlayer mPlayer = new MediaPlayer();
mPlayer .setDataSource(path);
mPlayer .prepare();
mPlayer .start();
Upvotes: 3