0

我正在使用添加多个音频文件

    try {
        MediaPlayer mp = MediaPlayer.create(context, R.raw.play);
        audioArr[0] = mp;
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

                mp.start();

            }
        });

        mp.prepare();

    } catch (Exception e) {
        e.printStackTrace();
    }

并在功能中发布媒体

for (MediaPlayer media : audioList) {
        if (media != null) {
            if(media.isPlaying())
            media.stop();
            media.release();
            media = null;
        }
    }

仍然无法在我的应用程序中播放多个音频文件,第一次播放音频,所以请指导

4

3 回答 3

0

尝试这个 :

void addSongs()
{
    File songs = new File(sdPath); //sdPath is the path of your audio files

    if(songs.listFiles(new SongFilter()).length > 0)
    {
        for(File file : songs.listFiles(new SongFilter()))

        {
            vector.add(file.getName());
        }

        arrayAdapter.setNotifyOnChange(true); //arrayAdapter Array Adapter
        songList.setAdapter(arrayAdapter); //songList is a ListView
    }
}

SongFilter 类是这样的:

class SongFilter implements FilenameFilter
{
    public boolean accept(File dir, String filename) 
    {
        return (filename.endsWith(".mp3"));
    }       
}
于 2013-09-24T06:30:48.533 回答
0
buttonSound.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// play another sound here.do your stuff whatever you want to do
mp.stop();
}
});
于 2013-09-24T06:31:01.057 回答
0
    public class AudioActivity extends Activity implements MediaPlayer.OnCompletionListener {
         int [] songs;
         MediaPlayer mediaPlayer;
         int current_index = 0; 
        @Override
        protected void onCreate(Bundle savedInstanceState) {##}
        private void start(){
            songs= new int[] {R.raw.s1,R.raw.s2,R.raw.s3,R.raw.s4};
            mediaPlayer = MediaPlayer.create(this, songs[0]);
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.start();
        }

        @Override
        public void onCompletion(MediaPlayer mp) {
**// This is the completion listener registered with mediaplayer instance.**            
playNextSong();
        }
         private void playNextSong()
            {
                current_index = (current_index +1)% songs.length;
                AssetFileDescriptor afd = this.getResources().openRawResourceFd(songs[current_index]);
                if(current_index!=0){
                try
                {   
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    afd.close();
                }
                catch (IllegalArgumentException e){}
                catch (IllegalStateException e){}
                catch (IOException e){}
             }else{
                 mediaPlayer.stop(); 
             }
            }


    }
于 2013-09-24T06:32:01.037 回答