Android recorded aac(.mp3) file not playing in iOS - android

Hi I'm using the following code to record audio on Android
myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
myRecorder.setAudioSamplingRate(44100);
myRecorder.setAudioEncodingBitRate(256);
myRecorder.setAudioChannels(1);
voiceFileName = getFilename();
myRecorder.setOutputFile(voiceFileName);
private String getFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, "test");
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
}
but that aac(.mp3) file does not play on iOS.
EDIT
let audioFilename = getDocumentsDirectory().stringByAppendingPathComponent("whistle.mp3")
let audioURL = NSURL(fileURLWithPath: audioFilename)
let whistlePlayer:AVAudioplayer = try AVAudioPlayer(contentsOfURL: audioURL)
whistlePlayer.play()
How do I play this file in IOS(Swift)?

i got solution just changed extension to .m4a it work on ios
let audioFilename = getDocumentsDirectory().stringByAppendingPathComponent("voice.m4a")
let audioURL = NSURL(fileURLWithPath: audioFilename)
do
{
audioPlayer = try AVAudioPlayer(contentsOfURL: audioURL)
audioPlayer.delegate = self
audioPlayer.play()
}
catch{
print("filenotfound")
}

myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); suggests you should have been writing to mp4 (m4a). Not sure aac can be written to mp3 container

check if you can play that file in safari of iPhone.
and can you tell how are you trying to play that file?

Related

checking a file if it exist or not exist (not working)

I am trying to check the mp3 file located to my raw folder under my res. my mp3 file is exist but my code gives me a false return.
String filename = "android.resource://" + this.getPackageName() + "/raw/w";
Toast.makeText(ViewPager.this, mp3check(filename)+"",Toast.LENGTH_LONG).show();
checking the mp3 file
public boolean mp3check(String _filename) {
return new File(_filename).exists();
}
I don't think you can treat a raw resource as a regular file.
Since it is a resource, there is no need to check if it exists.
You can open it using the resource ID.
For example, if your file is named, mysong.mp3, you can open it like this:
InputStream is = getResources().openRawResource(R.raw.mysong);
or
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.mysong);
You can use the AssetFileDescriptor to play it with a MediaPlayer.
try this
String filename = "android.resource://" + this.getPackageName() + "/raw/w";
String newFilePath = "new file path"; // ExternalStorageDirectory path
final Path destination = Paths.get(newFilePath );
try (
final InputStream in = getResources().openRawResource(R.raw.w);
) {
Files.copy(in, destination);
}
Toast.makeText(ViewPager.this, mp3check(filename)+"",Toast.LENGTH_LONG).show();
public boolean mp3check(String _newFilePath) {
return new File(_newFilePath).exists();
}

How to get the full path Mp3 songs from internal storage?

I want to implement a simple Media Player.How can i retrieve the full path of Mp3 songs from internal storage in android.
mpintro = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/intro.mp3"));
mpintro.setLooping(true);
mpintro.start();
If you have any problem search first. do your efforts you'll find it...
here's a link try it Get Mp3 from SD card
Try this check for file if it exists or not
public static String rootPath = Environment.getExternalStorageDirectory() + "/YourDirectory/";
File file = new File(Environment.getExternalStorageDirectory()
+ "/YourDirectoryFolder/Audio/" + NmaeofFile + "/");
if (!file.exists())
return false;
else{
//Do your work here
}
And then simply start media player
player.setDataSource(rootPath);//rootpath is the path of your file
player.prepare();
player.start();

Android audio capture

I'm following this article to capture Audio on Android. I thought it will be easy, but there's a problem. Here's the code:
File dir = activity.getDir("recordings", Context.MODE_PRIVATE);
File file = new File(dir, "testaudio.mpeg4");
FileUtil fileUtil = new FileUtil();
File parent = file.getParentFile();
if(!parent.exists()){
Log.i(TAG, "Creating non-existent path:" + parent.getAbsolutePath());
parent.mkdirs();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
try{
recorder.setOutputFile(file.getAbsolutePath());
recorder.prepare();
recorder.start();
}catch(IOException e){
//more code
}
So, it does create a testaudio.mpeg4 file under /data/data/com.myapp/app_recordings/ folder. But, after transferring the file to Mac (using adb pull) it doesn't play. I am on a Mac and I've tried a few audio formats (e.g. MP3, MPEG, 3GP etc.) but nothing seems to be working. Any help/guidance will be appreciated.
Use this
File dir = new File(Environment
.getExternalStorageDirectory().getAbsolutePath() + "/recording/");
It seems like a Mac issue, nothing wrong with the code or device. I can hear the Audio on the device.

Reading mp3 from expansion file

Reading mp3 file from expansion file
I have create and expansion file with name
"main.1.com.example.app.obb"
which contains and audio file name "about_eng.mp3"
Now the issue i have written the following code to read and play the mp3 file
private final static String EXP_PATH = "/Android/obb/";
static String[] getAPKExpansionFiles(Context ctx, int mainVersion, int patchVersion) {
String packageName = ctx.getPackageName();
Vector<String> ret = new Vector<String>();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// Build the full path to the app's expansion files
File root = Environment.getExternalStorageDirectory();
File expPath = new File(root.toString() + EXP_PATH + packageName);
// Check that expansion file path exists
if (expPath.exists()) {
if ( mainVersion > 0 ) {
String strMainPath = expPath + File.separator + "main." +
mainVersion + "." + packageName + ".obb";
File main = new File(strMainPath);
if ( main.isFile() ) {
ret.add(strMainPath);
}
}
if ( patchVersion > 0 ) {
String strPatchPath = expPath + File.separator + "patch." +
mainVersion + "." + packageName + ".obb";
File main = new File(strPatchPath);
if ( main.isFile() ) {
ret.add(strPatchPath);
}
}
}
}
String[] retArray = new String[ret.size()];
ret.toArray(retArray);
return retArray;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Get a ZipResourceFile representing a merger of both the main and patch files
try {
ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(this,1,1);
if(expansionFile!=null){
AssetFileDescriptor fd = expansionFile.getAssetFileDescriptor("about_eng.mp3");
//or
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource( fd.getFileDescriptor(),
fd.getStartOffset(),fd.getLength());
mediaPlayer.prepare();
mediaPlayer.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get an input stream for a known file inside the expansion file ZIPs
}
But it is always throwing exception at this line
mediaPlayer.setDataSource( fd.getFileDescriptor(),
fd.getStartOffset(),fd.getLength());
mediaPlayer.prepare();
because the variable fd is null.
Can any body help me to solve this
i have googled and found that we shold have to make .zip with 0% (No compression) that is mention in http://developer.android.com/google/play/expansion-files.html
Tip: If you're packaging media files into a ZIP, you can use media playback calls on the files with offset and length controls (such as MediaPlayer.setDataSource() and SoundPool.load()) without the need to unpack your ZIP. In order for this to work, you must not perform additional compression on the media files when creating the ZIP packages. For example, when using the zip tool, you should use the -n option to specify the file suffixes that should not be compressed:
zip -n .mp4;.ogg main_expansion media_files
so How to make 0% compression zip using winrar?
here see the compression method
so we should have to upload this zip in play store.
so you not need to use ZipHelper.java that is mentioned in other SO answer
just simply use
ZipResourceFile expansionFile=null;
try {
expansionFile = APKExpansionSupport.getAPKExpansionZipFile(getApplicationContext(),3,0);
AssetFileDescriptor fd = expansionFile.getAssetFileDescriptor("test.mp3");
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(fd.getFileDescriptor(),fd.getStartOffset(),fd.getLength());
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try to use expansionFile.getAssetFileDescriptor("main_expansion/about_eng.mp3").
If it doesn't work, call this:
ZipResourceFile.ZipEntryRO [] entries = expansionFile.getAllEntries();
and look at the file names:
entries[0].mFileName;.
That could be a hint how the path should look like.
I know that the question is old, but maybe it helps someone.
To read mp3, with mediaplayer, you can do it 3 ways
for example. my application package name is
com.bluewindsolution.android.sampleapp
then my expansion file should be
main.1.com.bluewindsolution.android.sampleapp.obb
in detail, you can followed in here
[main|patch].< expansion-version >.< package-name >.obb
From expansion you can get it via 3 ways:
via AssetFileDescriptor
// this one work with image file, media file
// Get a ZipResourceFile representing a specific expansion file
// mainContext, version no. of your main expansion, version no of your patch
ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(this, 1, 0);
AssetFileDescriptor afd_img1 = expansionFile.getAssetFileDescriptor("your_path/your_file.jpg");
AssetFileDescriptor afd_mpbg = expansionFile.getAssetFileDescriptor("your_path/your_file.mp3");
// to set image
ImageView imageView1 = (ImageView)findViewById(R.id.iv1);
imageView1.setImageBitmap(BitmapFactory.decodeFileDescriptor(afd_iv1.getFileDescriptor()));
// to set sound
try {
mpbg.setDataSource(afd_mpbg.getFileDescriptor(), afd_mpbg.getStartOffset(), afd_mpbg.getDeclaredLength());
mpbg.prepareAsync();
mpbg.start();
} catch (IllegalStateException e) {
Log.w("Error=====", "Failed to prepare media player", e);
}
via InputStream
// this one work with image file, media file
// Get a ZipResourceFile representing a specific expansion file
ZipResourceFile expansionFile = new ZipResourceFile(filePathToMyZip);
// Get an input stream for a known file inside the expansion file ZIPs
InputStream fileStream = expansionFile.getInputStream(pathToFileInsideZip);
via Uri
//this one can use in almost thing such as jpg, mp3, mp4, pdf, etc.
//you need to create new class to override APEZProvider
public class ZipFileContentProvider extends APEZProvider {
#Override
public String getAuthority() {
return "com.bluewindsolution.android.sampleapp";
}
}
//you need to add <provider> in AndroidManifest.xml
<provider
android:name="com.bluewindsolution.android.sampleapp.ZipFileContentProvider"
android:authorities="com.bluewindsolution.android.sampleapp"
android:exported="false"
android:multiprocess="true"
>
<meta-data android:name="mainVersion"
android:value="1"></meta-data>
</provider>
//after that you can use it any time by this code:
String filename2 = "image/i_commu_a_1_1_1_1.jpg"; // suppose you store put your file in directory in .obb
String uriString2 = "content://com.bluewindsolution.android.sampleapp" + File.separator + filename2;
Uri uri2 = Uri.parse(uriString2);
imageView2.setImageURI(uri2);
// Filename inside my expansion file
String filename = "video/v_do_1_1_1.mp4"; // suppose you store put your file in directory in .obb
String uriString = "content://com.bw.sample_extension_download_main" + File.separator + filename;
Uri uri = Uri.parse(uriString);
v1 = (VideoView)findViewById(R.id.videoView1);
v1.setVideoURI(uri);
v1.start();

How to give specific location to voice recorder

Thanks for previous replies
I am doing application with android inbuilt voice recorder. i want to store the voice in specific location. but whenever i use the android in built voice recorder(using intent action) it save all voice into default folder. is there anyway to customize the location to save the voice. If anyone have idea pls guide me..
from com.android.soundrecorder.Recorder.java,we could find:
public void startRecording(int outputfileformat, String extension) {
if (mSampleFile == null) {
File sampleDir = Environment.getExternalStorageDirectory();
if (!sampleDir.canWrite()) // Workaround for broken sdcard support on the device.
sampleDir = new File("/sdcard/sdcard");
try {
mSampleFile = File.createTempFile(SAMPLE_PREFIX, extension, sampleDir);
} catch (IOException e) {
setError(SDCARD_ACCESS_ERROR);
return;
}
....
}
}
mSampleFile is created in code,
So...we can't customize the location to save the voice.
Try this code:
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "DemoApplication"+File.separator+"Media"+File.separator+"audio"+File.separator);
if(root.exists())
root.delete();
root.mkdirs();
File voiceDirectory = new File(root, String.format("AudioFile_%d.amr", System.currentTimeMillis()));
outputFileUri = Uri.fromFile(voiceDirectory);
intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

Categories

Resources