我正在開發一個音樂播放器的應用程序。在這裏我將檢索SD卡中可用的所有音樂文件並顯示在列表中。 爲了播放歌曲,列表中會有一個播放按鈕。 最初,所有的圖像將是「PLAY BUTTON」狀態。 更改圖像的按鈕在列表中查看它在android
然後當用戶點擊播放按鈕時,播放按鈕將變爲此。
和音樂將起到/暫停相應。
但是,我面對的問題是,如果我播放第一首歌曲,該歌曲播放並且圖標發生變化,但是如果向下滾動列表,則「PAUSE」按鈕圖像會隨機出現在名單。
下面是代碼: (其仍在當談到播放音樂,但我定義來解決這個問題,圖像更新發育)
MusicActivity:
package sample.music;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MusicActivity extends Activity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;
private static LayoutInflater inflater = null;
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mMediaPlayer.release();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_music_grid();
}
private void init_phone_music_grid() {
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(getApplicationContext()));
musiclist.setDividerHeight(50);
// musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
static class ViewHolder {
TextView textView;
ImageView imageView;
}
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();
String id = null;
View vi = convertView;
final int muscPosition = position;
final ViewHolder viewHolder;
if (convertView == null) {
inflater = MusicActivity.this.getLayoutInflater();
vi = inflater.inflate(R.layout.item, null);
}
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) vi.findViewById(R.id.text);
viewHolder.imageView = (ImageView) vi.findViewById(R.id.image);
//final ImageView tempView = viewHolder.imageView;
viewHolder.imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(muscPosition);
String filename = musiccursor
.getString(music_column_index);
try {
if (mMediaPlayer.isPlaying()) {
Toast.makeText(MusicActivity.this,
"mMediaPlayer.isPlaying()",
Toast.LENGTH_LONG).show();
try {
mMediaPlayer.stop();
mMediaPlayer.release();
viewHolder.imageView.setImageResource(R.drawable.play_button);
//notifyDataSetChanged();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(MusicActivity.this, e + "",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MusicActivity.this,
"mMediaPlayer.start", Toast.LENGTH_LONG)
.show();
viewHolder.imageView.setImageResource(R.drawable.pause_button);
//notifyDataSetChanged();
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (Exception e) {
Toast.makeText(MusicActivity.this, e + ""+".....",
Toast.LENGTH_LONG).show();
}
}
});
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);
vi.setTag(viewHolder);
viewHolder.textView.setText(id);
return vi;
}
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/PhoneMusicList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="50dip" android:src="@drawable/play_button" android:scaleType="centerCrop"/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:layout_gravity="left|center_vertical" android:textSize="20dip" android:layout_marginLeft="10dip"/>
</LinearLayout>
您的CursorAdapter是否在光標索引或ListView位置繪製「正在播放」圖標? (前者是正確的。)這是我要檢查的第一件事。 – Sparky 2012-03-20 13:23:33
如果你這樣做,所以PLZ發給我演示我發現這很多很長的時間回覆 – 2013-12-11 11:05:53