简体   繁体   English

如何在原始文件夹android中获取字符串文件名(我的音频文件)

[英]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); 原因是我想使用该文件名到这样的cheapSoundFile类中: 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. CheapSoundFile类中使用的第一个参数(例如:pathFile)是String fileName。 The purpose of this code CheapSoundFile fileRecord = CheapSoundFile.create(pathFile, listener); 这段代码的目的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. 是从用户那里获得录音形式的语音输入,其结果就像上面的2张图片一样。 My question is: what if I have prepared the audio file in raw folder to be able to produce output like 2 images above? 我的问题是:如果我准备了原始文件夹中的音频文件以能够产生类似上面2张图像的输出,该怎么办? 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: 但是该代码会产生错误FileNotFoundExeption,请参见下图: 在此处输入图片说明 . And i've also tried on this link and I change the code (inside try block) to be something like this 而且我也尝试过此链接 ,并将代码(在try块内)更改为如下所示

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); 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. 将从原始文件夹读取abaa文件,然后使用FileOutputStream创建一个新文件并从该文件对象获取绝对路径。 Make you you have the Read and write permissions. 使您拥有读写权限。 Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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