简体   繁体   English

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

[英]when i hit play button more then once in my mp3 player my application crashes

This is fragment activity and i have created a player to play online mp3 it works fine when i hit play button first time and i start the audio and if i pause it pauses. 这是片段活动,我创建了一个播放器来在线播放mp3,当我第一次按下“播放”按钮并启动音频时,如果我暂停播放,它会正常播放。

Problem is when i hit play button more then once application crashes. 问题是,当我再按一次播放按钮时,应用程序崩溃了。 please help 请帮忙

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;
    }
}

here is log cat, please review and answer. 这是日志猫,请查看并回答。

Caution: You must either catch or pass IllegalArgumentException and IOException when using setDataSource(), because the file you are referencing might not exist. 警告:使用setDataSource()时,必须捕获或传递IllegalArgumentException和IOException,因为您所引用的文件可能不存在。

Here is the thing. 这是东西。 You must catch IllegalArgumentException too because the file you're trying to load might not be in existence since you're getting it from an online server. 您还必须捕获IllegalArgumentException因为您要加载的文件可能不存在,因为您是从联机服务器获取文件的。 Replace your code with the following: 用以下代码替换您的代码:

   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();
                }

            }
        });

Furthermore, I don't know why you're using string array instead of normal string. 此外,我不知道为什么要使用字符串数组而不是普通字符串。 Read more 阅读更多


UPDATE You can show notification in your app bar when the music has started playing using below snippet: 更新当音乐开始播放时,您可以使用以下片段在应用栏中显示通知:

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);
            }
        }

More on displaying notification in status bar HERE 更多关于在状态栏中显示通知HERE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MP3播放器,播放按钮没有声音 - MP3 Player, play button has no sound 当我按下按钮时,我的 Android 应用程序崩溃了 - My Android App crashes when I hit the button 没有文件扩展名的 MP3 文件如何在 Android 文件应用程序中播放,有什么方法可以在我的 MP3 播放器 Android 应用程序中播放这些文件? - How can MP3 files without a file extension play in the Android files app and is there any way I can play such files in my MP3 player Android app? 当我点击“注册”按钮时,我的应用程序停止并退出 - My Application Stops and Exits when I hit the Register button 当我在其中一个活动上按下BACK按钮时,我的应用程序崩溃了 - My app crashes when I hit BACK button on one of my activities 我正在用Java制作命令行音乐播放器,而“跳过”按钮有时会跳过多次 - I'm making a command line music player in java, and my 'skip' button is sometimes skipping more than once 为什么我的Android MP3 Player代码不起作用? - Why does my Android MP3 Player code not work? 单击再次播放按钮时我的整个应用程序崩溃 [关闭] - My Whole App Crashes When I Click My Play Again Button [closed] MP3歌曲无法在媒体播放器中播放 - MP3 songs do not play in media player 按下按钮时可以用来播放mp3的一种方法是什么? - What is one method I can use to play an mp3 when a button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM