简体   繁体   中英

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

在此输入图像描述

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM