等到当前媒体播放完之后再播放另一个Android版

时间:2014-05-25 14:13:21

标签: java android android-mediaplayer

这是我在android中的媒体播放器代码。我的问题是循环播放zipfolder中的所有文件而不等待当前完成播放。我该如何解决这个问题。我是第一个做我的第一个项目的新手!需要一些专家帮助。

//代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.io.IOUtils;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button playButton;
    private MediaPlayer mp;    
    private static final String MAIN_TAG ="ERROR";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         try {
             super.onCreate(savedInstanceState); 
        //final String file_loc= Environment.getExternalStorageDirectory().toString();
        //Log.i("location",file_loc);
             ZipFile zip = new ZipFile("/storage/emulated/0/AjeshDocument/sample.zip");

             for(int i=1;i<9;i++){

             ZipEntry entry = zip.getEntry("sample/rihanna_"+i+".mp3");                        
             if (entry != null) {
                 InputStream in = zip.getInputStream(entry);
                 // see Note #3.
                 File tempFile = File.createTempFile("_AUDIO_", ".wav");
                 FileOutputStream out = new FileOutputStream(tempFile);
                 IOUtils.copy(in, out);

                 // do something with tempFile (like play it)
                 File f = tempFile;   
                 try {
                     if (f.exists())
                     {
                         Log.i(MAIN_TAG,"Audio file found!");
                         MediaPlayer mp = new MediaPlayer();
                         FileInputStream fis = new FileInputStream(f);
                         mp.setDataSource(fis.getFD());

                      //  if (mp.isPlaying() == false) {                        
                         mp.prepare();
                         //mp.setLooping(false);
                         mp.start();                         
                        // mp.stop();
                         //mp.release();

                         mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                             public void onCompletion(MediaPlayer mp2) {
                                 mp2.release();

                             };
                         });
                      // }  
                         Log.i(MAIN_TAG,"Song finished!");
                     }  


                   else
                     {
                         Log.i(MAIN_TAG,"File doesn't exist!!");
                     }

                 }
                 catch (IOException e)
                 {
                     Log.i(MAIN_TAG,e.toString());
                 }
             }
             else {
                 // no such entry in the zip
             }
            } //for end
            // mp.release();

         }  
             catch (Exception e) {
             // handle your exception cases...

             Log.i(MAIN_TAG,e.toString());

         }       

    }

    @Override
    protected void onResume() {
        Log.w("Info", "App Resume");

        super.onResume();
    }

    @Override
    protected void onStop() {
        Log.w("Info", "App stopped");

        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.w("Info", "App destoryed");

        super.onDestroy();

    }

}

1 个答案:

答案 0 :(得分:0)

这是因为您正在为每首歌创建一个新的mp实例。首先,您应该创建一个Song类来保存歌曲信息。或者,您可以创建一个包含歌曲文件路径的字符串列表。然后,你可以这样做:

int playlistPos = 0;

protected void onCreate(Bundled savedInstanceState) {
    super.onCreate(savedInstanceState);
    initSong(songs.get(playlistPos);
    mp.start();

    mp.setOnCompletionListener(new OnCompletionListener() {
        playlistPos++;
        initSong(songs.get(playlistPos);
        mp.start();
    });
}

private void initSong(Song song) {
    // Set the datasource in here, prepare, etc
}

每首歌曲完成后,它会将播放列表向前移动一个,然后再次启动播放器。