Android播播放完SD卡指定文件夹音乐之后,自动播放下一首

最近做一个项目,需要连续播放音乐,播放完一首歌之后,自动播放完下一首歌。不要重复播放。

代码如下:

package com.example.asyncplayer_ex;



import java.io.IOException;

import java.util.ArrayList;



import javax.security.auth.PrivateCredentialPermission;



import android.app.Activity;

import android.drm.DrmStore.Playback;

import android.media.AsyncPlayer;

import android.media.AudioManager;

import android.media.MediaPlayer;

import android.media.MediaPlayer.OnCompletionListener;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;



public class MainActivity extends Activity {

    private static final String TAG = "MP3";

    private AsyncPlayer asyncPlayer = new AsyncPlayer(null); 

    private String path;

    private MediaPlayer mediaPlayer;

    private int songIndex = 0;

    private ArrayList<String> songArrayList; //播放声音列表

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        mediaPlayer = new MediaPlayer();

        mediaPlayer.setOnCompletionListener(new CompletionListener());

        path = Environment.getExternalStorageDirectory().toString() + "/mp3/Voice/";

        songArrayList = new ArrayList<String>();

        songArrayList.add(path + "1.MP3");

        songArrayList.add(path + "2.MP3");

        songArrayList.add(path + "3.MP3");

        songArrayList.add(path + "4.MP3");

        songArrayList.add(path + "5.MP3");

        songArrayList.add(path + "6.MP3");

        songplay();        

    }

    

    private final class CompletionListener implements OnCompletionListener{



        @Override

        public void onCompletion(MediaPlayer mp) {

            nextsong();            

        }        

        

    }

    private void nextsong() {

        

        if (songIndex < songArrayList.size() - 1) {    

            songIndex = songIndex + 1;

            songplay();            

        }

        else {

            songArrayList.clear();

            songIndex = 0;



        }

        

        

    }

    private void songplay() {

        try {

            mediaPlayer.reset();

            mediaPlayer.setDataSource(songArrayList.get(songIndex));

            mediaPlayer.prepare();

            mediaPlayer.start();

        } catch (IllegalArgumentException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (SecurityException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IllegalStateException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        

    }



    @Override

    protected void onDestroy() {

        mediaPlayer.release();

        mediaPlayer = null;

        super.onDestroy();

    }

}

 

你可能感兴趣的:(android)