当前位置: 首页 > 知识库问答 >
问题:

在android中播放或暂停多个音频文件

戚建德
2023-03-14

共有1个答案

冯宪
2023-03-14

是的,如果您想要播放/暂停这个过程,Soundpool可能会有所帮助。

首先创建一个声音管理器,它保留在活动实例中,并创建一个映射来将音频文件链接到ID。

// Replace 10 with the maximum of sounds that you would like play at the same time.
SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 100); 
HashMap<String, Integer> stringToSoundID = new HashMap<String, Integer>();
HashMap<Integer, Integer> soundIdToStreamID = new HashMap<Integer, Integer>();
Integer streamIDbeingPlayed= -1;

然后将所有声音加载到soundPool中,保持文件和声音ID之间的链接。

for(String filePath: Files) {
  int soundID = soundPool.load(filePath, 1);
  stringToSoundID.put(filePath, soundID );
}
void playFile(String filePath) {
  if(streamIDbeingPlayed!= -1) {
    soundPool.pause(streamIDbeingPlayed);
  }
  Integer soundID = stringToSoundID.get(filePath);
  Integer streamID = soundIdToStreamID.get(soundID);
  if(streamID == null) {
    streamIDbeingPlayed = soundPool.play (soundID, 1, 1, 1, -1, 1);
    soundIdToStreamID.put(soundID, streamIDbeingPlayed);
  } else {
    soundPool.resume(streamID);
    streamIDbeingPlayed = streamID;
  }
}
 类似资料:
  • 函数名称:暂停音频播放 函数功能: 暂停音频播放 函数方法 media.pause() 函数用例 media.playAudio("/mnt/sdcard/LuaBox/Projects/1/1.mp3",5,true) --把音乐播放到 1 分钟的位置 media.jump(60*1000) mSleep(2000) -- 暂停音频播放 media.pause() 注意事项 目前积木编程函数和

  • 我正在开发一个音频播放器,它可以在后台播放音频文件。我的问题是,当录像机或视频播放器启动时,我需要暂停音频播放器。 有什么方法可以处理这个任务吗?例如,我有来处理这些调用。当我接到呼叫或wnat呼叫时,我们可以使用呼叫状态暂停播放器。我想为录像机或视频播放器以及相同的场景。当视频/录制开始时,我需要暂停音频播放器。

  • ap.pauseBackgroundAudio(CALLBACK) 暂停播放音乐。 代码示例 <script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script> <style>.output{ display:block; max-width: 100%

  • 我的应用程序中有一项服务。它发送和接收webservice请求和响应。当我收到来自web服务的特定响应时,我需要停止播放音频/视频文件(媒体文件正在从另一个应用程序播放)。当我完成某个过程时,我需要再次恢复。 注: 在andoid设备中,如果任何2G呼叫停止播放媒体文件,则音乐播放器播放任何音乐,然后当2G呼叫断开时,停止播放媒体文件。我只需要这样做。

  • 在webview iframe和webview上播放的视频设置在回收卡视图上。许多视频都在他们的recyclerview上。 我想在用户开始滚动回收器时暂停播放视频,并在用户播放另一个视频时暂停播放视频。 查看代码 我想用这个: 请帮我解决这个问题。任何帮助都将不胜感激。谢谢!

  • 对于自定义videoview,我必须重写MediaController(更改按钮的设计并连接到按钮,暂停/播放选择器)。情况是这样的:将视频暂停并打开应用程序,然后再次恢复,视频再次开始播放,而不是从暂停的点开始,因此问题出现了:如何使视频从停止的地方开始播放?我需要在视频活动中更改什么?