• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should an App's background music have a thread of it's own?

 
Raed Tabani
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to build an app where a piece of Music is played whenever the onCreate() is called, and I want for this music to be played throughout the whole app ie across Activities. what I've done so far is to create a Thread,within the OnCreate(), and I called it backgroundMusic, inside of it I created A MediaPlayer Object from a music piece in my Raw Folder. given that the music itself only runs for a minute or so and the average time spent on the app is more, I made a while Loop that as long as the app is running checks if the music is playing, if not then call start();


the code runs just fine, however I did notice some Lag especially later when some other Threads and Images gets loaded.what I want to ask, is this an appropriate way to play background Music? is there a better more efficient way of doing that?
also, how do I make the music stop as the app closes?
 
Darryl Burke
Bartender
Posts: 5149
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What class is the variable music? If it's android.media.MediaPlayer there's a method setLooping(...).
 
Raed Tabani
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's is a MediaPlayer Object and I just updated the code, thanks man . although I did ask this question on stackOverflow, and as for where to play the background music, It was clear to me that a Service is best given that the music is independent of an activity's life cycle. however, I also learned that Service runs on the UI thread and I was wondering if starting a thread within the service would help reduce load on the main thread and make app run smoother?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic