1

In my activity i have:

play.setOnClickListener(new View.OnClickListener() {

        @Override                           
        public void onClick(View v) {

        service.playSong(MEDIA_PATH);

        }
    });

and this is in Service:

void playSong(String file) {
        try {

                mp.reset();
                System.out.println(file+songs.get(currentPosition));
                mp.setDataSource(file+ songs.get(currentPosition));

                mp.prepare();
                mp.start();

                mp.setOnCompletionListener(new OnCompletionListener() {

                     public void onCompletion(MediaPlayer arg0) {
                             nextSong();
                     }
             });

        } catch (IOException e) {
                Log.e(getString(R.string.app_name), e.getMessage());
        }
}

As you see from the code above in my activity when i click the button play I am passing the path into the service in order to play music but it is crashing What is wrong?

user
  • 86,916
  • 18
  • 197
  • 190
  • You should add the stacktrace with the Exception from the Logcat. – user Feb 20 '12 at 17:46
  • Can you please specify it a bit more since I am new into Android?? –  Feb 20 '12 at 17:48
  • I really dont get what are u talking about... can you please show me any example how to do that? –  Feb 20 '12 at 17:51
  • You app is trowing an Exception. In Eclipse go to the Logcat (in the ddms http://developer.android.com/guide/developing/debugging/ddms.html ) and copy the stacktrace containing you exception(the red text). – user Feb 20 '12 at 17:53
  • I have declared ServiceMP3 service; at activity class and from activity i try to acces the method of Service by this: service.playSound() is this correct, what else i have to do? –  Feb 20 '12 at 18:09

1 Answers1

1

You need to bind with the Service before running that.

Check this links:

Community
  • 1
  • 1
Macarse
  • 91,829
  • 44
  • 175
  • 230
  • which of these two is in my case?? i think the second one, right? –  Feb 20 '12 at 18:00
  • is it that prob to call an Service method?? I still cant get it to work –  Feb 20 '12 at 18:05
  • @Samuel: Yes. Look for an example of how to bind to a `Service`. – Macarse Feb 20 '12 at 18:09
  • is it like this?? play= (Button)findViewById(R.id.play); play.setOnClickListener(new StartClick()); private class StartClick implements View.OnClickListener { public void onClick(View v) { Intent intent = new Intent(MainMP3.this,ServiceMP3.class); MainMP3.this.bindService(intent, connection, Context.BIND_AUTO_CREATE); } } –  Feb 20 '12 at 18:31