简体   繁体   中英

How to get String file name (my audio file) in raw folder android

I want to get the string file name of my audio file in raw folder. The reason is I want to use that filename into a cheapSoundFile class like this CheapSoundFile fileRecord = CheapSoundFile.create(pathFile, listener); the result is a number like the image below.

Here is my code:

public class EvalActivity extends AppCompatActivity {

    TextView title, lainlain, hammingWindow, frameGained;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eval);

        title = findViewById(R.id.textView);
        lainlain = findViewById(R.id.textView2);
        hammingWindow = findViewById(R.id.hammingWindow);
        frameGained = findViewById(R.id.frame_gained);

        Intent intent = getIntent();
        String pathFile = intent.getStringExtra("filepath");
        title.setText(pathFile);


        final CheapSoundFile.ProgressListener listener = new CheapSoundFile.ProgressListener() {

            @Override
            public boolean reportProgress(double fractionComplete) {
                return true;
            }
        };
        try {
            CheapSoundFile fileRecord = CheapSoundFile.create(pathFile, listener);
            int[] frameGainsRecord = fileRecord.getFrameGains();

            // TODO read audio file below here
            CheapSoundFile fileSource = CheapSoundFile.create(
                    Uri.parse("android.resource://com.latihcoding.iqro/" + R.raw.abaa).toString(),
                    listener
            );

            int numFrame = fileRecord.getNumFrames();
            int samplePerFrame = fileRecord.getSamplesPerFrame();
            int sumFrameGainsRecord = 0;
            int i;
            for (i = 0; i < frameGainsRecord.length; i++){
                sumFrameGainsRecord += frameGainsRecord[i];
            }

            int [] frameRecord = fileRecord.getFrameLens();

            Log.v("Evaluation","fileLoadDone");
            int freqs = fileRecord.getSampleRate();
            int sampleRateAbaa = fileSource.getSampleRate();
            String frequency =  "Sample rate = " + freqs + "\ngetFrameLens return array"
                    + myJoin(frameRecord, ",");
            frequency += " getFrameLens length = " + frameRecord.length + " numframe " + numFrame;
            frequency += " sample per frame " + samplePerFrame + " getFrameGains " + frameGainsRecord.length;
            frequency += " total frame gains record " + sumFrameGainsRecord + " dan nilai i = " + i;
            frequency += "\ngetFrameGains return array\n" + myJoin(frameGainsRecord, ",");
            frequency += " hasil hamming window dibawah ini menggunakan panjang dari numframe \n";
            lainlain.setText(frequency);

            String audioRawFolder = "" + sampleRateAbaa;
            hammingWindow.setText(audioRawFolder);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String myJoin(int[] arr, String separator) {
        if (null == arr || 0 == arr.length) return "";

        StringBuilder sb = new StringBuilder(256);
        sb.append(arr[0]);

        //if (arr.length == 1) return sb.toString();

        for (int i = 1; i < arr.length; i++) sb.append(separator).append(arr[i]);

        return sb.toString();
    }

    public static String myJoin(float[] arr, String separator) {
        if (null == arr || 0 == arr.length) return "";

        StringBuilder sb = new StringBuilder(256);

        sb.append(arr[0]);

        //if (arr.length == 1) return sb.toString();

        for (int i = 1; i < arr.length; i++) sb.append(separator).append(arr[i]);

        return sb.toString();
    }
}

在此处输入图片说明 在此处输入图片说明

The first parameter (for example: pathFile) used in the CheapSoundFile class is String fileName. The purpose of this code CheapSoundFile fileRecord = CheapSoundFile.create(pathFile, listener); is to get voice input from the user in the form of a recording and the result works like 2 images above. My question is: what if I have prepared the audio file in raw folder to be able to produce output like 2 images above? I've tried this link and I wrote the code again like this

CheapSoundFile fileSource = CheapSoundFile.create(
                    Uri.parse("android.resource://com.latihcoding.iqro/" + R.raw.abaa).toString(),
                    listener
            );

but that code produce error FileNotFoundExeption, see the image below: 在此处输入图片说明 . And i've also tried on this link and I change the code (inside try block) to be something like this

InputStream stream = getResources().openRawResource(R.raw.abaa);
// TODO read audio file from raw folder below here
            byte[] buffer = new byte[stream.available()];
            stream.read(buffer);
            stream.close();
            String fileSourcePath = new String(buffer, "UTF-8");
            CheapSoundFile fileSource = CheapSoundFile.create(fileSourcePath, listener);

And the result is:

 W/System.err: java.io.FileNotFoundException: RIFF�/��WAVEfmt �������������������������data�/�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
W/System.err: ��
W/System.err: ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������    ��  ��  ��  ��  ��  ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
W/System.err: ��
W/System.err: ���*�*�6�6�B�B�L�L�W�W�f�f�t�t�������������������������������������+�+�?�?�R�R�g�g�{�{�������������������������������������+��+��<��<��P��P��e��e��y��y��������������������������������������������''77EETTaapp������������((22;;DDLLRRYY__bbhhppvv}}��������������������������������������������������������������������������������������������������������{{wwqqjjffbb\\XXTTLLDD??<<995511--&&��������������������������������{{wwqqii^^PPAA44++%%  ����������������������������������������������������������������������������������������������{��{��p��p��d��d��\��\��U��U��Q��Q��N��N��G��G��?��?��9��9��2��2��*��*��%��%��������������������������������������������������������������������������������������z�z�k�k�[�[�K�K�9�9�(�(���������������������������
W/System.err:     at com.latihcoding.hendriktekayadiskoringkaraoke.soundfile.CheapSoundFile.create(CheapSoundFile.java:52)
W/System.err:     at com.latihcoding.hendriktekayadiskoringkaraoke.EvalActivity.onCreate(EvalActivity.java:51)
W/System.err:     at android.app.Activity.performCreate(Activity.java:5447)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:166)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:136)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5590)
W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096)
W/System.err:     at dalvik.system.NativeStart.main(Native Method)
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@420c0bc8 time:3328964
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@42040820 time:3328965

What is the proper solution of this problem? Thanks

One Solution would be to read the file from raw folder and create a temp file and after your process is completed you can then delete the file. look at the code below:

void createExternalStoragePrivateFile() {
    // Create a path where we will place our private file on external
    // storage.
    File file = new File(getExternalFilesDir(null), "audio.3gp");

    try {
        // Very simple code to copy a picture from the application's
        // resource into the external file.  Note that this code does
        // no error checking, and assumes the picture is small (does not
        // try to copy it in chunks).  Note that if external storage is
        // not currently mounted this will silently fail.
        InputStream is = getResources().openRawResource(R.drawable.abaa);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }
}

void deleteExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "audio.3gp");
    if (file != null) {
        file.delete();
    }
}

boolean hasExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "audio.3gp");
    if (file != null) {
        return file.exists();
    }
    return false;
}

The getResources().openRawResource(R.drawable.abaa); will read the abaa file form the raw folder and then with FileOutputStream you can create a new file and get the absolutepath from the file object. Make you you have the Read and write permissions. Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM