Skip to content

Commit 2944601

Browse files
committed
android sound
1 parent 28d28e6 commit 2944601

9 files changed

+182
-8
lines changed

src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.github.androidify" >
4+
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
46

57
<application
68
android:allowBackup="true"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.github.androidify;
2+
3+
4+
import android.media.MediaPlayer;
5+
import android.os.Environment;
6+
import android.util.Log;
7+
8+
import java.io.IOException;
9+
10+
public class AndroidSoundPlayer {
11+
12+
private final String fileName;
13+
private MediaPlayer mediaPlayer = null;
14+
15+
public AndroidSoundPlayer(String fileName) {
16+
this.fileName = Environment.getExternalStorageDirectory().
17+
getAbsolutePath() + "/" + fileName;
18+
}
19+
20+
public void startPlaying() {
21+
if (mediaPlayer != null) stopPlaying();
22+
23+
mediaPlayer = new MediaPlayer();
24+
try {
25+
mediaPlayer.setDataSource(fileName);
26+
mediaPlayer.prepare();
27+
mediaPlayer.start();
28+
} catch (IOException e) {
29+
Log.e("SoundPlayer", "prepare() failed");
30+
}
31+
}
32+
33+
public void stopPlaying() {
34+
if (mediaPlayer == null) return;
35+
mediaPlayer.release();
36+
mediaPlayer = null;
37+
}
38+
39+
public boolean isPlaying() {
40+
return mediaPlayer != null && mediaPlayer.isPlaying();
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.github.androidify;
2+
3+
4+
import android.media.MediaRecorder;
5+
import android.os.Environment;
6+
import android.util.Log;
7+
8+
import java.io.IOException;
9+
10+
public class AndroidSoundRecorder {
11+
12+
private MediaRecorder mediaRecorder = null;
13+
private String mFileName;
14+
15+
public AndroidSoundRecorder(String mFileName) {
16+
this.mFileName = Environment.getExternalStorageDirectory().
17+
getAbsolutePath() + "/" + mFileName;
18+
}
19+
20+
public void startRecording() {
21+
if (mediaRecorder != null) stopRecording();
22+
23+
mediaRecorder = new MediaRecorder();
24+
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
25+
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
26+
mediaRecorder.setOutputFile(mFileName);
27+
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
28+
mediaRecorder.setAudioSamplingRate(44100);
29+
mediaRecorder.setAudioEncodingBitRate(16);
30+
31+
try {
32+
mediaRecorder.prepare();
33+
} catch (IOException e) {
34+
Log.e("HELLO", "prepare() failed");
35+
}
36+
37+
mediaRecorder.start();
38+
39+
}
40+
41+
public void stopRecording() {
42+
if (mediaRecorder == null) return;
43+
44+
mediaRecorder.stop();
45+
mediaRecorder.release();
46+
mediaRecorder = null;
47+
}
48+
49+
public boolean isRecording() {
50+
return mediaRecorder != null;
51+
}
52+
53+
}

src/main/java/com/github/androidify/PlaceholderFragment.java

+61-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99
import android.support.v4.app.FragmentManager;
1010
import android.support.v4.view.ViewPager;
1111
import android.view.LayoutInflater;
12+
import android.view.MotionEvent;
1213
import android.view.View;
1314
import android.view.ViewGroup;
15+
import android.widget.ImageButton;
1416

1517
public class PlaceholderFragment extends Fragment {
1618

19+
private final String mySound = "my_recorded_sound";
20+
private AndroidSoundRecorder soundRecorder;
21+
private AndroidSoundPlayer soundPlayer;
22+
1723
@Override
1824
public View onCreateView(LayoutInflater inflater, ViewGroup container,
1925
Bundle savedInstanceState) {
@@ -27,6 +33,55 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
2733
viewPagerBody.setAdapter(new AndroidifyViewPagerAdapter(fm, AndroidDrawables.getBodies()));
2834
viewPagerLegs.setAdapter(new AndroidifyViewPagerAdapter(fm, AndroidDrawables.getLegs()));
2935

36+
initShareButton(rootView, viewPagerHead, viewPagerBody, viewPagerLegs);
37+
initPlayButton(rootView);
38+
initRecordButton(rootView);
39+
40+
return rootView;
41+
}
42+
43+
private void initPlayButton(View rootView) {
44+
soundPlayer = new AndroidSoundPlayer(mySound);
45+
46+
View playButton = rootView.findViewById(R.id.button_play_sound);
47+
playButton.setOnClickListener(new View.OnClickListener() {
48+
@Override
49+
public void onClick(View view) {
50+
if (soundPlayer.isPlaying()) {
51+
soundPlayer.stopPlaying();
52+
} else {
53+
soundPlayer.startPlaying();
54+
}
55+
}
56+
});
57+
}
58+
59+
private void initRecordButton(View rootView) {
60+
soundRecorder = new AndroidSoundRecorder(mySound);
61+
62+
View recordButton = rootView.findViewById(R.id.button_record_sound);
63+
recordButton.setOnTouchListener(new View.OnTouchListener() {
64+
65+
@Override
66+
public boolean onTouch(View view, MotionEvent motionEvent) {
67+
ImageButton recordButton = (ImageButton) view;
68+
69+
if (MotionEvent.ACTION_UP == motionEvent.getAction()) {
70+
if (soundRecorder.isRecording()) {
71+
soundRecorder.stopRecording();
72+
recordButton.setImageDrawable(getResources().getDrawable(R.drawable.record_off));
73+
} else {
74+
soundRecorder.startRecording();
75+
recordButton.setImageDrawable(getResources().getDrawable(R.drawable.record_on));
76+
}
77+
}
78+
79+
return false;
80+
}
81+
});
82+
}
83+
84+
private void initShareButton(View rootView, final ViewPager viewPagerHead, final ViewPager viewPagerBody, final ViewPager viewPagerLegs) {
3085
View shareButton = rootView.findViewById(R.id.button_share);
3186
shareButton.setOnClickListener(new View.OnClickListener() {
3287
@Override
@@ -39,13 +94,16 @@ public void onClick(View view) {
3994

4095
String imagePath = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), bitmap, "Android Avatar", null);
4196
Uri imageURI = Uri.parse(imagePath);
42-
4397
startShareActivity(imageURI);
4498
}
4599
});
100+
}
46101

47-
48-
return rootView;
102+
@Override
103+
public void onPause() {
104+
super.onPause();
105+
soundRecorder.stopRecording();
106+
soundPlayer.stopPlaying();
49107
}
50108

51109
private void startShareActivity(Uri imageURI) {
9.18 KB
Loading

src/main/res/drawable/play.png

42.5 KB
Loading

src/main/res/drawable/record_off.png

25.7 KB
Loading

src/main/res/drawable/record_on.png

28.1 KB
Loading

src/main/res/layout/fragment_main.xml

+24-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<LinearLayout
6060
android:layout_width="match_parent"
6161
android:layout_height="0dp"
62-
android:weightSum="2"
62+
android:weightSum="3"
6363
android:layout_weight="20">
6464

6565
<LinearLayout
@@ -68,11 +68,14 @@
6868
android:gravity="center"
6969
android:layout_height="wrap_content">
7070

71-
<Button
71+
<ImageButton
7272
android:id="@+id/button_share"
7373
android:layout_width="wrap_content"
7474
android:layout_height="match_parent"
75-
android:text="Share" />
75+
android:src="@drawable/android_icon"
76+
android:background="@null"
77+
android:adjustViewBounds="true"
78+
android:scaleType="centerInside" />
7679
</LinearLayout>
7780

7881
<LinearLayout
@@ -85,10 +88,26 @@
8588
android:id="@+id/button_record_sound"
8689
android:layout_width="wrap_content"
8790
android:layout_height="match_parent"
88-
android:src="@drawable/record_sound_icon"
91+
android:src="@drawable/record_off"
92+
android:background="@null"
93+
android:adjustViewBounds="true"
94+
android:scaleType="centerInside" />
95+
</LinearLayout>
96+
97+
<LinearLayout
98+
android:layout_width="0dp"
99+
android:layout_weight="1"
100+
android:gravity="center"
101+
android:layout_height="wrap_content">
102+
103+
<ImageButton
104+
android:id="@+id/button_play_sound"
105+
android:layout_width="wrap_content"
106+
android:layout_height="match_parent"
107+
android:src="@drawable/play"
89108
android:background="@null"
90109
android:adjustViewBounds="true"
91-
android:scaleType="fitStart" />
110+
android:scaleType="centerInside" />
92111
</LinearLayout>
93112
</LinearLayout>
94113

0 commit comments

Comments
 (0)