简体   繁体   中英

MediaPlayer Listview onItemClick not playing

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM