我正在开发一个应用程序,其中包含一个列表视图屏幕,其中包含来自服务器的所有铃声。单击任何铃声后,它必须开始播放该铃声,然后单击其旁边的下载按钮,则必须开始下载该铃声铃声,而不会干扰媒体播放器的播放。
这个我要工作的是,但是CheckedTextView
旁边有下载按钮时,铃声不会播放。
我要做的就是在列表项单击上启动MediaPlayer
,再次单击时暂停它,然后在该项目旁边单击下载按钮时下载铃声。
在播放过程中,为了显示当前播放的性质,我使用了CheckedTextView
,当它只是要在列表视图中被充气的项目时,它可以正常工作。但是,当我在其旁边添加下载按钮时,它不会响应点击。
我也查看了this链接和this链接,但在那里找不到任何帮助。
这是我的 RingtonesActivity.java 的片段,其中存在mediaPlayer
listView_ringtones.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listView_ringtones.setItemChecked(position, true);
url = ringtoneList.get(position).getUrl();
ringtoneTitle = ringtoneList.get(position).getTitle();
pDialog = ProgressDialog.show(activityContext, "Starting Ringtone: ", ringtoneTitle);
if (mediaPlayer != null) {
mediaPlayer.stop();
}
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
itemClicked = true;
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
mp.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
}
});
}
});
这是我的 RingtoneAdapter.java
的getView()
方法
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.ringtone_list_item, parent, false);
holder = new ViewHolder();
holder.checkedTextView = convertView.findViewById(R.id.ringtone_checked);
holder.download = convertView.findViewById(R.id.ringTones_download);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String downloadFileUrl = ringtoneList.get(position).getUrl();
downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(downloadFileUrl);
DownloadManager.Request request = new DownloadManager.Request(uri);
Toast.makeText(context, "Downloading...", Toast.LENGTH_SHORT).show();
request.setTitle(ringtoneList.get(position).getTitle());
request.setDescription("in progress..");
request.setDestinationInExternalPublicDir("/AvengersRingtoneDownloads",
ringtoneList.get(position).getTitle() + ".mp3");
request.setVisibleInDownloadsUi(true);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadManager.enqueue(request);
}
});
String title = ringtoneList.get(position).getTitle();
String duration = "\nDuration:" + ringtoneList.get(position).getDuration();
SpannableStringBuilder builder = new SpannableStringBuilder();
Spannable span = new SpannableString(title);
span.setSpan(new RelativeSizeSpan(1.0f), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new StyleSpan(Typeface.BOLD), 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new ForegroundColorSpan(Color.rgb(16, 96, 122)), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(span);
SpannableString blueSpannable = new SpannableString(duration);
blueSpannable.setSpan(new ForegroundColorSpan(Color.rgb(23, 163, 209)), duration.indexOf(duration), duration.length(), 0);
builder.append(blueSpannable);
holder.checkedTextView.setText(builder);
return convertView;
}
这是我的 listview_item.xml
<CheckedTextView
android:id="@+id/ringtone_checked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:drawableStart="@drawable/checked_state_list_song"
android:gravity="center_vertical"
android:textSize="17sp"
xmlns:android="http://schemas.android.com/apk/res/android" />
这是我的列表视图定义
<ListView
android:id="@+id/listView_ringtones"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sultan_taunting"
android:background="?attr/selectableItemBackground"
android:choiceMode="singleChoice" />
谁能告诉我我在做什么错?因为我已经在互联网上搜索了很长时间,还没有找到解决方法或正确的解决方案。