1

カバーフロー実装のリンクをたどった

http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html

ここでonclickイベントを使用して、sdcardから曲を再生したい、つまり、カバーフローの各画像をクリックして、sdカードから別の曲を再生したい

あなたのアイデアを共有してください。

ヘルプはいつでも大歓迎です。

前もって感謝します。

      coverFlow.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(CoverAdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

               if (position == 0) {

              // play song number1

            } else if (position == 1) {

            // play song number 2

            }

        }
    });

私はこれのためにこのコードに従っています

   public class Entertainment extends Activity {
ListView musiclist;
TextView CurrentPlying;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;
int seekBarState =0;
ImageButton rewindButton,playButton,forwardButton,stopButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.entertainment);
    list_of_media_files();
    Gallery addsgallery = (Gallery) findViewById(R.id.addsgallery);   
    addsgallery.setAdapter(new AddsViewAdaptor(this)); 
    rewindButton = (ImageButton) findViewById(R.id.rewind);
    playButton = (ImageButton) findViewById(R.id.play);
    forwardButton = (ImageButton) findViewById(R.id.forward);
    stopButton = (ImageButton) findViewById(R.id.stop);
    playButton.setOnClickListener(new PlayButtonListner());
    rewindButton.setOnClickListener(new RewindButtonListner());
    forwardButton.setOnClickListener(new ForwardButtonListner());
    stopButton.setOnClickListener(new StopButtonListner());
}
class PlayButtonListner implements View.OnClickListener{
    public void onClick(View v) {
        try{
            if (mMediaPlayer.isPlaying()) {

                mMediaPlayer.pause();
            }else{
                mMediaPlayer.start();
            }

        }
        catch(Exception ex){
            Log.i("Exception", "Exception in mediaplayer e = " + ex); 
        }
    }
}
public void onInit(int status) {


}

private void list_of_media_files() {
    //System.gc();

    String[] proj = { MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Video.Media.SIZE };
    musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            proj, null, null, null);
    count = musiccursor.getCount();
    musiclist = (ListView) findViewById(R.id.PhoneMusicList);
    musiclist.setAdapter(new MusicAdapter(this));

    musiclist.setOnItemClickListener(musicgridlistener);
    mMediaPlayer = new MediaPlayer();


}

private OnItemClickListener musicgridlistener = new OnItemClickListener() {

    public void onItemClick(AdapterView parent, View v, int position,
            long id) {
        //System.gc();
        music_column_index = musiccursor
        .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        musiccursor.moveToPosition(position);
        String filename = musiccursor.getString(music_column_index);
        //   CurrentPlying =(TextView) findViewById(R.id.currentSong);
        //CurrentPlying.setText(music_column_index);
        try {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.reset();
            }
            mMediaPlayer.setDataSource(filename);
            mMediaPlayer.prepare();
            mMediaPlayer.start();

        } catch (Exception e) {

        }

    }
};

public class MusicAdapter extends BaseAdapter {
    private Context mContext;

    public MusicAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return count;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        System.gc();
        TextView tv = new TextView(mContext);
        String id = null;
        if (convertView == null) {
            music_column_index = musiccursor
            .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
            musiccursor.moveToPosition(position);
            id = musiccursor.getString(music_column_index);
            music_column_index = musiccursor
            .getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
            musiccursor.moveToPosition(position);
            id += " Size(KB):" + musiccursor.getString(music_column_index);
            tv.setText(id);
        } else
            tv = (TextView) convertView;
        return tv;
    }
}

class RewindButtonListner implements View.OnClickListener{
    public void onClick(View v) {
        try{
            if (mMediaPlayer.isPlaying()) {
                seekBarState=mMediaPlayer.getCurrentPosition();
                mMediaPlayer.seekTo(seekBarState-300);
            }else{
                mMediaPlayer.start();
            }

        }
        catch(Exception ex){
            Log.i("Exception", "Exception in mediaplayer e = " + ex); 
        }
    }
}
class ForwardButtonListner implements View.OnClickListener{
    public void onClick(View v) {
        try{
            if (mMediaPlayer.isPlaying()) {
                seekBarState=mMediaPlayer.getCurrentPosition();
                mMediaPlayer.seekTo(seekBarState+300);
            }else{
                mMediaPlayer.start();
            }

        }
        catch(Exception ex){
            Log.i("Exception", "Exception in mediaplayer e = " + ex); 
        }
    }
}
class StopButtonListner implements View.OnClickListener{
    public void onClick(View v) {
        try{
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
                mMediaPlayer.reset();
            };

        }
        catch(Exception ex){
            Log.i("Exception", "Exception in mediaplayer e = " + ex); 
        }
    }
}

}

4

1 に答える 1

0

最初にsdcardからリストまたは曲リストの配列を作成し、位置を使用して曲を渡します

coverFlow.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(CoverAdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

              playSong(list[position]); <---
        }
    });
于 2012-04-27T11:53:35.790 に答える