Skip to content

Commit

Permalink
Update sample app + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
adrielcafe committed Sep 16, 2016
1 parent b477b9c commit eba0328
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 48 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
> Convert audio files inside your Android app easily. This is a wrapper of [FFmpeg-Android-Java](https://github.com/WritingMinds/ffmpeg-android-java) lib.
Supported formats:
* WAV
* AAC
* MP3
* M4A
* WMA
* WAV
* FLAC

Lib size: ~9mb

## How To Use

1 - Add these permissions into your `AndroidManifest.xml` and [request for them in Android 6.0+](https://developer.android.com/training/permissions/requesting.html)
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ android {
}

dependencies {
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:appcompat-v7:24.2.1'
compile project(':lib')
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cafe.adriel.androidaudioconverter.sample;

import android.Manifest;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.File;

Expand All @@ -18,16 +20,16 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Giving time to lib load
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
convertAudio();
}
}, 2000);
if (getSupportActionBar() != null) {
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(getResources().getColor(R.color.colorPrimaryDark)));
}

Util.requestPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
Util.requestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}

private void convertAudio(){
public void convertAudio(View v){
/**
* Update with a valid audio file!
* Supported formats: {@link AndroidAudioConverter.AudioFormat}
Expand All @@ -36,13 +38,14 @@ private void convertAudio(){
IConvertCallback callback = new IConvertCallback() {
@Override
public void onSuccess(File convertedFile) {
Log.d("SUCCESS", convertedFile.getPath());
Toast.makeText(MainActivity.this, "SUCCESS: " + convertedFile.getPath(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Exception error) {
error.printStackTrace();
Toast.makeText(MainActivity.this, "ERROR: " + error.getMessage(), Toast.LENGTH_LONG).show();
}
};
Toast.makeText(this, "Converting audio file...", Toast.LENGTH_SHORT).show();
AndroidAudioConverter.with(this)
.setFile(wavFile)
.setFormat(AndroidAudioConverter.AudioFormat.MP3)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cafe.adriel.androidaudioconverter.sample;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

public class Util {

public static void requestPermission(Activity activity, String permission) {
if (ContextCompat.checkSelfPermission(activity, permission)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, 0);
}
}

}
20 changes: 14 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/colorPrimary"
tools:context="cafe.adriel.androidaudioconverter.sample.MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="20dp"
android:text="CONVERT AUDIO"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@android:color/white"
android:onClick="convertAudio"
style="@style/Widget.AppCompat.Button.Borderless"/>

</RelativeLayout>
6 changes: 0 additions & 6 deletions app/src/main/res/values-w820dp/dimens.xml

This file was deleted.

5 changes: 0 additions & 5 deletions app/src/main/res/values/dimens.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,52 @@
public class AndroidAudioConverter {

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

@Override
public String toString() {
return super.toString().toLowerCase();
return name().toLowerCase();
}
}

private static boolean loaded;

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

private static boolean load;

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

public static boolean isLoad(){
return load;
public static boolean isLoaded(){
return loaded;
}

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

}

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

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

@Override
Expand All @@ -70,7 +69,7 @@ public void onFinish() {
}
});
} catch (Exception e){
load = false;
loaded = false;
callback.onFailure(e);
}
}
Expand All @@ -95,7 +94,7 @@ public AndroidAudioConverter setCallback(IConvertCallback callback) {
}

public void convert() {
if(!isLoad()){
if(!isLoaded()){
callback.onFailure(new Exception("FFmpeg not loaded"));
return;
}
Expand All @@ -108,10 +107,9 @@ public void convert() {
return;
}
final File convertedFile = getConvertedFile(audioFile, format);
String[] cmd = new String[]{"-y", "-i", audioFile.getPath(), convertedFile.getPath()};
final String[] cmd = new String[]{"-y", "-i", audioFile.getPath(), convertedFile.getPath()};
try {
FFmpeg.getInstance(context)
.execute(cmd, new FFmpegExecuteResponseHandler() {
FFmpeg.getInstance(context).execute(cmd, new FFmpegExecuteResponseHandler() {
@Override
public void onStart() {

Expand Down Expand Up @@ -144,7 +142,7 @@ public void onFinish() {

private static File getConvertedFile(File originalFile, AudioFormat format){
String[] f = originalFile.getPath().split("\\.");
String filePath = originalFile.getPath().replace("." + f[f.length - 1], "." + format);
String filePath = originalFile.getPath().replace(f[f.length - 1], format.toString());
return new File(filePath);
}
}

0 comments on commit eba0328

Please sign in to comment.