android.media.AudioManager#getProperty ( )源码实例Demo

下面列出了android.media.AudioManager#getProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: walt   文件: AudioTest.java
AudioTest(Context context) {
    super(context);
    playbackRepetitions = getIntPreference(context, R.string.preference_audio_out_reps, 10);
    recordingRepetitions = getIntPreference(context, R.string.preference_audio_in_reps, 5);
    threshold = getIntPreference(context, R.string.preference_audio_in_threshold, 5000);

    //Check for optimal output sample rate and buffer size
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    String frameRateStr = am.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
    String framesPerBufferStr = am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
    logger.log("Optimal frame rate is: " + frameRateStr);
    logger.log("Optimal frames per buffer is: " + framesPerBufferStr);

    //Convert to ints
    frameRate = Integer.parseInt(frameRateStr);
    framesPerBuffer = Integer.parseInt(framesPerBufferStr);

    //Create the audio engine
    createEngine();
    createBufferQueueAudioPlayer(frameRate, framesPerBuffer);
    logger.log("Audio engine created");
}
 
源代码2 项目: droidkit-webrtc   文件: AudioManagerAndroid.java
@SuppressWarnings("unused")
private AudioManagerAndroid(Context context) {
  AudioManager audioManager = (AudioManager)
      context.getSystemService(Context.AUDIO_SERVICE);

  mNativeOutputSampleRate = DEFAULT_SAMPLING_RATE;
  mAudioLowLatencyOutputFrameSize = DEFAULT_FRAMES_PER_BUFFER;
  if (android.os.Build.VERSION.SDK_INT >=
      android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
    String sampleRateString = audioManager.getProperty(
        AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
    if (sampleRateString != null) {
      mNativeOutputSampleRate = Integer.parseInt(sampleRateString);
    }
    String framesPerBuffer = audioManager.getProperty(
        AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
    if (framesPerBuffer != null) {
        mAudioLowLatencyOutputFrameSize = Integer.parseInt(framesPerBuffer);
    }
  }
  mAudioLowLatencySupported = context.getPackageManager().hasSystemFeature(
      PackageManager.FEATURE_AUDIO_LOW_LATENCY);
}
 
源代码3 项目: webrtc-app-mono   文件: AudioManagerAndroid.java
@SuppressWarnings("unused")
private AudioManagerAndroid(Context context) {
  AudioManager audioManager = (AudioManager)
      context.getSystemService(Context.AUDIO_SERVICE);

  mNativeOutputSampleRate = DEFAULT_SAMPLING_RATE;
  mAudioLowLatencyOutputFrameSize = DEFAULT_FRAMES_PER_BUFFER;
  if (android.os.Build.VERSION.SDK_INT >=
      android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
    String sampleRateString = audioManager.getProperty(
        AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
    if (sampleRateString != null) {
      mNativeOutputSampleRate = Integer.parseInt(sampleRateString);
    }
    String framesPerBuffer = audioManager.getProperty(
        AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
    if (framesPerBuffer != null) {
        mAudioLowLatencyOutputFrameSize = Integer.parseInt(framesPerBuffer);
    }
  }
  mAudioLowLatencySupported = context.getPackageManager().hasSystemFeature(
      PackageManager.FEATURE_AUDIO_LOW_LATENCY);
}
 
源代码4 项目: webrtc_android   文件: WebRtcAudioManager.java
private static int getSampleRateForApiLevel(AudioManager audioManager) {
  if (Build.VERSION.SDK_INT < 17) {
    return DEFAULT_SAMPLE_RATE_HZ;
  }
  String sampleRateString = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
  return (sampleRateString == null) ? DEFAULT_SAMPLE_RATE_HZ : Integer.parseInt(sampleRateString);
}
 
源代码5 项目: webrtc_android   文件: WebRtcAudioManager.java
private static int getLowLatencyFramesPerBuffer(AudioManager audioManager) {
  if (Build.VERSION.SDK_INT < 17) {
    return DEFAULT_FRAME_PER_BUFFER;
  }
  String framesPerBuffer =
      audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
  return framesPerBuffer == null ? DEFAULT_FRAME_PER_BUFFER : Integer.parseInt(framesPerBuffer);
}
 
源代码6 项目: TelePlus-Android   文件: VoIPBaseService.java
@Override
public void onCreate() {
	super.onCreate();
	if (BuildVars.LOGS_ENABLED) {
		FileLog.d("=============== VoIPService STARTING ===============");
	}
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)!=null) {
		int outFramesPerBuffer = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
		VoIPController.setNativeBufferSize(outFramesPerBuffer);
	} else {
		VoIPController.setNativeBufferSize(AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT) / 2);
	}
	try {
		cpuWakelock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "telegram-voip");
		cpuWakelock.acquire();

		btAdapter=am.isBluetoothScoAvailableOffCall() ? BluetoothAdapter.getDefaultAdapter() : null;

		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		if(!USE_CONNECTION_SERVICE){
			filter.addAction(ACTION_HEADSET_PLUG);
			if(btAdapter!=null){
				filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
				filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
			}
			filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
		}
		registerReceiver(receiver, filter);

		soundPool = new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
		spConnectingId = soundPool.load(this, R.raw.voip_connecting, 1);
		spRingbackID = soundPool.load(this, R.raw.voip_ringback, 1);
		spFailedID = soundPool.load(this, R.raw.voip_failed, 1);
		spEndId = soundPool.load(this, R.raw.voip_end, 1);
		spBusyId = soundPool.load(this, R.raw.voip_busy, 1);

		am.registerMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));

		if(!USE_CONNECTION_SERVICE && btAdapter!=null && btAdapter.isEnabled()){
			int headsetState=btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
			updateBluetoothHeadsetState(headsetState==BluetoothProfile.STATE_CONNECTED);
			//if(headsetState==BluetoothProfile.STATE_CONNECTED)
			//	am.setBluetoothScoOn(true);
			for (StateListener l : stateListeners)
				l.onAudioSettingsChanged();
		}

	} catch (Exception x) {
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("error initializing voip controller", x);
		}
		callFailed();
	}
}
 
源代码7 项目: snapdroid   文件: SnapclientService.java
private void startProcess() throws IOException {
    Log.d(TAG, "startProcess");
    String player = "oboe";
    String configuredEngine = Settings.getInstance(getApplicationContext()).getAudioEngine();
    if (configuredEngine.equals("OpenSL"))
        player = "opensl";
    else if (configuredEngine.equals("Oboe"))
        player = "oboe";
    else {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            player = "opensl";
        else
            player = "oboe";
    }

    String rate = null;
    String fpb = null;
    String sampleformat = "*:16:*";
    AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) && Settings.getInstance(getApplicationContext()).doResample()) {
        rate = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
        fpb = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
        sampleformat = rate + ":16:*";
    }
    Log.i(TAG, "Configured engine: " + configuredEngine + ", active engine: " + player + ", sampleformat: " + sampleformat);

    ProcessBuilder pb = new ProcessBuilder()
            .command(this.getApplicationInfo().nativeLibraryDir + "/libsnapclient.so", "-h", host, "-p", Integer.toString(port), "--hostID", getUniqueId(this.getApplicationContext()), "--player", player, "--sampleformat", sampleformat, "--logfilter", "*:info,Stats:debug")
            .redirectErrorStream(true);
    Map<String, String> env = pb.environment();
    if (rate != null)
        env.put("SAMPLE_RATE", rate);
    if (fpb != null)
        env.put("FRAMES_PER_BUFFER", fpb);
    process = pb.start();

    Thread reader = new Thread(new Runnable() {
        @Override
        public void run() {
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            String line;
            try {
                while ((line = bufferedReader.readLine()) != null) {
                    log(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    logReceived = false;
    reader.start();
}
 
源代码8 项目: TelePlus-Android   文件: VoIPBaseService.java
@Override
public void onCreate() {
	super.onCreate();
	if (BuildVars.LOGS_ENABLED) {
		FileLog.d("=============== VoIPService STARTING ===============");
	}
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)!=null) {
		int outFramesPerBuffer = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
		VoIPController.setNativeBufferSize(outFramesPerBuffer);
	} else {
		VoIPController.setNativeBufferSize(AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT) / 2);
	}
	try {
		cpuWakelock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "telegram-voip");
		cpuWakelock.acquire();

		btAdapter=am.isBluetoothScoAvailableOffCall() ? BluetoothAdapter.getDefaultAdapter() : null;

		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		if(!USE_CONNECTION_SERVICE){
			filter.addAction(ACTION_HEADSET_PLUG);
			if(btAdapter!=null){
				filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
				filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
			}
			filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
		}
		registerReceiver(receiver, filter);

		soundPool = new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
		spConnectingId = soundPool.load(this, R.raw.voip_connecting, 1);
		spRingbackID = soundPool.load(this, R.raw.voip_ringback, 1);
		spFailedID = soundPool.load(this, R.raw.voip_failed, 1);
		spEndId = soundPool.load(this, R.raw.voip_end, 1);
		spBusyId = soundPool.load(this, R.raw.voip_busy, 1);

		am.registerMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));

		if(!USE_CONNECTION_SERVICE && btAdapter!=null && btAdapter.isEnabled()){
			int headsetState=btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
			updateBluetoothHeadsetState(headsetState==BluetoothProfile.STATE_CONNECTED);
			//if(headsetState==BluetoothProfile.STATE_CONNECTED)
			//	am.setBluetoothScoOn(true);
			for (StateListener l : stateListeners)
				l.onAudioSettingsChanged();
		}

	} catch (Exception x) {
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("error initializing voip controller", x);
		}
		callFailed();
	}
}
 
public void queryOptimalAudioSettings() {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    String text = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
    int framesPerBlock = Integer.parseInt(text);
    mSynthEngine.setFramesPerBlock(framesPerBlock);
}
 
源代码10 项目: Telegram-FOSS   文件: VoIPBaseService.java
@Override
public void onCreate() {
	super.onCreate();
	if (BuildVars.LOGS_ENABLED) {
		FileLog.d("=============== VoIPService STARTING ===============");
	}
	try {
		AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)!=null) {
			int outFramesPerBuffer = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
			TgVoip.setBufferSize(outFramesPerBuffer);
		} else {
			TgVoip.setBufferSize(AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT) / 2);
		}

		cpuWakelock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "telegram-voip");
		cpuWakelock.acquire();

		btAdapter=am.isBluetoothScoAvailableOffCall() ? BluetoothAdapter.getDefaultAdapter() : null;

		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		if(!USE_CONNECTION_SERVICE){
			filter.addAction(ACTION_HEADSET_PLUG);
			if(btAdapter!=null){
				filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
				filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
			}
			filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
		}
		registerReceiver(receiver, filter);

		soundPool = new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
		spConnectingId = soundPool.load(this, R.raw.voip_connecting, 1);
		spRingbackID = soundPool.load(this, R.raw.voip_ringback, 1);
		spFailedID = soundPool.load(this, R.raw.voip_failed, 1);
		spEndId = soundPool.load(this, R.raw.voip_end, 1);
		spBusyId = soundPool.load(this, R.raw.voip_busy, 1);

		am.registerMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));

		if(!USE_CONNECTION_SERVICE && btAdapter!=null && btAdapter.isEnabled()){
			int headsetState=btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
			updateBluetoothHeadsetState(headsetState==BluetoothProfile.STATE_CONNECTED);
			//if(headsetState==BluetoothProfile.STATE_CONNECTED)
			//	am.setBluetoothScoOn(true);
			for (StateListener l : stateListeners)
				l.onAudioSettingsChanged();
		}
	} catch (Exception x) {
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("error initializing voip controller", x);
		}
		callFailed();
	}
}
 
源代码11 项目: Telegram   文件: VoIPBaseService.java
@Override
public void onCreate() {
	super.onCreate();
	if (BuildVars.LOGS_ENABLED) {
		FileLog.d("=============== VoIPService STARTING ===============");
	}
	try {
		AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)!=null) {
			int outFramesPerBuffer = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
			TgVoip.setBufferSize(outFramesPerBuffer);
		} else {
			TgVoip.setBufferSize(AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT) / 2);
		}

		cpuWakelock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "telegram-voip");
		cpuWakelock.acquire();

		btAdapter=am.isBluetoothScoAvailableOffCall() ? BluetoothAdapter.getDefaultAdapter() : null;

		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		if(!USE_CONNECTION_SERVICE){
			filter.addAction(ACTION_HEADSET_PLUG);
			if(btAdapter!=null){
				filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
				filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
			}
			filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
		}
		registerReceiver(receiver, filter);

		soundPool = new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
		spConnectingId = soundPool.load(this, R.raw.voip_connecting, 1);
		spRingbackID = soundPool.load(this, R.raw.voip_ringback, 1);
		spFailedID = soundPool.load(this, R.raw.voip_failed, 1);
		spEndId = soundPool.load(this, R.raw.voip_end, 1);
		spBusyId = soundPool.load(this, R.raw.voip_busy, 1);

		am.registerMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));

		if(!USE_CONNECTION_SERVICE && btAdapter!=null && btAdapter.isEnabled()){
			int headsetState=btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
			updateBluetoothHeadsetState(headsetState==BluetoothProfile.STATE_CONNECTED);
			//if(headsetState==BluetoothProfile.STATE_CONNECTED)
			//	am.setBluetoothScoOn(true);
			for (StateListener l : stateListeners)
				l.onAudioSettingsChanged();
		}
	} catch (Exception x) {
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("error initializing voip controller", x);
		}
		callFailed();
	}
}