Skip to content

Format extension #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ android {
compileSdkVersion 29
buildToolsVersion '29.0.2'


repositories {
maven {
url "https://jitpack.io"
}
}

defaultConfig {
applicationId 'by.naxa.soundrecorder'
minSdkVersion 16
Expand Down Expand Up @@ -54,4 +61,6 @@ dependencies {
// Firebase
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
compile 'com.github.adrielcafe:AndroidAudioConverter:0.0.8'

}
1 change: 1 addition & 0 deletions app/lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
24 changes: 24 additions & 0 deletions app/lib/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 24
buildToolsVersion "24.0.1"

defaultConfig {
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile 'com.github.adrielcafe:ffmpeg-android-java:2a627f6ecd@aar'
}
17 changes: 17 additions & 0 deletions app/lib/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/adrielcafe/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
7 changes: 7 additions & 0 deletions app/lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="cafe.adriel.androidaudioconverter">

<application android:label="@string/app_name"/>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package cafe.adriel.androidaudioconverter;

import android.content.Context;

import com.github.hiteshsondhi88.libffmpeg.FFmpeg;
import com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.FFmpegLoadBinaryResponseHandler;

import java.io.File;
import java.io.IOException;

import cafe.adriel.androidaudioconverter.callback.IConvertCallback;
import cafe.adriel.androidaudioconverter.callback.ILoadCallback;
import cafe.adriel.androidaudioconverter.model.AudioFormat;

public class AndroidAudioConverter {

private static boolean loaded;

private Context context;
private File audioFile;
private AudioFormat format;
private IConvertCallback callback;

private AndroidAudioConverter(Context context){
this.context = context;
}

public static boolean isLoaded(){
return loaded;
}

public static void load(Context context, final ILoadCallback callback){
try {
FFmpeg.getInstance(context).loadBinary(new FFmpegLoadBinaryResponseHandler() {
@Override
public void onStart() {

}

@Override
public void onSuccess() {
loaded = true;
callback.onSuccess();
}

@Override
public void onFailure() {
loaded = false;
callback.onFailure(new Exception("Failed to loaded FFmpeg lib"));
}

@Override
public void onFinish() {

}
});
} catch (Exception e){
loaded = false;
callback.onFailure(e);
}
}

public static AndroidAudioConverter with(Context context) {
return new AndroidAudioConverter(context);
}

public AndroidAudioConverter setFile(File originalFile) {
this.audioFile = originalFile;
return this;
}

public AndroidAudioConverter setFormat(AudioFormat format) {
this.format = format;
return this;
}

public AndroidAudioConverter setCallback(IConvertCallback callback) {
this.callback = callback;
return this;
}

public void convert() {
if(!isLoaded()){
callback.onFailure(new Exception("FFmpeg not loaded"));
return;
}
if(audioFile == null || !audioFile.exists()){
callback.onFailure(new IOException("File not exists"));
return;
}
if(!audioFile.canRead()){
callback.onFailure(new IOException("Can't read the file. Missing permission?"));
return;
}
final File convertedFile = getConvertedFile(audioFile, format);
final String[] cmd = new String[]{"-y", "-i", audioFile.getPath(), convertedFile.getPath()};
try {
FFmpeg.getInstance(context).execute(cmd, new FFmpegExecuteResponseHandler() {
@Override
public void onStart() {

}

@Override
public void onProgress(String message) {

}

@Override
public void onSuccess(String message) {
callback.onSuccess(convertedFile);
}

@Override
public void onFailure(String message) {
callback.onFailure(new IOException(message));
}

@Override
public void onFinish() {

}
});
} catch (Exception e){
callback.onFailure(e);
}
}

private static File getConvertedFile(File originalFile, AudioFormat format){
String[] f = originalFile.getPath().split("\\.");
String filePath = originalFile.getPath().replace(f[f.length - 1], format.getFormat());
return new File(filePath);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cafe.adriel.androidaudioconverter.callback;

import java.io.File;

public interface IConvertCallback {

void onSuccess(File convertedFile);

void onFailure(Exception error);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cafe.adriel.androidaudioconverter.callback;

public interface ILoadCallback {

void onSuccess();

void onFailure(Exception error);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cafe.adriel.androidaudioconverter.model;

public enum AudioFormat {
AAC,
MP3,
M4A,
WMA,
WAV,
FLAC;

public String getFormat() {
return name().toLowerCase();
}
}
3 changes: 3 additions & 0 deletions app/lib/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">AndroidAudioConverter</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import by.naxa.soundrecorder.fragments.FileViewerFragment;
import by.naxa.soundrecorder.fragments.RecordFragment;
import by.naxa.soundrecorder.util.EventBroadcaster;
import cafe.adriel.androidaudioconverter.AndroidAudioConverter;
import cafe.adriel.androidaudioconverter.callback.ILoadCallback;

public class MainActivity extends AppCompatActivity {

Expand All @@ -45,6 +47,20 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


AndroidAudioConverter.load(this, new ILoadCallback() {
@Override
public void onSuccess() {
// Great!
}
@Override
public void onFailure(Exception error) {
// FFmpeg is not supported by device
}
});


if (SoundRecorderApplication.getInstance().isNightModeEnabled()) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); //For night mode theme
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.w3c.dom.Text;

import by.naxa.soundrecorder.BuildConfig;
import by.naxa.soundrecorder.DBHelper;
import by.naxa.soundrecorder.R;
import by.naxa.soundrecorder.RecordingItem;
import by.naxa.soundrecorder.fragments.PlaybackFragment;
import by.naxa.soundrecorder.fragments.SettingsFragment;
import by.naxa.soundrecorder.listeners.OnDatabaseChangedListener;
import by.naxa.soundrecorder.listeners.OnSingleClickListener;
import by.naxa.soundrecorder.util.EventBroadcaster;
Expand Down Expand Up @@ -238,6 +242,7 @@ public void removeOutOfApp(String filePath) {
* rename a file
*/
public void rename(int position, String name) {

final String mFilePath = Paths.combine(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC),
Paths.SOUND_RECORDER_FOLDER, name);
Expand Down Expand Up @@ -275,11 +280,17 @@ private void shareFileDialog(int position) {
}

private void renameFileDialog(final int position) {

// File rename dialog
AlertDialog.Builder renameFileBuilder = new AlertDialog.Builder(mContext);

LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.dialog_rename_file, null);
TextView text = (TextView)view.findViewById(R.id.textView);
text.setText(SettingsFragment.getFormat());

// TextView textView = (TextView) view.findViewById(R.id.textView);
// textView.setText(SettingsFragment.getFormat());

final TextInputEditText input = view.findViewById(R.id.new_name);

Expand All @@ -292,7 +303,8 @@ public void onClick(DialogInterface dialog, int id) {
final Editable editable = input.getText();
if (editable == null)
return;
final String value = editable.toString().trim() + ".mp4";

final String value = editable.toString().trim() + SettingsFragment.getFormat();
rename(position, value);
} catch (Exception e) {
if (Fabric.isInitialized()) Crashlytics.logException(e);
Expand Down
Loading