在这里,我正在尝试制作一个使用API工作的音乐流媒体应用程序,并且我已经使用了Android中提供的MusicPlayer类。这里我要做的是从列表视图中获取API的响应,然后单击列表视图中的任何项目来流式播放该特定歌曲。这些歌曲存在于服务器上,我从服务器得到正确的响应,音乐流也根据我的说法正确实现。在SeekBar的帮助下显示音乐进度,我100%确定它也按照我的意愿工作。
我最近添加的新内容是,每当我点击列表中的特定歌曲项目时,该项目左侧的耳机图标就会变为播放图标,如下面的屏幕截图所示
Screenshot of the app from my phone
此处,歌曲左侧的播放图标显示最近播放,但我只想播放当前播放的歌曲,而不是最近播放的歌曲。
但我无法想办法做到这一点,我尝试了一些像
这样的事情但是第3人没有工作,并且没有找到实现第1和第2的方法......
ListView实现的代码片段
CustomSongList adapter = new CustomSongList(Songs.this, songdetails);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
url = songdetails.get(position).getUrl(); // your URl
pos = position;
ImageView listenImage = (ImageView) view.findViewById(R.id.musicicon);
listenImage.setImageResource(R.drawable.play); /* I am changing icon from headphone icon to play icon here*/
if (mediaPlayer != null) {
mediaPlayer.stop();
}
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaFileLengthInMilliseconds = mediaPlayer.getDuration();// gets the song length in milliseconds from URL
mediaPlayer.start();
imPlay.setVisibility(View.INVISIBLE);
imPause.setVisibility(View.VISIBLE);
primarySeekBarProgressUpdater();
}
});
以下是ListView Adapter类的代码片段。
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.songlist_item, null, true);
TextView Title = (TextView) rowView.findViewById(R.id.title);
TextView Singer = (TextView) rowView.findViewById(R.id.singer);
TextView Duration = (TextView) rowView.findViewById(R.id.duration);
Title.setText(songlist.get(position).getTitle());
Singer.setText("Singer : " + songlist.get(position).getSinger());
Duration.setText("Duration : " + songlist.get(position).getDuration()+" mins");
return rowView;
}
这是歌曲布局文件的片段
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/seekBar"
android:layout_below="@+id/songheading"
android:layout_margin="10dp"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:divider="@drawable/transperent_color"
android:dividerHeight="5dip" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/buttonPlay" />
<ImageView
android:id="@+id/buttonPlay"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/play" />
<ImageView
android:id="@+id/buttonPause"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/pause" />
<ImageView
android:id="@+id/buttonStop"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="@drawable/stop" />
为listview的每个项目设置的耳机图标由以下代码段
完成 <ImageView
android:id="@+id/musicicon"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/app_name"
android:gravity="center"
android:layout_marginTop="10dp"
android:src="@drawable/listen_icon" />
修改
应用@KalaBalik提供的解决方案
感谢@KalaBalik发布标记歌曲的步骤,但我已经逐步应用了您的步骤,我发现我的应用现在没有显示列表本身,加载完成后,这里是截图
以下是我所说的对代码执行的编辑
listview的代码段
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/seekBar"
android:layout_below="@+id/songheading"
android:layout_margin="10dp"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:choiceMode="singleChoice" // -----here
android:divider="@drawable/transperent_color"
android:dividerHeight="5dip" />
CheckedTextView实现
<CheckedTextView
android:id="@+id/tvChecked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/playing" />
checkedTextview的复合drawable,
playing.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/play" android:state_checked="true" />
<item android:drawable="@drawable/listen_icon" />
SpannableStringBuilder实现
String finalString = title + "\n" + singer + "\n" + duration;
SpannableStringBuilder sb = new SpannableStringBuilder();
Typeface exoMedium = Typeface.createFromAsset(context.getAssets(), "fonts/Exo-Medium.ttf");
TypefaceSpan exoMediumSpan = new CustomTypeFaceSpan("", exoMedium);
sb.setSpan(exoMediumSpan, finalString.indexOf(title), title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sb.setSpan(new ForegroundColorSpan(Color.BLUE), finalString.indexOf(singer), singer.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sb.setSpan(new ForegroundColorSpan(Color.CYAN), finalString.indexOf(duration), duration.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
我使用this链接
创建了一个CustomTypeFaceSpan在checkedTextView holder.checkedTextView.setText(sb);
中设置SpannableStringBuilder我在这里使用ViewHolder模式
并在单击项目时将listview设置为true
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
list.setItemChecked(position, true);
但结果在上面的截图中,告诉我我做错了什么。
答案 0 :(得分:1)
你需要的是以某种方式标记或&#34;检查&#34;这首歌正在播放。例如,可以使用CheckedTextView
来完成此操作。
将其放入ListView XML:android:choiceMode="singleChoice"
在ListItem XML中,我建议只有一个CheckedTextView
。您仍然可以通过以下方式进行格式化:(a)使用复合可绘制的图像(例如:android:drawableStart="@drawable/playing"
)和(b)以编程方式将文本设置为SpannableStringBuilder
的字符串。这样,您可以在一个字符串中使用不同的字体大小,样式和颜色,这些字符串放在(一)CheckedTextView
中。从理论上讲,您可以使用更复杂的listItem布局,但它会获得complicated quickly。
在您的听众中,您只需检查当前位置的项目,如下所示:list.setItemChecked(position, true);
现在(在初始化之后)你总是只有一个检查列表项,你所要做的就是对它作出反应。您可以使用复合drawable上的state list来完成此操作。该状态列表将保存关于何时(在哪种状态下:已检查或未检查)将显示一个或另一个可绘制的指令。
为了使ListView
有效运行,建议(但不是必须)实施ViewHolder
模式,如下所示:
适配器内部:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkedTextView.setText(filteredList.get(position));
return convertView;
}
static class ViewHolder {
final CheckedTextView checkedTextView;
private ViewHolder(View convertView) {
checkedTextView = (CheckedTextView) convertView.findViewById(R.id.listentry);
}
}
答案 1 :(得分:1)
您链接的代码不包含我提议的所有更改。例如,songlist_item不引用选择器。最重要的是,我不想处理MediaPlayer
准备回调的细节。因此,我提出了一个最小但完整且可验证的问题示例。请分别测试和理解代码,然后将其编入您的项目中。
MainActivity:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = findViewById(R.id.list);
ArrayList<String> songdetails = new ArrayList<>();
songdetails.add("Song1");
songdetails.add("Song2");
songdetails.add("Song3");
CustomArrayAdapter adapter = new CustomArrayAdapter(this, R.layout.listitem, songdetails);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
listView.setItemChecked(i, true);
// the selected song can be found at position i of songdetails
// start playing the selected song here
}
});
}
}
CustomArrayAdapter:
public class CustomArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final int resource;
private ArrayList<String> songlist;
CustomArrayAdapter(Activity context, int resource, ArrayList<String> songlist) {
super(context, resource);
this.context = context;
this.resource = resource;
this.songlist = songlist;
}
@Override
public int getCount() {
return songlist.size();
}
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
final ViewHolder holder;
String song = songlist.get(position);
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(resource, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkedTextView.setText(song);
return convertView;
}
static class ViewHolder {
final CheckedTextView checkedTextView;
private ViewHolder(View convertView) {
checkedTextView = convertView.findViewById(R.id.tvChecked);
}
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice" />
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvChecked"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableStart="@drawable/checkedstatelist" />
并且checkstatelist.xml(驻留在res / drawable /中):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:drawable/ic_media_play" android:state_checked="true" />
<item android:drawable="@android:drawable/ic_media_pause" android:state_checked="false" />
</selector>
这就是结果: