java - 带有 AudioEffect 的 Android MediaPlayer : Getting Error (-22, 0)

标签 java android audio android-mediaplayer audioeffect

好吧,这就是我的要求..

我有一个 WAV 文件,我想打开它,添加一些效果,然后播放。

我正在使用 MediaPlayer播放文件,PresetReverb添加一些效果。

这是我的代码

public void playSound(){
   String fullPath = MainActivity.this.filePath + "tmpaudio.wav";

   final MediaPlayer player = new MediaPlayer();
   player.setDataSource(this, Uri.parse(fullPath));

   PresetReverb pReverb = new PresetReverb(0,player.getAudioSessionId());
   pReverb.setPreset(PresetReverb.PRESET_LARGEROOM);
   pReverb.setEnabled(true);
   player.attachAuxEffect(eReverb.getId());
   player.setAuxEffectSendLevel(1.0f);

   //prepare for playback
   player.prepare();

   // Media prepared listener
   player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
      public void onPrepared(MediaPlayer mp) {
         //play
         player.start();
      } 
   });
}

当我运行这段代码时,我在 logcat 中得到一个日志(不是我记录的。)

05-02 12:02:42.356: E/MediaPlayer(17250): Error (-22,0)

但是当我评论这些行时

PresetReverb pReverb = new PresetReverb(0,player.getAudioSessionId());
pReverb.setPreset(PresetReverb.PRESET_LARGEROOM);
pReverb.setEnabled(true);
player.attachAuxEffect(eReverb.getId());
player.setAuxEffectSendLevel(1.0f);

MediaPlayer 正在成功播放文件。所以我的 WAV 文件没有问题。

有点失望,我尝试了EnvironmentalReverb , 而不是 PresetReverb,

EnvironmentalReverb eReverb = new EnvironmentalReverb(1, player.getAudioSessionId());
eReverb.setDecayHFRatio((short) 1000);
eReverb.setDecayTime(10000);
eReverb.setDensity((short) 1000);
eReverb.setDiffusion((short) 1000);
eReverb.setReverbLevel((short) -1000);
eReverb.setEnabled(true);
player.attachAuxEffect(eReverb.getId());
player.setAuxEffectSendLevel(1.0f);

我也有同样的错误(错误(-22,0))。

所以要么我遗漏了一些如此明显的东西,要么 AudioEffect 有问题家庭类(在文档或 api 本身)。任何人都可以解释一下吗?

编辑:我忘了添加,当我调试代码时记录错误

 player.start();

被执行。在发布上面的代码段之前,我已经删除了异常处理部分。但我很肯定,我执行时没有捕获到异常。

再次编辑:

来自 this链接 我开始明白错误 -22 是 PVMFErrLicenseRequiredPreviewAvailable

/*
 Error due to the lack of a valid license for the content.  However
 a preview is available.
 */
const PVMFStatus PVMFErrLicenseRequiredPreviewAvailable = (-22);

我用 PVMFErrLicenseRequiredPreviewAvailable 进行了谷歌搜索,我得到了 this文档。在第 87 页

14.10.5 Preview of DRM Content without a Valid License Available

A variation on the scenario covered in Section 14.10.3 is the case where there is no valid license for full playback of a piece of content, but there it can be previewed. This scenario might be a common way of initially distributing content so that consumers can preview it before deciding to purchase a full license. In this case the Init() method will return with the code PVMFErrLicenseRequiredPreviewAvailable, which indicates that a license is required for full playback but a preview is available. In order to play the preview, the application must remove the current source then add it back with a flag set on the local data source to indicate preview mode.

现在我播放的 WAV 文件是我使用 Android SDK 本身中的 SpeechToText 工具生成的。我不知道是什么许可证阻止我播放此文件。

最佳答案

好吧,我终于让它工作了。我重新阅读了文档,尝试了代码,突然间一切正常了。这些是我的发现,以防将来对任何人有所帮助。

在预设混响的情况下

在 presetReverb 文档中

The PresetReverb is an output mix auxiliary effect and should be created on Audio session 0. In order for a MediaPlayer or AudioTrack to be fed into this effect, they must be explicitely attached to it and a send level must be specified. Use the effect ID returned by getId() method to designate this particular effect when attaching it to the MediaPlayer or AudioTrack.

说的是

PresetReverb pReverb = new PresetReverb(0,player.getAudioSessionId()); <== error

是不允许的。您只能使用全局 Audio Session 0 来构建 PresetReverb。

PresetReverb pReverb  = new PresetReverb(1,0); <== correct

现在我们可以使用 MediaPlayer 或 AudioTrack 将它附加到

player.attachAuxEffect(pReverb.getId());
player.setAuxEffectSendLevel(1.0f);

我的完整 PresetReverb 代码是

PresetReverb pReverb    = new PresetReverb(1,0);
player.attachAuxEffect(pReverb.getId());
pReverb.setPreset(PresetReverb.PRESET_LARGEROOM);
pReverb.setEnabled(true);
player.setAuxEffectSendLevel(1.0f);

注意:如果您正在寻找好的混响效果或回声,最好使用 EnvironmentalReverb。我对 PresetReverb 的性能有些失望。

在环境混响的情况下

来自 EnvironmentalReverb 的文档

The EnvironmentalReverb is an output mix auxiliary effect and should be created on Audio session 0. In order for a MediaPlayer or AudioTrack to be fed into this effect, they must be explicitely attached to it and a send level must be specified. Use the effect ID returned by getId() method to designate this particular effect when attaching it to the MediaPlayer or AudioTrack.

完全像 PresetReverb,但是当我写的时候

Log.e("DEBUG","sessionId : " + player.getAudioSessionId()); <== printed "454"
EnvironmentalReverb  eReverb 
             = new EnvironmentalReverb(0,player.getAudioSessionId()); //should be error as per documentation
//player.attachAuxEffect(eReverb.getId());   <== Yes this one was commented

没有错误,我得到了很好的混响效果和回声。所以这似乎是文档中的错误。此外,当我们将播放器的 session ID 传递给构造函数(player.getAudioSessionId())时,似乎没有必要将播放器附加到 EnvironmentalReverb 实例。奇怪..

为了完整起见,这个也像文档所说的那样工作。

EnvironmentalReverb  eReverb 
             = new EnvironmentalReverb(0,0); 
player.attachAuxEffect(eReverb.getId());     <== No,not comment this one

对于其他 AudioEffect 子项(均衡器、BassBoost、虚拟器)

这些不是我问题的一部分。但是对于将来看到这个的每个人..

NOTE: attaching insert effects (equalizer, bass boost, virtualizer) to the global audio output mix by use of session 0 is deprecated.

参见 documentation

关于错误 (-22,0)

嗯,公平地说,这不是一条信息性错误消息。我不知道为什么在我们搞砸 Audio Session 时会出现无效许可错误。无论如何,当我更正 Audio Session 参数时,这个错误就消失了。这就是我所知道的。

关于java - 带有 AudioEffect 的 Android MediaPlayer : Getting Error (-22, 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10409122/

相关文章:

android - Android 4.0 或更高版本的应用程序崩溃

delphi - 捕获 CTRL + S 时如何摆脱 Windows 声音?

java - 我的实体 OOP 模型正确吗?

Java Array 2D 无法使用参数

c# - 将RSA加密Java代码移植到C#

java - 通知总是创建新的 Activity 而不是恢复以前的 Activity

java - MATLAB Java 类

android - 有没有办法让我在 React Native 中创建一个文件夹并将 JSON 文件放入 Android 和 iOS 的移动本地存储中?

matlab - 选择非默认方向的视频,音频错误

iphone - spotify 播放器在 iphone 中播放旧轨道一段时间