I'm trying to write a simple audio player that plays files from the res/raw folder in the background.

There are two classes.

In MyService , in the OnCreate method, the Mediaplayer starts:

 public class MyService extends Service { MediaPlayer mp; final String TAG = "AUDIOPLAYER"; @Override public void onCreate() { super.onCreate(); mp = MediaPlayer.create(this, R.raw.fawaid_1); playSound(); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } public class MusicBinder extends Binder { MyService getService() { return MyService.this; } } public void pausePlayer(){ mp.pause(); } public void playSound(){ mp.start(); } 

In the MainActivity the pausePlayer() method from the MyService class is MyService on the button.

MainActivity code:

 public class MainActivity extends ActionBarActivity { TextView curTimeText; Button playB; Button seekB; Button pauseB; SeekBar seekPlayer; Context c; final String TAG = "AUDIOPLAYER"; Handler handler; SharedPreferences sp; String spStr = "bookSettings"; private MyService myService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button playB = (Button) findViewById(R.id.playB); Button pauseB = (Button) findViewById(R.id.pauseB); Button seekB = (Button) findViewById(R.id.seekB); SeekBar seekPlayer = (SeekBar) findViewById(R.id.seekPlayer); Intent intent = new Intent(this, MyService.class); startService(intent); MyService myService = new MyService(); pauseB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { MyService service = new MyService(); service.pausePlayer(); } }); } private ServiceConnection musicConnection = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { MyService.MusicBinder binder = (MyService.MusicBinder)service; //get service myService = binder.getService(); } @Override public void onServiceDisconnected(ComponentName name) { } }; 

When you start the application, the audio starts to play, but when you press pauseB, the program throws a NullPointerException error:

 07-20 19:09:50.395 4153-4153/? W/System.err﹕ java.lang.NullPointerException 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at ru.grawor.islamicaudiobooks.MyService.pausePlayer(MyService.java:40) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at ru.grawor.islamicaudiobooks.MainActivity$1.onClick(MainActivity.java:62) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at android.view.View.performClick(View.java:4438) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at android.os.Looper.loop(Looper.java:136) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 07-20 19:09:50.395 4153-4153/? W/System.err﹕ at dalvik.system.NativeStart.main(Native Method) 

How to make so that when pressing pauseB playback was suspended?

    2 answers 2

     MyService service = new MyService(); service.pausePlayer(); 

    Here you create a new object of your Service with this method of using the onCreate method of the service will not be called. In order for this to work, you must use myService which is initialized in the onServiceConnected method. This method will be called after the service is created, that is, including the onCreate method has already been called. Until then, check the verification of the myService field on NPE in onClickListener

      You are not using the service correctly. You should read the documentation on the use of services, this will be enough to solve your problem http://developer.android.com/intl/ru/guide/components/services.html

      Do not call MediaPlayer.create(this, R.raw.fawaid_1); in the onCreate service, it will block the main flow of your program. After reading the documentation on the services you will know that the service is running in the main program thread.

      • Thank you for your reply. For more confidence, can you tell where to call this method (create ())? - Joe Silent
      • @JoeSilent in onStartCommand start a separate stream and call MediaPlayer.create in it. - Nik
      • There are a lot of solutions in Google. Here is one of the options - Mikhail
      • Thank you so much for your help! I'll try to figure it out - Joe Silent