Trying to make a MediaPlayer work on with a ListView however it doesn't seem to play when I press the item on the ListView. Not sure what I'm doing wrong. Any help would be great. Kinda new to this sorry if its obvious.
public class soundboard2 extends AppCompatActivity implements AbsListView.OnScrollListener {
private int lastTopValue = 0;
private ImageView backgroundImage;
private ArrayAdapter adapter;
private String[] mFragmentTitles;
private ListView mDrawerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewsound);
final MediaPlayer sound01 = MediaPlayer.create(this, R.raw.sound01);
mDrawerList = (ListView)findViewById(R.id.list);
mFragmentTitles = getResources().getStringArray(R.array.fragments);
adapter = new ArrayAdapter(this, R.layout.list_row, mFragmentTitles);
mDrawerList.setAdapter(adapter);
// inflate custom header and attach it to the list
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.custom_header, mDrawerList, false);
mDrawerList.addHeaderView(header, null, false);
// we take the background image and button reference from the header
backgroundImage = (ImageView) header.findViewById(R.id.listHeaderImage);
mDrawerList.setOnScrollListener(this);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView a, View v, int position, long id) {
if (mDrawerList.getItemAtPosition(position)=="Player") {
try {
sound01.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound01.start();
sound01.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
}
});
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Rect rect = new Rect();
backgroundImage.getLocalVisibleRect(rect);
if (lastTopValue != rect.top) {
lastTopValue = rect.top;
backgroundImage.setY((float) (rect.top / 2.0));
}
}
}
Strings
<string-array name="fragments">
<item>Player</item>
<item>two</item>
<item>three</item>
</string-array>