单击列表项时播放声音文件

时间:2012-11-11 01:12:21

标签: android android-listview android-mediaplayer

我想显示用户可以点按以播放相关声音文件的声音列表。我有两个类(AndroidListViewActivity,SingleListItem)我想在单击按钮时播放声音文件。(所选项目)当我点击时我收到一条错误消息。它不起作用,请帮助我。

AndroidListViewActivity.class(用于列表和选择项目)

SingleListItem.class(显示所选项目)

按钮单击(在SingleListItem布局上,播放所选声音)

Logcat消息:

11-11 16:18:09.285: E/dalvikvm(17161): VM aborting
11-11 16:18:09.285: A/libc(17161): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)

AndroidListViewActivity类;

public class AndroidListViewActivity extends ListActivity {
    int [] sounds;
 MediaPlayer mediaPlayer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
sounds= new int[] {R.raw.sound1,R.raw.sound2,R.raw.sound3,R.raw.sound4,R.raw.sound5};        

    // storing string resources into Array
    String[] sesler = getResources().getStringArray(R.array.sounds);        
    // Binding Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label,   sounds));

    ListView lv = getListView();

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {     

          // selected item 
          String product = ((TextView) view).getText().toString();
          String selectedFromList = getItemAtPosition(position);

            @SuppressWarnings("unused")
            int resId = getResources().getIdentifier(selectedFromList, "raw", getPackageName());

  // Launching new Activity on selecting single List Item
          Intent i = new Intent(getApplicationContext(), SingleListItem.class);
          Intent intent = new Intent(getApplicationContext(), SingleListItem.class);
          // sending data to new activity
          i.putExtra("product", product);
          intent.putExtra("position", position);
          startActivity(i);
          startActivity(intent);            
      }

    private String getItemAtPosition(int position) {
        // TODO Auto-generated method stub
        return null;
    }
    });
}
}

SingleListItem类;

public class SingleListItem extends Activity{

 int [] sounds;
 MediaPlayer mediaPlayer;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sounds= new int[]   {R.raw.sound1,R.raw.sound2,R.raw.sound3,R.raw.sound4,R.raw.sound5};

           this.setContentView(R.layout.single_list_item_view);

    TextView txtProduct = (TextView) findViewById(R.id.product_label);

    Intent i = getIntent();

    Intent intent = getIntent();

    // getting attached intent data
    String product = i.getStringExtra("product");

    int position = intent.getExtras().getInt("position");
    // displaying selected product name
    txtProduct.setText(product);
    mediaPlayer = MediaPlayer.create(this, sesdosya[position]); 
    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener(){

     public void onClick(View arg0) {                            
    mediaPlayer.start();   
    //start the progress dialog
     }
    }); 
 }    
}

0 个答案:

没有答案