Android mp3 player to play list of songs

后端 未结 6 1051
梦如初夏
梦如初夏 2021-02-03 16:27

I need the sample code of an mp3 player in android to play more than one file. i.e Song should be played one after the other from a particular folder in our system.

Can

相关标签:
6条回答
  • 2021-02-03 16:35

    You could check out the source code for the music player that ships with Android as well. https://android.googlesource.com/platform/packages/apps/Music/

    0 讨论(0)
  • 2021-02-03 16:39

    A complete mp3 player ready to be integrated in your project: github.com/avafab/URLMediaPlayer

    0 讨论(0)
  • 2021-02-03 16:40

    simple functionality media player, with most of the functions,

    class represent the song

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

    function that return all the media files mark as mp3 and add them to array

    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();
                }
            }
        }
    

    shared interface between the player and the activity

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

    class represent the media player

    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;
        }
    }
    

    class represent the main activity

    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);
    }
    }
    

    good luck!

    0 讨论(0)
  • 2021-02-03 16:50

    Good Tutorial for Basic Start

    • http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/
    • http://alucard1990.hubpages.com/hub/How-to-Make-a-Simple-Media-Player-for-Android
    • http://www.hrupin.com/2010/12/simple-android-mp3-media-player
    • http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-i
    • http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/

    Android Developer Reference

    • http://developer.android.com/reference/android/media/MediaPlayer.html
    • http://developer.android.com/guide/topics/media/mediaplayer.html
    0 讨论(0)
  • 2021-02-03 16:54

    This is a very good example of an easy to understand mp3 player: https://github.com/saquibhafiz/MP3Player

    Or if you would prefer a tutorail:

  • http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/
  • http://www.hrupin.com/2010/12/simple-android-mp3-media-player
0 讨论(0)
  • 2021-02-03 16:57

    At some point, you are going have to deal with this when it comes to playing music on Android.

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

    0 讨论(0)
  • 提交回复
    热议问题