0

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.

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.

1 Answers1

0

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

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. 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

devmike01
  • 1,971
  • 1
  • 20
  • 30
  • i'm using string array because otherwise i can't stream this link live. please help improving this –  Sep 02 '18 at 06:36
  • First, If the answer solved the problem, accept it. I'd refactor the code for you later. – devmike01 Sep 02 '18 at 19:48
  • not solved :( now app crashes on real device but runs fine on virtual device. please help –  Sep 03 '18 at 17:23
  • i can paste link to that question –  Sep 04 '18 at 17:15
  • https://stackoverflow.com/questions/52135022/my-application-crashes-after-showing-the-splash-screen-on-a-real-device-but-not –  Sep 04 '18 at 17:23
  • Kindly help @DevMike –  Sep 11 '18 at 19:45
  • 1
    problem was in the media player and the way i used the pause button, now i made it and improved the code and now it runs good. –  Sep 11 '18 at 23:37
  • can you help me showing notification of that mp3 in status bar when it plays? –  Sep 11 '18 at 23:40
  • Yes. I can't post codes in the comment section. So I'll share a link to one of my open source project where i use the notification api. Here it is. https://github.com/devmike01/ItemsReminder/blob/master/app/src/main/java/com/example/oluwagbenga/reminder/views/utils/AlarmReceiver.java. – devmike01 Sep 11 '18 at 23:46
  • Also, I have updated my answer with what you requested for @SmartoDev – devmike01 Sep 12 '18 at 00:01
  • You are really great and helpful <3 –  Sep 12 '18 at 20:25
  • I'm glad my answer helps. – devmike01 Sep 14 '18 at 09:42