【问题标题】:when i hit play button more then once in my mp3 player my application crashes当我在我的 mp3 播放器中多次点击播放按钮时,我的应用程序崩溃了
【发布时间】:2018-08-31 12:44:55
【问题描述】:

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

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

【问题讨论】:

  • 你遇到了什么错误。你能把你的错误日志贴在这里吗?
  • 已发布日志,请帮忙

标签: java android performance android-fragments crash


【解决方案1】:

注意:您必须捕获或传递 IllegalArgumentException 和 使用 setDataSource() 时出现 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();
                }

            }
        });

此外,我不知道您为什么使用字符串数组而不是普通字符串。阅读更多


更新 当音乐开始播放时,您可以使用以下 sn-p 在应用栏中显示通知:

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

【讨论】:

  • 我正在使用字符串数组,否则我无法直播此链接。请帮助改进这一点
  • 首先,如果答案解决了问题,接受它。稍后我会为您重构代码。
  • 未解决 :( 现在应用程序在真实设备上崩溃但在虚拟设备上运行良好。请帮助
  • 我可以粘贴指向该问题的链接
  • 问题出在媒体播放器和我使用暂停按钮的方式上,现在我做了它并改进了代码,现在它运行良好。
猜你喜欢
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2023-03-28
  • 1970-01-01
  • 2021-10-16
  • 2020-10-15
相关资源
最近更新 更多