通过 ExoPlayer 或其他选项播放多个音频文件?
Play Multiple audio files through ExoPlayer or some other option?
我的问题:我想在我的应用程序中加载 70 种声音,然后播放多种声音(一次最多 8 种声音)。示例应用程序如下:
https://play.google.com/store/apps/details?id=net.relaxio.sleepo
我在我的应用程序中尝试使用 SoundPool 系统,但它在加载时出现问题。 SoundPool 加载 70 个文件大约需要 60 秒以上。所以这个选项不适合我的应用程序。
然后我尝试 MediaPlayer。 MediaPlayer 可以加载,但会出现另一个问题。当我在 mediaplayer 中设置循环时,它会在重新启动之间留出间隙。
然后我找到了一个 github 项目,它一次播放多个声音,而且加载速度很快,但我仍然对这个项目有疑问。它无法正确加载超过 30 个声音。在这个项目中,他们使用 ExoPlayer(下面是 link)。
https://github.com/zoenb/Soft-Sound
请帮我解决声音加载和循环间隙问题。
是的,您必须创建多个 (8) 个 exoplayer 实例,您不能只创建一个实例
这是我的代码,
private void initPlayer() {
if (player == null) {
trackSelectorParameters = new DefaultTrackSelector.ParametersBuilder().build();
TrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory();
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
trackSelector.setParameters(trackSelectorParameters);
RenderersFactory renderersFactory = ((MyAppApplication) context.getApplicationContext()).buildRenderersFactory();
player = ExoPlayerFactory.newSimpleInstance(context, renderersFactory, trackSelector);
player.setPlayWhenReady(true);
//to print log of ExoPlayer
//player.addAnalyticsListener(new EventLogger(trackSelector));
progressiveMediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory);
}
}
使用上面的代码初始化exoplayer,调用此方法将所有8个URI
同时播放
public void startPlaying(Uri uri) {
if (player == null) initPlayer();
MediaSource mediaSource = progressiveMediaSourceFactory.createMediaSource(uri);
player.prepare(mediaSource, true, false);
}
这里是 Github link - https://github.com/google/ExoPlayer
使用exoplayer-core
库
为此,我建议您为 ExoPlayer 创建一个单独的 class 来初始化、启动、停止等等...
public class MyAppExoPlayer {
private final Context context;
private DataSource.Factory dataSourceFactory;
private SimpleExoPlayer player;
private DefaultTrackSelector trackSelector;
private DefaultTrackSelector.Parameters trackSelectorParameters;
private ProgressiveMediaSource.Factory progressiveMediaSourceFactory;
private ConcatenatingMediaSource concatenatingMediaSource;
private int currentMediaPlayerIndex = 0;
public MyAppExoPlayer(Context context) {
this.context = context;
dataSourceFactory = ((MyAppApplication) context.getApplicationContext()).buildDataSourceFactory();
initPlayer();
}
private void initPlayer() {
if (player == null) {
trackSelectorParameters = new DefaultTrackSelector.ParametersBuilder().build();
TrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory();
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
trackSelector.setParameters(trackSelectorParameters);
RenderersFactory renderersFactory = ((MyAppApplication) context.getApplicationContext()).buildRenderersFactory();
player = ExoPlayerFactory.newSimpleInstance(context, renderersFactory, trackSelector);
player.setPlayWhenReady(true);
//to print log of ExoPlayer
//player.addAnalyticsListener(new EventLogger(trackSelector));
progressiveMediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory);
}
concatenatingMediaSource = new ConcatenatingMediaSource();
}
public void startPlaying(Uri uri) {
if (player == null) initPlayer();
MediaSource mediaSource = progressiveMediaSourceFactory.createMediaSource(uri);
player.prepare(mediaSource, true, false);
}
public void addToQ(Uri uri) {
if (player == null) return;
MediaSource mediaSource = progressiveMediaSourceFactory.createMediaSource(uri);
concatenatingMediaSource.addMediaSource(mediaSource);
}
public void stopPlaying() {
if (player != null) {
player.stop(true);
concatenatingMediaSource.clear();
}
}
public long getCurrentPosition() {
if (player != null)
return player.getCurrentPosition();
return 0;
}
public void releasePlayer() {
if (player != null) {
if (trackSelector != null) {
trackSelectorParameters = trackSelector.getParameters();
}
player.release();
player = null;
trackSelector = null;
}
}
}
现在创建 8 个实例 if MyAppExoPlayer
并调用 startPlaying()
我的问题:我想在我的应用程序中加载 70 种声音,然后播放多种声音(一次最多 8 种声音)。示例应用程序如下: https://play.google.com/store/apps/details?id=net.relaxio.sleepo
我在我的应用程序中尝试使用 SoundPool 系统,但它在加载时出现问题。 SoundPool 加载 70 个文件大约需要 60 秒以上。所以这个选项不适合我的应用程序。 然后我尝试 MediaPlayer。 MediaPlayer 可以加载,但会出现另一个问题。当我在 mediaplayer 中设置循环时,它会在重新启动之间留出间隙。 然后我找到了一个 github 项目,它一次播放多个声音,而且加载速度很快,但我仍然对这个项目有疑问。它无法正确加载超过 30 个声音。在这个项目中,他们使用 ExoPlayer(下面是 link)。 https://github.com/zoenb/Soft-Sound
请帮我解决声音加载和循环间隙问题。
是的,您必须创建多个 (8) 个 exoplayer 实例,您不能只创建一个实例
这是我的代码,
private void initPlayer() {
if (player == null) {
trackSelectorParameters = new DefaultTrackSelector.ParametersBuilder().build();
TrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory();
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
trackSelector.setParameters(trackSelectorParameters);
RenderersFactory renderersFactory = ((MyAppApplication) context.getApplicationContext()).buildRenderersFactory();
player = ExoPlayerFactory.newSimpleInstance(context, renderersFactory, trackSelector);
player.setPlayWhenReady(true);
//to print log of ExoPlayer
//player.addAnalyticsListener(new EventLogger(trackSelector));
progressiveMediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory);
}
}
使用上面的代码初始化exoplayer,调用此方法将所有8个URI
同时播放
public void startPlaying(Uri uri) {
if (player == null) initPlayer();
MediaSource mediaSource = progressiveMediaSourceFactory.createMediaSource(uri);
player.prepare(mediaSource, true, false);
}
这里是 Github link - https://github.com/google/ExoPlayer
使用exoplayer-core
库
为此,我建议您为 ExoPlayer 创建一个单独的 class 来初始化、启动、停止等等...
public class MyAppExoPlayer {
private final Context context;
private DataSource.Factory dataSourceFactory;
private SimpleExoPlayer player;
private DefaultTrackSelector trackSelector;
private DefaultTrackSelector.Parameters trackSelectorParameters;
private ProgressiveMediaSource.Factory progressiveMediaSourceFactory;
private ConcatenatingMediaSource concatenatingMediaSource;
private int currentMediaPlayerIndex = 0;
public MyAppExoPlayer(Context context) {
this.context = context;
dataSourceFactory = ((MyAppApplication) context.getApplicationContext()).buildDataSourceFactory();
initPlayer();
}
private void initPlayer() {
if (player == null) {
trackSelectorParameters = new DefaultTrackSelector.ParametersBuilder().build();
TrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory();
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
trackSelector.setParameters(trackSelectorParameters);
RenderersFactory renderersFactory = ((MyAppApplication) context.getApplicationContext()).buildRenderersFactory();
player = ExoPlayerFactory.newSimpleInstance(context, renderersFactory, trackSelector);
player.setPlayWhenReady(true);
//to print log of ExoPlayer
//player.addAnalyticsListener(new EventLogger(trackSelector));
progressiveMediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory);
}
concatenatingMediaSource = new ConcatenatingMediaSource();
}
public void startPlaying(Uri uri) {
if (player == null) initPlayer();
MediaSource mediaSource = progressiveMediaSourceFactory.createMediaSource(uri);
player.prepare(mediaSource, true, false);
}
public void addToQ(Uri uri) {
if (player == null) return;
MediaSource mediaSource = progressiveMediaSourceFactory.createMediaSource(uri);
concatenatingMediaSource.addMediaSource(mediaSource);
}
public void stopPlaying() {
if (player != null) {
player.stop(true);
concatenatingMediaSource.clear();
}
}
public long getCurrentPosition() {
if (player != null)
return player.getCurrentPosition();
return 0;
}
public void releasePlayer() {
if (player != null) {
if (trackSelector != null) {
trackSelectorParameters = trackSelector.getParameters();
}
player.release();
player = null;
trackSelector = null;
}
}
}
现在创建 8 个实例 if MyAppExoPlayer
并调用 startPlaying()