java - 当我再按一次播放按钮,然后在mp3播放器中一次,我的应用程序崩溃

标签 java android performance android-fragments crash

这是片段 Activity ,我创建了一个播放器来在线播放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;
    }
}

这是日志猫,请查看并回答。

最佳答案

Caution: You must either catch or pass IllegalArgumentException and IOException when using setDataSource(), because the file you are referencing might not exist.



这是东西。您还必须捕获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

关于java - 当我再按一次播放按钮,然后在mp3播放器中一次,我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52115759/

相关文章:

java - 当仅知道对象的抽象基类时如何复制对象

Java 全局 isEmpty() 方法

android - 有没有办法通过adobe air访问android上的蓝牙

android - 无法解析类 org.jetbrains.plugins.gradle.tooling.internal.ExtraModelBuilder

c++ - 具有相互独立线程的多线程开销

java - ViewPagerIndicator - 将 TabPageIndicator 设置到中心

java - 根据 fxml、java fx 中子 UI 的点击更新父级 UI

android - 在运行时为 Android 4.0 添加 TLSv1.2 支持

performance - mongodb更新导致读取速度极慢

java - VisualVM 中的 "Total loaded"是什么意思?