1
的單擊事件播放音樂我也跟着鏈接的CoverFlow實施如何從SD卡上的CoverFlow圖像
http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html
這裏有onclick事件我想從SD卡播放歌曲,我的意思是在點擊的每個圖像coverflow我想播放來自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);
}
}
}
}
謝謝對於快速回復,但我有點困惑,將歌曲存儲在列表中並播放他們與覆蓋流的onclick。我會試試,同時你可以稍微回答一下你的回答。再次感謝 – Randroid 2012-04-27 12:11:41
我的意思是,你可以有歌曲名稱列表.... 這樣你就可以根據該列表 – MAC 2012-04-27 12:13:27
您可以發佈您的代碼,您是如何播放的歌曲播放歌曲.... ,所以我可以解釋... – MAC 2012-04-27 12:14:24