App stops working when mediaplayer is stopped in S

2019-09-20 02:02发布

问题:

I am making a media player which plays music using an URL. I implemented a foreground service and also a notification. They are working fine. The problem is when I implement .stop function in media player , which is (rathu.stop)in my code, Apps gives an error displaying app stops working. I understand that I have used stop media player function in onDestroy method of service, but I dont know other place to implement that function. Then I have to set that method to a Onclicklistner also. Please help me.

Following are my codes.

MainActivity.java

package com.example.yomal.rathumakarafm;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;

import java.io.IOException;
import java.security.Key;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private SeekBar volumeSeekbar = null;

    private AudioManager audioManager = null;
    int Volume=0;

    ImageView aboutImage;
    private Button buttonStart;
    private Button buttonStop;
    private Button buttonPause;
    public static final String CHANNEL_ID = "exampleServiceChannel";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        setContentView(R.layout.activity_main);
        initControls();

        buttonStart = (Button) findViewById(R.id.buttonStart);
        buttonStop = (Button) findViewById(R.id.buttonStop);
//        buttonPause = (Button)findViewById(R.id.buttonPause);
        aboutImage = (ImageView) findViewById(R.id.about);


        buttonStart.setOnClickListener(this);
        buttonStop.setOnClickListener(this);
        aboutImage.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {


        if (v == buttonStart) {
            startService(new Intent(this, RathuMakara.class));
            buttonStart.setEnabled(false);
            buttonStop.setEnabled(true);
            Toast play = Toast.makeText(getApplicationContext(), "හරි 3, 2, 1 ... සද්දෙට අහමු", Toast.LENGTH_LONG);
            play.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            play.show();


        } else if (v == buttonStop) {
            stopService(new Intent(this, RathuMakara.class));
            Toast play = Toast.makeText(getApplicationContext(), "Stopping Play", Toast.LENGTH_SHORT);
            play.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            play.show();


            buttonStop.setEnabled(false);
            buttonStart.setEnabled(true);
        } else if (v == aboutImage)

            aboutImage = (ImageView) findViewById(R.id.about);


        aboutImage.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent aboutIntent = new Intent(MainActivity.this, AboutActivity.class);
                startActivity(aboutIntent);

            }
        });

        /*else if (v == buttonPause){
            onPause();
        }*/

    }

    private void initControls()

    {

        try {
            volumeSeekbar = (SeekBar) findViewById(R.id.sb);
            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            volumeSeekbar.setMax(audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
            volumeSeekbar.setProgress(audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC));


            volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onStopTrackingTouch(SeekBar arg0) {
                    Toast volume = Toast.makeText(getApplicationContext(), "Volume: " + Integer.toString(Volume), Toast.LENGTH_SHORT);
                    volume.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                    volume.show();

                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0) {
                }

                @Override
                public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                            Volume = progress , 0);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public boolean onKeyDown(int keyCode, KeyEvent event){
        AudioManager audio = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
        int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
            volumeSeekbar.setProgress(currentVolume);
            return false;
        } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
            volumeSeekbar.setProgress(currentVolume);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }



}

RathuMakara.java (Service class)

package com.example.yomal.rathumakarafm;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;
import android.provider.SyncStateContract;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.widget.SeekBar;
import android.widget.Toast;

import java.io.IOException;

import static com.example.yomal.rathumakarafm.MainActivity.CHANNEL_ID;

public class RathuMakara extends Service  {




    private MediaPlayer rathu;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public int onStartCommand(Intent intent,int flags , int startID){

        String url ="http://206.189.34.189:8000/rathumakara.mp3";
        MediaPlayer rathu = new MediaPlayer();
        rathu.setAudioStreamType(AudioManager.STREAM_MUSIC);



        try {
            rathu.setDataSource(url);

            rathu.prepare();

            rathu.start();


            Intent notificationIntent = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,
                    0, notificationIntent, 0);

            Intent playIntent = new Intent(this, MainActivity.class);
           // PendingIntent playPending = PendingIntent.






            Bitmap icon = BitmapFactory.decodeResource(getResources(),
                    R.drawable.logo);


            Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle("රතු මකරා FM \uD83D\uDC32")
                    .setContentText("Now Playing")
                    .setSmallIcon(R.drawable.logo)
                    .setLargeIcon(icon)
                    .setOngoing(true)
//                   .addAction(android.R.drawable.ic_media_play, "Play", )
                    .setContentIntent(pendingIntent)
                    .build();


            startForeground(1, notification);




        }
        catch (IOException e){

            e.printStackTrace();
        }catch (IllegalArgumentException e){
            e.printStackTrace();
        }catch (SecurityException e){
            e.printStackTrace();
        }catch (IllegalStateException e){
            e.printStackTrace();
        }

        return START_NOT_STICKY;




    }



    @Override
    public void onDestroy(){

        rathu.stop();
    }


    
}

Update

LogCat 2018-10-16 23:00:21.892 20437-20437/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.yomal.rathumakarafm:remote, PID: 20437 java.lang.RuntimeException: Unable to stop service com.example.yomal.rathumakarafm.RathuMakara@78b36d4: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference at android.app.ActivityThread.handleStopService(ActivityThread.java:4114) at android.app.ActivityThread.-wrap27(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2025) at android.os.Handler.dispatchMessage(Handler.java:108) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference at com.example.yomal.rathumakarafm.RathuMakara.onDestroy(RathuMakara.java:113) at android.app.ActivityThread.handleStopService(ActivityThread.java:4090) at android.app.ActivityThread.-wrap27(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2025)  at android.os.Handler.dispatchMessage(Handler.java:108)  at android.os.Looper.loop(Looper.java:166)  at android.app.ActivityThread.main(ActivityThread.java:7425)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 

回答1:

Remove the word MediaPlayer from inside onStartCommand(). You declared a local variable with the same name as a member variable, causing the member variable to be "shadowed". That left it uninitialized, which caused an NPE in your onDestroy().