0

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>
user2407147
  • 1,508
  • 2
  • 22
  • 40
  • Check your `if` condition. More on string comparison [here](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Voicu Feb 15 '16 at 22:17
  • Yes I definitely think its to do with the if condition but not sure how to fix it even after looking at that. Any help? – user2407147 Feb 15 '16 at 22:40

1 Answers1

0

There could be a lot of things going wrong here, but I'll point out two things:

You need to prepare your media before it can be started. You probably don't want to wait until the last second to create and prepare the media before playing it.

You're creating a MediaPlayer in your onCreate but aren't doing anything with it.

Perhaps also consider instead using SoundPool which is more appropriate for shorter clips that need to play very quickly in response to events.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Okay so I've changed it so I'm using the MediaPlayer from the onCreate, however it still doesn't play. Really want to use the MediaPlayer. – user2407147 Feb 15 '16 at 22:31
  • I also have no idea if the media is something that's playable by MediaPlayer. You might also want to check out the media compatibility matrix. http://developer.android.com/guide/appendix/media-formats.html – Doug Stevenson Feb 15 '16 at 22:33
  • It definitely is as I've used it in a similar app with MediaPlay that used buttons instead of listview. – user2407147 Feb 15 '16 at 22:35