Android mp3播放器播放歌曲列表

时间:2010-06-29 09:43:26

标签: android

我需要Android中mp3播放器的示例代码才能播放多个文件。 即歌曲应该从我们系统中的特定文件夹中依次播放。

任何人都可以发布一些示例代码吗?

6 个答案:

答案 0 :(得分:5)

答案 1 :(得分:2)

一个完整的MP3播放器,可以集成到您的项目中: github.com/avafab/URLMediaPlayer

答案 2 :(得分:1)

简单功能的媒体播放器,具有大部分功能,

类代表歌曲

 private class SongDetails {
        String songTitle = "";
        String songArtist = "";
        //song location on the device
        String songData = "";
 }

将所有媒体文件标记为mp3并将其添加到数组

的函数
private void getAllSongs()
    {
       //creating selection for the database
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
        final String[] projection = new String[] {
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.DATA};

        //creating sort by for database
        final String sortOrder = MediaStore.Audio.AudioColumns.TITLE
                + " COLLATE LOCALIZED ASC";

        //stating pointer
        Cursor cursor = null;

        try {
            //the table for query
            Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            // query the db
            cursor = getBaseContext().getContentResolver().query(uri,
                    projection, selection, null, sortOrder);
            if (cursor != null) {

                //create array for incoming songs
                songs = new ArrayList<SongDetails>(cursor.getCount());

                //go to the first row
                cursor.moveToFirst();

                SongDetails details;

                while (!cursor.isAfterLast()) {
                    //collecting song information and store in array,
                    //moving to the next row
                    details = new SongDetails();
                    details.songTitle = cursor.getString(0);
                    details.songArtist = cursor.getString(1);
                    details.songData = cursor.getString(2);
                    songs.add(details);
                    cursor.moveToNext();

                }
            }
        } catch (Exception ex) {

        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

播放器与活动之间的共享界面

public interface OnPlayerEventListener {
    void onPlayerComplete();
    void onPlayerStart(String Title,int songDuration,int songPosition);
}

类代表媒体播放器

public class simplePlayer
{
    OnPlayerEventListener mListener;
    Activity mActivity;

    //give access to the gui
    public ArrayList<songDetails> songs = null;
    public boolean isPaused = false;
    public int currentPosition = 0;
    public int currentDuration = 0;

    //single instance
    public static MediaPlayer player;

   //getting gui player interface
   public  simplePlayer(Activity ma)
   {
       mActivity = ma;
       mListener = (OnPlayerEventListener) mActivity;
   }

    //initialize the player
    public void init(ArrayList<songDetails>_songs)
    {
        songs = _songs;
        currentPosition = 0;


        if(player == null)
        {
            player = new MediaPlayer();
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {

                    player.stop();
                    player.reset();

                    nextSong();
                    mListener.onPlayerSongComplete();

                }
            });
        }
    }

    //stop the current song
    public void stop()
    {
        if(player != null)
        {
            if(player.isPlaying())
                //stop music
                player.stop();

            //reset pointer
            player.reset();
        }
    }

    //pause the current song
    public void pause()
    {
        if(!isPaused && player != null)
        {
            player.pause();
            isPaused = true;
        }
    }

    //playing the current song
    public void play()
    {
        if(player != null)
        {
            if(!isPaused && !player.isPlaying())
            {
                if(songs != null)
                {
                    if(songs.size() > 0)
                    {
                        try {
                            //getting file path from data
                            Uri u = Uri.fromFile(new File(songs.get(currentPosition).songData));

                            //set player file
                            player.setDataSource(mActivity,u);
                            //loading the file
                            player.prepare();
                            //getting song total time in milliseconds
                            currentDuration = player.getDuration();

                            //start playing music!
                            player.start();
                            mListener.onPlayerSongStart("Now Playing: "
                                    + songs.get(currentPosition).songArtist
                                    + " - "+ songs.get(currentPosition).songTitle
                                    ,currentDuration,currentPosition);
                        }
                        catch (Exception ex)
                        {
                            ex.printStackTrace();
                        }
                    }
                }
                else
                {
                    //continue playing, reset the flag
                    player.start();
                    isPaused = false;
                }
            }
        }
    }

    //playing the next song in the array
    public  void nextSong()
    {
        if(player != null)
        {
            if(isPaused)
                isPaused = false;

            if(player.isPlaying())
                player.stop();

            player.reset();

            if((currentPosition + 1) == songs.size())
                currentPosition = 0;
            else
                currentPosition  = currentPosition + 1;

            play();
        }
    }

    //playing the previous song in the array
    public void previousSong()
    {
        if(player != null)
        {
            if(isPaused)
                isPaused = false;

            if(player.isPlaying())
                player.stop();

            player.reset();

            if(currentPosition - 1 < 0)
                currentPosition = songs.size();
            else
                currentPosition = currentPosition -1;

            play();
        }
    }

    //getting new position for playing by the gui seek bar
    public void setSeekPosition(int msec)
    {
        if(player != null)
            player.seekTo(msec);
    }

    //getting the current duration of music
    public int getSeekPosition()
    {
        if(player != null)
            return  player.getDuration();
        else
            return -1;
    }
}

类代表主要活动

public class MainActivity extends ActionBarActivity 
implements OnPlayerEventListener {

simplePlayer player;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //getting all songs in the device
    getAllSongs();

    if (player == null) {
        //create new instance and send the listener
        player = new simplePlayer(this);
        //initialize the simplePlayer
        player.init(songs);
        //start playing the first song
        player.play();

        seekBar.setMax(player.currentDuration);
    }
}

@Override
public void onPlayerSongComplete() {
 //need to be implement!
}

@Override
public void onPlayerSongStart(String Title, int songDuration, int songPosition) {
    this.setTitle(Title);
    seekBar.setMax(songDuration);
    seekBar.setProgress(0);
}
}
祝你好运!

答案 3 :(得分:0)

您也可以查看Android附带的音乐播放器的源代码。 https://android.googlesource.com/platform/packages/apps/Music/

答案 4 :(得分:0)

在某些时候,当你在Android上播放音乐时,你将不得不处理这个问题。

http://developer.android.com/reference/android/media/MediaPlayer.html

答案 5 :(得分:0)