2014-11-10 2 views
0

다음과 같이 목록보기가 설정되어 있습니다. 각 행에 '재생'버튼이 있습니다. 미디어 플레이어가 두 개의 오디오 파일 (하나씩)을 재생해야합니다. 이 재생 버튼 클릭.Listview 버튼 클릭시 두 개의 오디오 파일 재생

public class WordsActivity extends SherlockActivity{ 
    Category currentCat; 
    ArrayList<Word> words; 
    String catID; 
    MediaPlayer m; 

    protected void onCreate(Bundle savedInstanceState) { 
     ... 
     words = myDbHelper.getAllWordsforCat(catID); 
     wordListAdapter adapter = new wordListAdapter(this, words); 
     ListView listView = (ListView) findViewById(R.id.words_list); 
     listView.setAdapter(adapter); 
    } 

    ... 

    public class wordListAdapter extends ArrayAdapter<Word>{ 

     private final Context context; 
     private final ArrayList<Word> itemsArrayList; 

     public wordListAdapter(Context context, ArrayList<Word> itemsArrayList) { 

      super(context, R.layout.words_row, itemsArrayList); 

      this.context = context; 
      this.itemsArrayList = itemsArrayList; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      final Word word = itemsArrayList.get(position); 
      LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View rowView = inflater.inflate(R.layout.words_row, parent, false); 

      ... 
      ImageView playbtn = (ImageView) rowView.findViewById(R.id.word_play_btn); 
      playbtn.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        playfile(word.getengSound()); 
       } 
      }); 

      ... 

      return rowView; 
     } 

     public void playfile(String filename) { 
      try { 
       if (m.isPlaying()) { 
        m.stop(); 
        m.release(); 
        m = new MediaPlayer(); 
       } 
       m = new MediaPlayer(); 
       AssetFileDescriptor descriptor = getAssets().openFd(filename); 
       m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength()); 
       descriptor.close(); 

       m.prepare(); 
       m.setVolume(1f, 1f); 
       m.start(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 


} 


} 

를 사용하면 오디오 파일이 자산 번들의 일부입니다 볼 수 있듯이, 위의 코드는 완료 첫 떨어져 재생한다, 두 번째 (() word.getFreSound)를 첫 번째 오디오 파일을 재생합니다.

답변

0

"Word"개체의 새 ArrayList를 가져 와서 각 항목을 클릭하고 ObjectList (wrt 인덱스)라는 단어를 ArrayList (A PLayList처럼)에 추가합니다 .Play Music (playfile() 메서드 사용) ArrayList의 객체입니다 (다음 활동으로 이동하면 목록을 지울 수 있습니다).