Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5997065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:12:34+00:00 2026-05-23T00:12:34+00:00

I have a service bound to an activity. The activity is a ListView of

  • 0

I have a service bound to an activity. The activity is a ListView of playable files. The service plays a certain audio file, passed from the Activty. In the previous version I hadn’t had the Service bind, so when clicking multiple times on a play element, multiple instances of sounds would occur. I thought I could solve this by binding the service, so I would communicate every time with the same instance, however it still plays multiple files if they are clicked. Maybe i understood the concept of binding wrong, I’m not sure, the android documentation is sometimes a bit vague and misleading. Here is my code, thanks for any input.

Activity:

public class ViewSounds extends ListActivity {

  private PlayService myService;

  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setVolumeControlStream(AudioManager.STREAM_MUSIC);

  // Bind Service
  Intent intent = new Intent(ViewSounds.this, PlayService.class);       
  getApplicationContext().bindService(intent, serviceConncetion, BIND_AUTO_CREATE); 

  // Get list vars
  String[] lex_names = getResources().getStringArray(R.array.lex_names);
  //final String[] lex_files = getResources().getStringArray(R.array.lex_files);

  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_sounds, lex_names));  
  final ListView lv = getListView();
  lv.setTextFilterEnabled(true);

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                   

    //int playFile = getResources().getIdentifier(lex_files[position], "raw", getPackageName());   
     myService.playAudio();

    }
  });   
  }  

  private ServiceConnection serviceConncetion = new ServiceConnection() { 

   @Override
   public void onServiceConnected(ComponentName name, IBinder service) { 
         // TODO Auto-generated method stub 
         myService = ((PlayService.MyBinder)service).getService();                   
         } 

         @Override
         public void onServiceDisconnected(ComponentName name) {
         // TODO Auto-generated method stub 
         myService = null; 
         } 
        }; 

         @Override
         public void onDestroy(){ 
         super.onDestroy(); 
         unbindService(serviceConncetion);    
         }
 }

Service:

public class PlayService extends Service {

MediaPlayer player;

private final IBinder binder = new MyBinder();

@Override
public IBinder onBind(Intent intent) {
    return binder;
}       

public class MyBinder extends Binder {
     PlayService getService() {
        return PlayService.this;
    }
}

@Override
public void onCreate() {
    super.onCreate();
}

public void playAudio(){   
            // I hardcoded the file name for this preview

    player = MediaPlayer.create(this, R.raw.no_harm);
    player.setLooping(false); // Set looping
    if (!player.isPlaying()) {      
    player.start(); 
    }
 }

public void onStart(Intent intent, int startId){
    super.onStart(intent, startId);
    Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();

    // Get veriable from Activity
    // int extras; 
    // extras = intent.getExtras().getInt("playFile");          

}
@Override
public void onDestroy() {
    super.onDestroy();
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:12:35+00:00Added an answer on May 23, 2026 at 12:12 am

    You should stop playback of previous file before playing another file. In your code you actually creating new MediaPlayer for each new media file.

    Here is how your playAudio should look like:

    public void playAudio(){   
       // I hardcoded the file name for this preview
    
       if(player != null){
           player.release();
       }
    
       player = MediaPlayer.create(this, R.raw.no_harm);
       player.setLooping(false); // Set looping
       if (!player.isPlaying()) {      
          player.start(); 
       }
    }
    

    But you should avoid creation of new media players for each new playback. You could do this next way:

    public void playAudio(){   
       // I hardcoded the file name for this preview
    
       if(player == null){
            player = new MediaPlayer();
       }
    
       if(player.isPlaying()){  
            player.stop();
       }
    
       player.reset();
       player.setLooping(false);
    
       player.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
            @Override
            void onPrepared(MediaPlayer mp){
                mp.play();
            }
       });       
       player.prepareAsync(); 
    }
    

    You should also use onSetErrorListener(...) and onSetComlpletionListener(...) for more control over the player. So that your activity can react on state changes in MediaPlayer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListBox that is being bound to data from a WCF service.
I have a service bound to an activity. The service will access a JSON
I have an activity called message.java which is bound to the service GPS.java. The
I have an Activity and Service bound to it. If Service is killed by
I have an android service, I bound it to an activity, and unbound on
I have an Android activity in which I have a ListView bound to a
I have some problem with calling web service from flex. I have service with
I have an Activity communicating with and directly accessing a background Service's data members
I have an Activity with a list that is bound to a ListAdapter reading
I have a background service that is getting updates from the location manager every

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.