2

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
arslan haktic
  • 4,348
  • 4
  • 22
  • 35
  • Did you solve this? I'm wondering because I would like to load a wav from ram as my .wav files are stored in assets, and it seems Superpowered can only load .wav files from filepaths? – Viktor Sehr Nov 03 '17 at 14:08

4 Answers4

3

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.

Gabor Szanto
  • 1,329
  • 8
  • 12
  • Thanks a lot for your kind response. Actually i am doing this in Android project. Thats why asking here. If you can share any example code that will be highly apreciateable. Thanks – arslan haktic Jan 07 '17 at 15:28
  • I really appreciate your help and support. I tried this method, by accessin that path like: Environment.getExternalStorageDirectory().getPath() +"/recorded_audio.wav" Also converting this to file and then getting file absolute path, cronical path and relative path but unfortunately nothin works and app is getting crash continously. Can you please help. Also i need to write file after applying effects in external storage you valueable information will be highly apreciateable. – arslan haktic Jan 09 '17 at 14:27
  • An absolute path should work. If you provide wrong path, then it should still not crash the app, you'd receive an open error in that case. Don't forget to not pass any other variable to the open method in this "direct loading" case. So it will look like player->open("path_to_file"); – Gabor Szanto Jan 09 '17 at 17:32
  • It opens for some audio files but not for all. using AssetFileDescriptor all file plays perfectly Please help answer my question here: https://stackoverflow.com/questions/51125017/superpoweredadvancedaudioplayer-cannot-open-some-files-via-file-path Thank You – Kathan Shah Jul 01 '18 at 18:27
  • I put like player->open("/storage/emulated/0/rec.wav"); but it does not work... please help how we can open file from internal storage memory? @Gabor Szanto – nidhi Jul 05 '18 at 07:00
  • /storage/emulated is never a real, absolute filesystem path, those look different don't forget the required manifest permissions too – Gabor Szanto Jul 07 '18 at 09:18
1

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.

Apoorv Mehrotra
  • 607
  • 5
  • 13
  • Thanks for your suggestion but this is not relevent to my problem, as i said i am using NDK and super powered sdk, above code pass three attribute to that c++ code and thats working but it reads from assets. So i need to get offset and length of file from external storage. – arslan haktic Jan 02 '17 at 08:04
0
void SuperpoweredExample::setPlayerA(const char *path) {

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

}

Just send File path as a string.

Example "/sdcard/sample.mp3"

0

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