我按照coverflow实施的链接
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);
}
}
}
}