Farid Ala
Farid Ala

Reputation: 668

Playing music files in Android

I have a list of music files that should be played with my Android application. Actually user selects a list of files to be played for a specified time, and then clicks on 'start button'. Please tell me how I can implement it so that the following requirements are met:

  1. The program should work, even if power button is pressed or the display is turned off.
  2. Music files should be played one by one.

Upvotes: 2

Views: 7901

Answers (2)

Tyler Treat
Tyler Treat

Reputation: 15018

To play audio files, you can use Android's MediaPlayer class.

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(audioFilePath);
mp.prepare();
mp.start();

This code snippet will load and play a single audio file. Take a look at the documentation for MediaPlayer to figure out how you might be able to leverage it for what you're trying to accomplish specifically.

Upvotes: 2

Ted Hopp
Ted Hopp

Reputation: 234857

In order that the music continue after your application activities exit, you need to implement the playing itself as a Service (specifically, a started service). For playing the music, take a look at MediaPlayer. There is a discussion of using a MediaPlayer in a Service in the Media Player guide topic.

Upvotes: 3

Related Questions