-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
longle_h
authored and
longle_h
committed
May 2, 2019
1 parent
85f6b87
commit fc24af6
Showing
15 changed files
with
284 additions
and
38 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
app/src/main/java/ca/ulaval/ima/mp/fragment/RecordFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.