Soundpool或MediaPlayer? - Android

时间:2015-07-30 09:37:53

标签: android audio android-mediaplayer soundpool

我对这两个班很困惑。

我有一个问题,即我有1000个 .wav 文件,这取决于用户加载不同的声音。

同样,用户可以连续播放多个声音,如顺序播放4个声音。

那我该使用哪个? {{1}}对于wav文件更好,但加载和保存文件并不好。

针对这种情况的任何建议?

1 个答案:

答案 0 :(得分:0)

我已经使用MediaPlayer完成了这项工作,感谢@blipinsk,之后我在上面的评论中阅读了他建议的答案StackOverFlow

我的文件比SoundPool可以容忍的大一些,我想要顺序播放很多文件。我必须使用SoundPool中的线程自己实现它。相反,它已在MediaPlayer中使用OnCompletionListener准备就绪。所以,我使用了MediaPlayer。

实际上我尝试使用带线程的SoundPool,但是因为它不支持大型媒体文件,所以我使用了Media Player。

我编写了这个包装MediaPlayer来运行播放列表的类,你可以添加到播放列表中,媒体播放器会一个接一个地运行它们。所以这是课程:

import android.media.MediaPlayer;
import android.os.Environment;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by MBH on 01.08.2015.
 */
public class THSafeListMediaPlayer {
    private static final String TAG_DEBUG = "MBH";
    private String PATH_OF_SOUND_FILES; // I use it because i will put all sound clips in one folder
    // , then i will pass the name of the folder only.
    private LinkedBlockingQueue<String> playList;
    private MediaPlayer mediaPlayer; // The media player to play the sounds, even in background
    private ExecutorService executorService; // For making sure there is only one thread at a time
    // adding to the queue
    private boolean isPaused = false;
    private int pausedPosition = -1;

    /**
     * Constructor will take care of initializing all the important variables
     */
    public THSafeListMediaPlayer() {
        // initializing the variables
        executorService = Executors.newSingleThreadExecutor();
        playList = new LinkedBlockingQueue<>();
        mediaPlayer = new MediaPlayer();
        PATH_OF_SOUND_FILES = Environment.getExternalStorageDirectory().getPath() + "/mbh/sounds/";
    }

    /**
     * It will only add file to the PlayList
     *
     * @param fileName: The file name
     */
    public void addFile(String fileName) {
        // you may add executorService here for safer threads adding here
        // here i use offer, because it is thread safe
        playList.offer(fileName);
    }

    /**
     * It will add file and play the last add file and continue to the play list
     *
     * @param fileName: the file name, playing the soundtrack will start from this file
     */
    public void addFileAndPlay(final String fileName) {
        // For MultiThreaded
//        executorService.submit(new Runnable() {
//            @Override
//            public void run() {
//                playList.offer(fileName);
//                if (!mediaPlayer.isPlaying())
//                    play(playList.poll());
//            }
//        });
        // For single threaded
        playList.offer(fileName);
        if (!mediaPlayer.isPlaying())
            play(playList.poll());
    }

    /**
     * Start playing the play list if there is files in the playlist
     *
     * @return: True if playing successfully done, otherwise, false;
     */
    public boolean play() {
        if (mediaPlayer != null) {
            if (!mediaPlayer.isPlaying()) {
                if (isPaused) {
                    mediaPlayer.seekTo(pausedPosition);
                    mediaPlayer.start();
                    pausedPosition = -1;
                    isPaused = false;
                    return true;
                } else if (!playList.isEmpty()) {
                    play(playList.poll());
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Pause the current played track, if there is track playing
     */
    public void pause() {
        if(isPaused)
            return;
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                pausedPosition = mediaPlayer.getCurrentPosition();
                isPaused = true;
            }
        }
    }

    /**
     * it will play the given file, when it finishes, or fails, it will play the next from the list
     *
     * @param fileName: the file name to start playing from it
     */
    private void play(String fileName) {
        if (mediaPlayer != null) {
            if (!mediaPlayer.isPlaying()) {
                try {
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(fileName);
                    mediaPlayer.prepare();
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            playNextSoundTrack();
                        }
                    });
                    mediaPlayer.start();
                } catch (Exception e) {
                    // TODO: Remove this error checking before publishin

                    // If the current file is not found, play the next track if there is
                    playNextSoundTrack();
                }
            }
        }
    }

    /**
     * this function will be called recursively to play the next track
     */
    private void playNextSoundTrack() {
        if (playList.size() != 0) {
            play(playList.poll());
        }
    }


}

我挣扎了一会儿。我希望它能帮助别人。

注意:我使用 LinkedBlockingQueue 来保留playList轨道,因为它实现为线程安全。

如果你想在线程中使用这个类,我建议你使用executorService,如果你将在多线程应用程序中使用它。