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?