从文件夹插入音频剪辑

时间:2012-10-07 04:06:42

标签: android

我有一个带有5个wav文件的res / raw文件夹,我希望能够在我的应用程序中播放。文件应该在列表视图中播放。我有一定的方式,但我几乎可以肯定有一个更好的方法来做到这一点。这就是我到目前为止所做的。

package churchapp.acosta;

import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class Services extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(this,R.layout.single_item, clips));

    ListView list = getListView();
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub

                            //what do i put here in order for user to play clip when 
                            //a list choice is selected?
                            //i tried the above code below but there has to be a better way?
                            /*MediaPlayer mPlayer2;
            if(position==0)
            {
                mPlayer2= MediaPlayer.create(Services.this, R.raw.clip1);
                        mPlayer2.start();
            }*/ 







        }

    });
}

static final String[] clips= new String[]{
    "This Weeks service","Last Weeks Service","3 Weeks Ago Service","4 Weeks ago Service ","5 Weeks Ago Service"
};

}

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

     MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.apple);
        mp.start();