Skip to content

Commit

Permalink
audio record
Browse files Browse the repository at this point in the history
  • Loading branch information
longle_h authored and longle_h committed May 2, 2019
1 parent 85f6b87 commit fc24af6
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 38 deletions.
29 changes: 0 additions & 29 deletions .idea/codeStyles/Project.xml

This file was deleted.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion app/src/main/java/ca/ulaval/ima/mp/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import ca.ulaval.ima.mp.fragment.FileConverterFragment;
import ca.ulaval.ima.mp.fragment.MemberFragment;
import ca.ulaval.ima.mp.fragment.MessageFragment;
import ca.ulaval.ima.mp.fragment.RecordFragment;
import ca.ulaval.ima.mp.fragment.SearchFragment;
import ca.ulaval.ima.mp.fragment.SoundFragment;
import ca.ulaval.ima.mp.gateway.Gateway;
Expand All @@ -36,14 +37,16 @@ public class MainActivity extends AppCompatActivity
ChannelFragment.OnChannelFragmentInteractionListener,
FileConverterFragment.OnFileConverterFragmentInteractionListener,
SoundFragment.OnSoundFragmentInteractionListener,
SearchFragment.Listener {
SearchFragment.Listener,
RecordFragment.Listener {

public static boolean debug = true;

public SoundFragment soundFragment = null;
public MessageFragment messageFragment = null;
public FileConverterFragment convertFragment = null;
public SearchFragment searchFragment = null;
public RecordFragment recordFragment = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -228,6 +231,16 @@ public void onSearchFile() {
}
}

@Override
public void onRecord() {
if (RecordFragment.requirePermissions(this)) {
if (recordFragment == null) {
recordFragment = RecordFragment.newInstance(this);
}
this.setFragment(recordFragment, R.id.frameContainer);
}
}

@Override
public void onVoiceDisconnect(boolean fromDestroy) {
Gateway.voice.stopPlaying();
Expand Down Expand Up @@ -278,4 +291,15 @@ public void onSearch(String search) {
startActivityForResult(intent, FileManager.CODE.PLAY_FILE);
}
}

@Override
public void onRecordEnd(File output) {
if (output != null) {
if (convertFragment != null) {
getSupportFragmentManager().beginTransaction().remove(convertFragment).commit();
}
convertFragment = FileConverterFragment.newInstance(output.getAbsolutePath());
setMainFragment(convertFragment);
}
}
}
160 changes: 160 additions & 0 deletions app/src/main/java/ca/ulaval/ima/mp/fragment/RecordFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package ca.ulaval.ima.mp.fragment;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import ca.ulaval.ima.mp.R;
import ca.ulaval.ima.mp.activity.FileManager;
import ca.ulaval.ima.mp.sdk.SDK;

public class RecordFragment extends Fragment {

private boolean isRecording = false;
private MediaRecorder mediaRecorder = null;
private RecordFragment.Listener mListener = null;
private File file = null;


public RecordFragment() {
// Required empty public constructor
}

public static RecordFragment newInstance(RecordFragment.Listener l) {
RecordFragment f = new RecordFragment();
f.setListener(l);
return f;
}

public void setListener(RecordFragment.Listener mListener) {
this.mListener = mListener;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_record, container, false);
}

@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {

final Button b = view.findViewById(R.id.action_record_button);
final EditText filename = view.findViewById(R.id.edit_record_name);
b.setText(getString(R.string.start_record));
b.setBackgroundResource(R.color.info);
mediaRecorder = new MediaRecorder();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) SDK.mainContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

if (filename.getText().toString().equals("")) {
new AlertDialog.Builder(view.getContext())
.setTitle("Le nom de ne peux pas être vide")
.setMessage("Le nom de fichier doit être spécifié, merci de préciser le nom")
.setNeutralButton(R.string.details, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
} else if (!isRecording) {
// Record
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
file = new File(FileManager.importedDir.getAbsolutePath() + "/" + filename.getText().toString() + ".mp3");
try {
FileOutputStream ff = new FileOutputStream(file);
mediaRecorder.setOutputFile(ff.getFD());
isRecording = true;
mediaRecorder.prepare();
b.setText(getString(R.string.stop_record));
b.setBackgroundResource(R.color.danger);
mediaRecorder.start();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
isRecording = false;
mediaRecorder.stop();
mediaRecorder.reset();
b.setText(getString(R.string.start_record));
b.setBackgroundResource(R.color.info);
if (mListener != null) {
mListener.onRecordEnd(file);
}
}
}
});

super.onViewCreated(view, savedInstanceState);
}


public static boolean requirePermissions(AppCompatActivity activity) {
if (Build.VERSION.SDK_INT >= 23) {
if (activity.checkSelfPermission(Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.RECORD_AUDIO}, 1);
return false;
}
}
return true;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

}

@Override
public void onPause() {
isRecording = false;
super.onPause();
}

@Override
public void onDetach() {
isRecording = false;
super.onDetach();
}

public interface Listener {
void onRecordEnd(File output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ public void onClick(View v) {
onDisconnect(false);
}
});
ImageButton recordButton = rootView.findViewById(R.id.action_record);
recordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onRecord();
}
});
}

public void setFileInfo(File file) {
Expand Down Expand Up @@ -197,6 +204,7 @@ public interface OnSoundFragmentInteractionListener {
void onPlayFile();
void onImportFile();
void onSearchFile();
void onRecord();
void onVoiceDisconnect(boolean fromDestroy);
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable-anydpi/ic_mic.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#FFFFFF">
<path
android:fillColor="#FF000000"
android:pathData="M12,14c1.66,0 2.99,-1.34 2.99,-3L15,5c0,-1.66 -1.34,-3 -3,-3S9,3.34 9,5v6c0,1.66 1.34,3 3,3zM17.3,11c0,3 -2.54,5.1 -5.3,5.1S6.7,14 6.7,11L5,11c0,3.41 2.72,6.23 6,6.72L11,21h2v-3.28c3.28,-0.48 6,-3.3 6,-6.72h-1.7z"/>
</vector>
Binary file added app/src/main/res/drawable-hdpi/ic_mic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_mic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_mic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_mic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions app/src/main/res/layout/fragment_record.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragment.RecordFragment">

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/edit_record_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="@string/placeholder_filename"
android:textColorHint="@color/colorAccent"
android:inputType="textPersonName"
android:textColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.202" />

<Button
android:id="@+id/action_record_button"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="@string/start_record"
android:background="@color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_record_name"
app:layout_constraintVertical_bias="0.166" />

</android.support.constraint.ConstraintLayout>

</FrameLayout>
Loading

0 comments on commit fc24af6

Please # to comment.