控制 MediaPlayer 中的音量

发布于 2024-11-25 02:44:59 字数 1972 浏览 0 评论 0原文

我正在有声读物中播放音频(旁白)。音频文件采用 .ogg 格式,每个长度在 10 到 15 秒之间。

编辑添加:我正在使用 Android 2.2,API 8,并且我的清单中有这个:

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

我有 setVolumeControlStream(AudioManager.STREAM_MUSIC); 在我的 onCreate() 方法中。

我的声音是通过与此类似的代码播放的:

mp = MediaPlayer.create(mContext, resource);
mp.setOnCompletionListener(this);
mp.seekTo(0);
mp.setLooping(looping);
if(isSoundEnabled())
{
    mp.setVolume(1, 1);
}
else
{
    // I still need sounds to call their onComplete listeners to drive action
    mp.setVolume(0,0);
}
nowPlaying = true;
mp.start();

但是,尽管有更多保证(33 个并且还在增加!),但人们只需要 setVolumeControlStream(AudioManager.STREAM_MUSIC); 在onCreate() 时,当我按下设备 (Motorola Xoom) 上的音量键时,我的声音音量没有变化。

为了验证我的音量键正在执行某些操作,我覆盖了 onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean result = true;
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
    {
        DebugLog.d(TAG, "volume up");
    }
    else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
    {
        DebugLog.d(TAG, "volume down");
    }
}

并通过 LogCat 确认我按下了正确的音量键:

07-19 12:16:31.440: DEBUG/BookReader(17830): volume down; thread 1
07-19 12:16:31.710: DEBUG/BookReader(17830): volume down; thread 1
07-19 12:16:31.980: DEBUG/BookReader(17830): volume down; thread 1
07-19 12:16:32.440: DEBUG/BookReader(17830): volume up; thread 1
07-19 12:16:32.820: DEBUG/BookReader(17830): volume up; thread 1

尽管以下链接是关于 SoundPool,我应该计算流音量而不是使用mp.setVolume(1, 1); 在我上面的第一个代码示例中?

I'm playing audio (narration) in an audiobook. The audio files are in .ogg format, between 10 and 15 seconds long each.

Edit to add: I'm using Android 2.2, API 8, and I have this in my Manifest:

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

I've got setVolumeControlStream(AudioManager.STREAM_MUSIC); in my onCreate() method.

My sounds are being played via code similar to this:

mp = MediaPlayer.create(mContext, resource);
mp.setOnCompletionListener(this);
mp.seekTo(0);
mp.setLooping(looping);
if(isSoundEnabled())
{
    mp.setVolume(1, 1);
}
else
{
    // I still need sounds to call their onComplete listeners to drive action
    mp.setVolume(0,0);
}
nowPlaying = true;
mp.start();

But, despite more assurances (33 and counting!) that one simply needs setVolumeControlStream(AudioManager.STREAM_MUSIC); in onCreate(), my sounds don't change in volume when I press the volume keys on my device (Motorola Xoom).

To verify my volume keys were doing something, I overrode onKeyDown:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean result = true;
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
    {
        DebugLog.d(TAG, "volume up");
    }
    else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
    {
        DebugLog.d(TAG, "volume down");
    }
}

and confirmed via LogCat that I'm pressing the correct volume keys:

07-19 12:16:31.440: DEBUG/BookReader(17830): volume down; thread 1
07-19 12:16:31.710: DEBUG/BookReader(17830): volume down; thread 1
07-19 12:16:31.980: DEBUG/BookReader(17830): volume down; thread 1
07-19 12:16:32.440: DEBUG/BookReader(17830): volume up; thread 1
07-19 12:16:32.820: DEBUG/BookReader(17830): volume up; thread 1

Though the following link is about SoundPool, should I be calculating stream volume instead of using mp.setVolume(1, 1); in my first code sample above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆凄美了谁 2024-12-02 02:44:59

检查以确保每次调用 onKeyDown 时都不会返回 true。如果是这种情况,那么您将告诉 android 您将处理所有 onKeyDown 事件,因此 android 可能会忽略音量键。

Check to make sure you're not returning true every time onKeyDown is called. If that's the case then you're telling android that you'll handle all onKeyDown events, so android might be ignoring the volume key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文