Read audio file from device memory instead of Assets

1.9k views Asked by At

I am working with superpowered sdk for audio processing that excepts a file from assets using the following code.

AssetFileDescriptor fd0 = getResources().openRawResourceFd(R.raw.recorded_audio);
int fileAoffset = (int) fd0.getStartOffset(), fileAlength = (int) fd0.getLength();

The Above values passed to a function, as

SuperpoweredExample(Integer.parseInt(samplerateString), Integer.parseInt(buffersizeString), getPackageResourcePath(), fileAoffset, fileAlength);

This works fine and if i place my audio file in raw folder that works best, but my audio file generated at run time and read that we can't write files in assets. Is there any way i can use file from external memory location?

4

There are 4 answers

1
Apoorv Mehrotra On

Try this code,it is using MediaPlayer class.

package com.example.audiomediaplayer1;  

import android.media.MediaPlayer;  
import android.net.Uri;  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
import android.widget.MediaController;  
import android.widget.VideoView;  

public class MainActivity extends Activity {  

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

        MediaPlayer mp=new MediaPlayer();  
        try{  
            mp.setDataSource("/sdcard/Music/maine.mp3");//Write your location here  
            mp.prepare();  
            mp.start();  

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

    }  

    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_main, menu);  
        return true;  
    }  

}

or this one short and simple from external storage along with path.

String filePath = Environment.getExternalStorageDirectory()+"/yourfolderNAme/yopurfile.mp3";
    mediaPlayer = new  MediaPlayer();
    mediaPlayer.setDataSource(filePath);
    mediaPlayer.prepare();   
    mediaPlayer.start();

hope it works for you.

6
Gabor Szanto On

Just pass your file path as a string to the Superpowered open() method. No need for file offset and file length this time, as your file is a "full" file this time, not some part of a file. Perhaps you don't need Java for this at all.

1
DSP_engineer On

1)in AndroidManifest.xml:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

between package and application tags

2)create copy of Open File function in your .cpp file, without offset and length parameters:

extern "C" JNIEXPORT void
Java_com_superpowered_playerexample_MainActivity_OpenFile2 (
        JNIEnv *env,
        jobject __unused obj,
        jstring path      // path to APK file
        // length of audio file
) {
    const char *str = env->GetStringUTFChars(path, 0);
    player->open(str, 0, 0);
    env->ReleaseStringUTFChars(path, str);
}

3) in your MainActivity.class:

String file = "/mnt/shared/Other/more.mp3";
OpenFile2(file); 

that works for me, hope this could help

1
Andrews Alvis On
void SuperpoweredExample::setPlayerA(const char *path) {

playerA->open(path,0,0);

}

Just send File path as a string.

Example "/sdcard/sample.mp3"