【问题标题】:Audio starts on list item click how to stop?音频从列表项开始单击如何停止?
【发布时间】:2012-12-06 18:49:41
【问题描述】:

当用户从列表中选择一个项目时播放音频,我有三个按钮一个是停止播放音频,另外两个是一个播放列表中的下一个项目,一个播放上一个项目

我将如何实现这一目标?我已经使按钮可点击,然后我尝试编写代码,例如。 mp.stop();停止音乐,但这不起作用?以及如何让其他按钮播放列表中的下一个和上一个项目?

下面是我的 .java 文件

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"},};





}

这是我的 nasheed2.xml 文件之一:

    <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>

这是另一个 nasheed1.xml 文件:

    <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>

【问题讨论】:

    标签: android-layout android-listview android-audiomanager


    【解决方案1】:

    play button点击事件上试试下面的代码。

    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;
                    }
                }
            });
    

    或为停止音频创建另一个按钮。

    点击那个停止按钮使用这个

    mp.stop();
    

    【讨论】:

    • 抱歉,我应该在媒体播放器启动的情况下或公共 void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3) {
    • 是的,在媒体播放器启动时单击项目并显示媒体播放器屏幕而不是在该屏幕上单击按钮后,您可以使用此代码。
    • 我似乎无法让它工作。它无法识别“mp”,这是因为我在 onItemClick 案例中创建了媒体播放器:0?
    • 我假设我还需要实现 onclicklistener?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    相关资源
    最近更新 更多