当我点击播放按钮更多然后在我的MP3播放器中我的应用程序崩溃
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当我点击播放按钮更多然后在我的MP3播放器中我的应用程序崩溃相关的知识,希望对你有一定的参考价值。
这是片段活动,我已经创建了一个播放在线mp3的播放器,当我第一次点击播放按钮并且我启动音频并且如果我暂停它暂停时,它工作正常。
问题是当我点击播放按钮时,一旦应用程序崩溃。请帮忙
public class ListenFragment extends Fragment {
final String url[] = {
"HTTP://counterexample"};
private MediaPlayer mediaPlayer;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(fragment_listen, container, false);
ImageButton btn_play = rootView.findViewById(R.id.btn_play);
ImageButton btn_pause = rootView.findViewById(R.id.btn_pause);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudiostreamType(AudioManager.STREAM_MUSIC);
btn_pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.pause();
}
});
btn_play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
mediaPlayer.setDataSource(String.valueOf(url[0]));
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
});
return rootView;
}
}
这是log cat,请查看并回答。
答案
警告:使用setDataSource()时,必须捕获或传递IllegalArgumentException和IOException,因为您引用的文件可能不存在。
这就是事情。您必须捕获IllegalArgumentException
,因为您尝试加载的文件可能不存在,因为您从在线服务器获取它。用以下代码替换您的代码:
btn_play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
mediaPlayer.setDataSource(String.valueOf(url[0]));
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException | IllegalArgumentException e) {
e.printStackTrace();
}
}
});
此外,我不知道为什么你使用字符串数组而不是普通的字符串。阅读更多
更新您可以使用以下代码段在音乐开始播放时在应用栏中显示通知:
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if (mp.isPlaying()){
//Show notification if music have started play
showNotif(context, CHANNEL_ID)
}
}
});
public void showNotif(Context context, String CHANNEL_ID){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_bubble_notif)
.setContentTitle("New Item Remind!")
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(context.getString(R.string.notif_msg, reminder.getNumberOfItems()))
.setPriority(NotificationCompat.PRIORITY_HIGH);
createNotificationChannel(context);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(0, mBuilder.build());
}
private void createNotificationChannel(Context context) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "CHANEL_NAME";
String description = "CHANNEL_DESC";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
有关在状态栏HERE中显示通知的更多信息
以上是关于当我点击播放按钮更多然后在我的MP3播放器中我的应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章