Nikhil Agrawal
Nikhil Agrawal

Reputation: 26518

Open mp3 files in android default media player on the same activity

I am developing an Android application in which I am showing a ListView of mp3 files from the sd card.

Now what I want is that when user clicks on any of the mp3 files it should start playing there on the same Activity. The code which I am using presently is not working for me to play audio files.

My code:

Uri uri = Uri.parse("/sdcard/music/sample.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

But this is something that I want. This is a screen Shot form android gingerbread

enter image description here

Upvotes: 3

Views: 3449

Answers (2)

Jevgeni Smirnov
Jevgeni Smirnov

Reputation: 3797

I suppose you should use another approach in case you want audio played in the same activity. I'd better initialize MediaPlayer object in this activity or as a background service.

In case of any questions feel free to ask.

UPDATE 1

In general case as I said you either need the MediaPlayer object in Activity or Music Service.

Now lets talk about controls.

You can simply put into your layout the block with controls and show/hide it when needed. MediaPlayer provides several convenient callbacks, so you will be able to update progressbar.

So, sum up:

  1. Include controls into layout.
  2. Include MediaPlayer into activity or make it work as a Service.
  3. Bond controls to player.
  4. ???
  5. PROFIT

Upvotes: 1

Developer
Developer

Reputation: 291

see this link here is a good example of getting all the music files and show in list and play when you click any item...

  1. Display MusicList and play on itemClick

  2. Another link

Upvotes: 3

Related Questions