Coder Social home page Coder Social logo

audiocapturesample's People

Contributors

julioz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

audiocapturesample's Issues

No audio recorded, only zeros in file

When I run this code in the emulator when playing music with another app, nothing is recorded. I tried several music apps.
All permissions are given.
I do get the popup message about the recording and I see the icon on top about the recoding.
The recoding file is saved, but when I open it, it only contains zeros.
I tried it on android 10 and 11.
Android 10: Automotive emulator and Polestar 2
Android 11: Automotive emulator (both with headset on and off)

AudioRecord not initializing on android 11 when app is in background

I have been implementing this functionality to record audio playbacks played on the device.

It is working fine when my app is in foreground, and I am getting pretty fine audio recordings, but it is throwing the below error whenever my app goes to background.

I have implemented this as a service and starting this service from another service which is a foreground service. I have tried all of the configuration for audioFormat but audioRecord object is failing to initialize.

Below is the function where app is crashed at run time.

private final void startAudioCapture() {

AudioPlaybackCaptureConfiguration config = new AudioPlaybackCaptureConfiguration.Builder(mediaProjection)
        .addMatchingUsage(AudioAttributes.USAGE_MEDIA)
        .build();

AudioFormat audioFormat = new AudioFormat.Builder()
        .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
        .setSampleRate(8000)
        .setChannelMask(AudioFormat.CHANNEL_IN_MONO)
        .build();

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
    return;
}
int bufferSize = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
audioRecord = new AudioRecord.Builder()
        .setAudioFormat(audioFormat)
        .setBufferSizeInBytes(bufferSize)
        .setAudioPlaybackCaptureConfig(config)
        .build();
audioRecord.startRecording();
audioCaptureThread = new Thread() {
    @Override
    public void run() {
        File outputFile = createAudioFile();
        writeAudioToFile(outputFile);
    }
};

audioCaptureThread.start();
mIsRecording = true;

}
Errors:- E/IAudioFlinger: createRecord returned error -1 E/AudioRecord: createRecord_l(1): AudioFlinger could not create record track, status: -1 E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.Myapp.app.internal, PID: 18240 java.lang.RuntimeException: Unable to start service com.Myapp.app.AudioService@bfz0813 with Intent { act=AudioService:Start cmp=com.Myapp.app.internal/com.Myapp.app.AudioService }: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.

Note:- This works perfect for all android version less than 11.

How to get permission in java ?

I am a student.

I want to rewrite this code in java.

for Microphone audio recording I get permission like the following.

public class Microphone extends AppCompatActivity {


public static boolean permissionToRecordAccepted = false;
private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
private String[] permissions = {Manifest.permission.RECORD_AUDIO};


@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_microphone);


    if (this.checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(permissions, REQUEST_RECORD_AUDIO_PERMISSION);
    } else {
        permissionToRecordAccepted = true;
    }

    if (permissionToRecordAccepted) {
        System.out.println("permission granted.");
    }


}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case REQUEST_RECORD_AUDIO_PERMISSION:
            permissionToRecordAccepted = (grantResults[0] == PackageManager.PERMISSION_GRANTED);
    }
}

}

I want to rewrite this code in java. But , I am confused .

I didn't know how to get permission for internal audio captureing in java.

Can you help me to get permission for internal audio ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.