问题描述
我很困惑这两个类的。
我有一个问题,我有1000的的.wav 的文件,这取决于用户加载不同的声音。
I have a problem that I have 1000 of .wav files, it depends on user to load different sounds.
还有,用户可以连续玩很多的声音,像4依次声音。
as well as, user can play many sounds in a row, like 4 sounds sequentially.
所以,我应该使用? 的Soundpool
是wav文件更好,但它并不好,它的负载,并保持加载的文件。
so which should I use? SoundPool
is better for wav files but it is not good that it loads and keep the files loaded.
对于这种情况任何建议?
any recommendation for this situation?
推荐答案
我已经在用我的方式的MediaPlayer
做到了,感谢@blipinsk,看完这个答案由他在上面的评论计算器的建议。
I have done it on my way using MediaPlayer
, Thanks for @blipinsk, After i read this answer StackOverFlow suggested by him in the comment above.
我的文件有点大了的Soundpool可以容忍,还有,我想连续播放多个文件。我不得不使用线程的Soundpool自己来实现它。相反,它准备在MediaPlayer的使用OnCompletionListener。所以,我用MediaPlayer的。
My files are a bit larger that SoundPool can tolerate, as well as, I want to play many files sequentially. Which i had to implement it myself using threads in SoundPool. On the contrary, It is ready in MediaPlayer using OnCompletionListener. So that, I used MediaPlayer.
其实我想的Soundpool与线程,它的工作原理,但由于它不支持大型媒体文件,我使用的媒体播放器。
Actually i tried SoundPool with threads, it works but since it does not support large media files, i used Media Player.
我写了这个类,裹在MediaPlayer运行播放列表,你可以添加到播放列表和媒体播放器将运行它们一个接一个。所以这里是类:
I wrote this class which wrap the MediaPlayer to run a playList, you can add to the playlist and the media player will run them one after another. so here is the class:
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());
}
}
}
我奋斗了它一段时间。我希望这会帮助别人。
I struggled for a while with it. I hope it will help others.
注意:我用的LinkedBlockingQueue 以保持播放列表曲目它,因为它的实现是线程安全的。
NOTE: I used LinkedBlockingQueue to keep the playList tracks in it, because it is implemented to be thread safe.
如果你想使用这个类的主题,我建议你使用的ExecutorService
如果u将使用它在多线程应用程序。
If you want to use this class in threads, i suggest you to use the executorService
if u will use it in multithreaded app.
这篇关于的Soundpool或MediaPlayer的? - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!