0

在我目前正在编写的应用程序中,用户可以从数据库中选择一个条目并播放该条目的内容:一个条目由许多声音文件组成(没有限制)。在我的应用程序中,我在一个列表中返回一个条目(已存储在我的数据库中)的声音文件的 URI 位置。代码如下:

public void audioPlayer() {
    // set up MediaPlayer
    MediaPlayer mp = new MediaPlayer();
    DatabaseHandler db = new DatabaseHandler(this);
    Entry retrieveEntry = new Entry();

    retrieveEntry = db.getEntry();

    List<String> path = retrieveEntry.getAudioUri();

    path.size();

    System.out.println("PATH SIZE: " +path.size());

    System.out.println("FILEZ: " + path);

    Iterator<String> i = path.iterator();
    String myAudio;
    int count = 0;

    while (i.hasNext()) {
        System.out.println(count);
            myAudio = i.next();
            System.out.println("MY AUDIO: " + myAudio);

            MediaPlayer player = MediaPlayer.create(this, Uri.parse(myAudio));
            player.start();
            player.stop();
            player = MediaPlayer.create(this, Uri.parse(myAudio));
            player.start();

            count++;
        }
}

我的用户要求有用户输入来播放文件 - 有没有办法播放第一个文件,然后等待用户按下按钮,然后播放第二个文件,然后等待用户按下按钮等.? 此刻,当按下播放按钮时,所有返回的声音文件都会同时播放,而不是一个接一个。

提前感谢您提供的任何帮助!

4

2 回答 2

2

您可以使用此类播放播放列表。这将开始一个音频,当该音频结束时,它将开始播放下一个音频,直到列表末尾。如果你想循环播放播放列表,即在到达结束后开始第一个音频,然后传入 isLooping=truestartPlayingPlaylist(list,looping)

    AudioPlayer player = new AudioPlayer();
    player.startPlayingPlaylist(list, false);

班级

public class AudioPlayer{
    MediaPlayer player = null;
    ArrayList<String> playlist = null;
    int position = 0;

    public AudioPlayer() {
        super();
        // TODO Auto-generated constructor stub
    }
    public void startPlayingPlaylist(ArrayList<String> list, boolean looping){
        playlist = list;
        if(player!=null){
            player.release();
        }
        if(playlist!=null && playlist.size()>0){
            player = MediaPlayer.create(LMApplicaton.getInstance(),Uri.parse(playlist.get(position)));
            player.setWakeMode(LMApplicaton.getInstance(), PowerManager.PARTIAL_WAKE_LOCK);
            player.setLooping(looping);
            player.start();
            // Set onCompletion listener
            player.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    position = position+1;
                    if(position<playlist.size()){
                        try {
                            player.reset();
                            player.setDataSource(playlist.get(position));
                            player.prepareAsync();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    } else if(player.isLooping()==true){
                        position = position%playlist.size();
                        try {
                            player.reset();
                            player.setDataSource(playlist.get(position));
                            player.prepareAsync();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    else if(player.isLooping()==false){
                        player.release();
                        player = null;
                    }
                }
            });
            player.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    player.start();
                }
            });
        }
    }
    public void pause(){
        if(player!=null && player.isPlaying()){
            player.pause();
        }
    }
    public void play(){
        if(player!=null && player.isPlaying()==false){
            player.start();
        }
    }
    public boolean isPlaying(){
        return player.isPlaying();
    }
    public void release(){
        if(player!=null){
            player.release();
        }
    }

}

编辑: 下面的类接收音频列表,然后播放第一个音频。它在用户呼叫时播放下一个音频startNextAudio()您可以根据您的要求使用其中任何一个

公共类 AudioPlayer{ MediaPlayer player = null; ArrayList 播放列表 = null; 整数位置 = 0;

public AudioPlayer() {
    super();
    // TODO Auto-generated constructor stub
}
public void startPlayingPlaylist(ArrayList<String> list){
    playlist = list;
    if(player!=null){
        player.release();
    }
    if(playlist!=null && playlist.size()>0){
        player = MediaPlayer.create(LMApplicaton.getInstance(),Uri.parse(playlist.get(position)));
        player.setWakeMode(LMApplicaton.getInstance(), PowerManager.PARTIAL_WAKE_LOCK);
        player.start();
        // Set onCompletion listener
        player.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                player.start();
            }
        });
    }
}
public void startNextAudio(){
    position = position+1;
    if(position<playlist.size()){
        try {
            player.reset();
            player.setDataSource(playlist.get(position));
            player.prepareAsync();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } else if(player.isLooping()==true){
        position = position%playlist.size();
        try {
            player.reset();
            player.setDataSource(playlist.get(position));
            player.prepareAsync();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }else{
        Log.i("AudioPlayer","Playlist reached at the end");
    }
}
public void pause(){
    if(player!=null && player.isPlaying()){
        player.pause();
    }
}
public void play(){
    if(player!=null && player.isPlaying()==false){
        player.start();
    }
}
public boolean isPlaying(){
    return player.isPlaying();
}
public void release(){
    if(player!=null){
        player.release();
    }
}

}

于 2012-04-15T14:58:39.053 回答
1

一种方法是实现 MediaPlayer.OnCompletionListener 接口。这为您提供了 MediaPlayer.onCompletion() 回调方法,您可以像这样使用它:

@Override
public void onCompletion(MediaPlayer mp) {
  if (i.hasNext) {
    // ...hand mp the next file
    // ...show the user the 'play next' button
  }
}

请注意,您还需要MediaPlayer.setOnCompletionListener()在设置中调用该方法。

于 2012-04-15T15:05:58.097 回答