gpt4 book ai didi

android - 在 Marshmallow 中使用 AudioTrack 播放音频流

转载 作者:行者123 更新时间:2023-11-29 17:19:53 25 4
gpt4 key购买 nike

当我尝试在 android Lollipop 版本中执行这部分代码时。一切都好。但在 Marshmallow 版本中不起作用:

public static void play() {

InputStream generetedStream = ...;

int minBufferSize = AudioTrack.getMinBufferSize(16000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioTrack trackw = new AudioTrack(AudioManager.STREAM_MUSIC, 16000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);


int bufferSize = 1024;
byte[] lin = new byte[bufferSize];
trackw.play();

int i = 0;
if (generetedStream != null) {
while (generetedStream != null && (i = generetedStream.read(lin, 0, bufferSize)) > -1) {
trackw.write(lin, 0, lin.length);
}
}

}

添加的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Marshmallow 版本的 AudioTrack 有变化吗???

最佳答案

尝试为版本 6.0 设置运行时权限。

if(Build.VERSION.SDK_INT>=23){
int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_PHONE_STATE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
} else {
// your code
}
}else{
// your code
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted -code

} else {

// permission not granted - code
}
}
}
}

您可以根据需要添加其他权限...

关于android - 在 Marshmallow 中使用 AudioTrack 播放音频流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37050607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com