MediaPlayer的音量问题,语音串流的问题 [英] MediaPlayer Volume issue-AudioStream issue

查看:133
本文介绍了MediaPlayer的音量问题,语音串流的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提供一个自定义的蜂鸣声,当我在我的应用程序得到一个消息。这提示音应该尊重主人的电话通知音量电平(不振铃音量)。这意味着,如果电话通知体积= 3/10,则蜂鸣强度应该是3/10。
我不能做到这一点,

I am trying to provide a custom beep sound when I get a message in my Application. This beep sound should respect the master phone notification volume level(not ringer volume). Which means if phone notification vol =3/10 , then beep intensity should be 3/10. I am not able to achieve this,

  AudioManager audioMan = (AudioManager) context
            .getSystemService(Context.AUDIO_SERVICE);
    int volume;

    if (mPlayer == null) {
        mPlayer = MediaPlayer.create(context, R.raw.mytone);
    }

    if (mPlayer.isPlaying()) {
        mPlayer.stop();
        mPlayer.release();
        mPlayer = MediaPlayer.create(context, R.raw.mytone);

    }

    volume = audioMan.getStreamVolume(AudioManager.STREAM_NOTIFICATION);

    mPlayer.setVolume(volume, volume);//this doesn't work for me, beep sound is taking media player volume by default.

    mPlayer.setOnErrorListener(new OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer player, int what, int extra) {
            player.stop();
            player.reset();
            return true;
        }
    });

    if (mVibrator == null)
        mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    mVibrator.cancel();

能否请你分享你的知识,给我方向。谢谢。

Can you please share your knowledge and give me directions. Thank you.

推荐答案

看起来你是在玩你的声音在音乐流由参考要去 AudioManager.STREAM_MUSIC 。修改音量修改的一切在那个流播放的水平。这就是为什么音乐/媒体播放搞砸了。

It looks like you are playing your sound over the music stream going by the reference to AudioManager.STREAM_MUSIC. Modifying the volume level modifies the level for everything played on that stream. This is why music/media playback is 'mucked up'.

如果你想使用的铃声流(和它的音量设置),那么你应该使用 AudioManager.STREAM_RING 来代替。你说你已经尝试过这一点,但code片段你给刚刚调整音量 - 你还没有向你展示了如何创建和配置的MediaPlayer 之前问它来播放您的声音。

If you want to use the ringer stream (and its volume setting) then you should be using AudioManager.STREAM_RING instead. You say you've tried this but the code snippet you've given just adjusts the volume - you've not shown how you create and configure your MediaPlayer before you ask it to play your sound.

您已经有了,当你设置你的的MediaPlayer 实例来选择合适的流。正如我已经成功地在那种你所描述的场景中使用不同的数据流,这就是你的问题所在。要选择在其上您的自定义提示音播放音频流,使用 setAudioStream 在这样的的MediaPlayer 实例:

You've got to select the appropriate stream when you set up your MediaPlayer instance. As I've successfully used different streams in the kind of scenario you are describing, this is where your problem lies. To select the audio stream over which your custom beep is played, use setAudioStream on your MediaPlayer instance like this:

// Get a reference to the MP3 file to play
AssetFileDescriptor afd = getContext().getResources().openRawResourceFd(R.raw.my_mp3);

// Create the media player
MediaPlayer mediaPlayer = new MediaPlayer();

// Give it the MP3 you want played
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

// Set the audio stream to play over
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);

// Now play the sound
mediaPlayer.prepare();
mediaPlayer.start();

它很好的做法,提供你的用户选择流为自己的选择 - 除了音乐和铃声的溪流有报警和通知流,每一个独立的音量电平(还有其他太多,但这些都是芯的)。看看上AudioManager 的Andr​​oid文档这里

这篇关于MediaPlayer的音量问题,语音串流的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆