0

Audio is played when the user selects an item from the list, i have three buttons one is to stop the playing audio and the other two are one to play the next item on the list and one to play the previous

How would i achieve this? i have made the buttons to be clickable and then i try writing the code eg. mp.stop(); to stop the music but this is not working? and also how would i get the other buttons to play next and previous items on the list?

Below is my .java file

public class Nasheeds extends ListActivity {
//ArrayList holds the data (as HashMaps) to load into the ListView
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
//SimpleAdapter does the work to load the data in to the ListView
private SimpleAdapter sa;





@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nasheeds2);



    //HashMap links each line of data to the correct TextView
    HashMap<String,String> item;
    for(int i=0;i<Nasheed.length;i++){
      item = new HashMap<String,String>();
      item.put( "line1", Nasheed[i][0]);
      item.put( "line2", Nasheed[i][1]);

      list.add( item );
    }

    sa = new SimpleAdapter(this, list,
            R.layout.nasheeds1,
            new String[] { "line1","line2" },
            new int[] {R.id.displayname, R.id.title});

    setListAdapter(sa);



  getListView().setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        switch (arg2)
        {
        case 0:
            System.out.println("User selected option 1");
             MediaPlayer mp = MediaPlayer.create(Nasheeds.this,         R.raw.mok);  
              mp.start(); 


            TextView tv=(TextView) findViewById(R.id.selectedfile);
            tv.setText("Playing "+ "Mountains of Mekkah, Zain Bikha");
            break;
        case 1:
            System.out.println("User selected option 2");

        case 2:

            break;                  
        }

    }

  });

}


private String[][] Nasheed =
    {{"Mountains of Mekkah","Zain Bikha"},
    {"Hadith 2","....add hadith...."},
    {"Hadith 3",".....add hadith"},};





}

and this is my one of the nasheed2.xml file:

    <ListView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        android:layout_weight="1.0"
        />

    <LinearLayout
            android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/screen_background_light"
    android:padding="10dip">

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/selectedfile"
            android:text="Not file selected"
            android:textColor="@android:color/black"
            android:gravity="center_horizontal"
            android:singleLine="true"
            android:ellipsize="middle"/>

    <SeekBar
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/seekbar"
            android:max="100"
            android:paddingBottom="10dip"/>

    <LinearLayout
                    android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@android:drawable/screen_background_light">

        <TextView
            android:id="@+id/songCurrentDurationLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

            <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/prev"
                    android:src="@android:drawable/ic_media_previous"
                    android:onClick="doClick"/>

            <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/play"
                    android:src="@android:drawable/ic_media_play"
                    android:onClick="doClick"/>

            <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/next"
                    android:src="@android:drawable/ic_media_next"
                    android:onClick="doClick"/>

            <TextView
                android:id="@+id/songTotalDurationLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

    </LinearLayout> 
    </LinearLayout>

this is the other nasheed1.xml file:

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/displayname"
            android:textSize="18dip"
            android:textStyle="bold"
            android:singleLine="true"
            android:ellipsize="end"/>

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/title"
                    android:textSize="15dip"
                    android:singleLine="true"
                    android:ellipsize="end"
                    android:layout_weight="1.0"/>

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/duration"
                    android:gravity="right"
                    android:textSize="15dip"
                    android:singleLine="true"
                    android:ellipsize="end"/>
    </LinearLayout>

 </LinearLayout>
Jay240692
  • 166
  • 1
  • 5
  • 17

1 Answers1

0

Try this below code on play button click event.

public boolean istrue = true;

btnplay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (istrue) {
                    mp.pause();
                    istrue = false;
                } else {
                    mp.start();
                    istrue = true;
                }
            }
        });

or create one other button for stop audio.

on click that stop button use this

mp.stop();
Arvind Kanjariya
  • 2,089
  • 1
  • 18
  • 23
  • sorry just to clarify should i use this code inside the case where media player starts or after the public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3) { – Jay240692 Dec 07 '12 at 10:58
  • yes after item click when media player start and apper media palyer screen than on that screen on button click you can use this code. – Arvind Kanjariya Dec 07 '12 at 11:21
  • I cant seem to get it to work. it doesn't recognize "mp" this is because i create media player inside the onItemClick case:0 ? – Jay240692 Dec 07 '12 at 11:43
  • i assume i also need to implement onclicklistener? – Jay240692 Dec 07 '12 at 12:32