10

そのため、2.3デバイスでは、デバイスの音量が0 / muteに設定されている場合でも、SoundPoolまたはMediaPlayerを使用して最大音量でサウンドを再生できます。サウンドを再生するときに、デバイスのレベルを手動で取得して設定する必要があることを理解していました。

これが私がその振る舞いを機能させたい方法です。

しかし、4.0デバイスで、サウンドがデバイスの設定レベルで自動的に再生されることに気付きました。これは望ましくありません。

これはOSのバージョン間の違いですか?もしそうなら、デバイスのボリュームを無視する方法はありますか?ミュートしても音を出して聞いてもらえますか?

この機能が必要な理由については説明できませんが、実際には必要です。

ありがとう!

4

2 に答える 2

19

目覚まし時計アプリケーションについても同様のニーズがありました。ボリュームに関するコメント付きの関連コードを次に示します。

これは、サウンド プロファイルがサイレントに設定されている場合、アラーム ストリームの音量が手動でゼロに設定されている場合、着信音の音量がゼロに設定されている場合に、HTC Rezound Android バージョン 4.0.3 で機能します。

    Context context;
    MediaPlayer mp;
    AudioManager mAudioManager;
    int userVolume;


    public AlarmController(Context c) { // constructor for my alarm controller class
        this.context = c;
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        //remeber what the user's volume was set to before we change it.
         userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);

        mp = new MediaPlayer();
    }
    public void playSound(String soundURI){

        Uri alarmSound = null;
        Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);


        try{
            alarmSound = Uri.parse(soundURI);
        }catch(Exception e){
            alarmSound = ringtoneUri;
        }
        finally{
            if(alarmSound == null){
                alarmSound = ringtoneUri;
            }
        }



        try {

            if(!mp.isPlaying()){
            mp.setDataSource(context, alarmSound);
            mp.setAudioStreamType(AudioManager.STREAM_ALARM);
            mp.setLooping(true);
            mp.prepare();
            mp.start();
            }


        } catch (IOException e) {
            Toast.makeText(context, "Your alarm sound was unavailable.", Toast.LENGTH_LONG).show();

        }
        // set the volume to what we want it to be.  In this case it's max volume for the alarm stream.
       mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_PLAY_SOUND);

    }

    public void stopSound(){
// reset the volume to what it was before we changed it.
        mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, userVolume, AudioManager.FLAG_PLAY_SOUND);
        mp.stop();
       mp.reset();

    }
    public void releasePlayer(){
        mp.release();
    }
于 2012-10-24T00:28:22.327 に答える