Reputation: 354
I want to record sound and then have the abillity to hear the recoded sound. I got the filepath but the MediaPlayer won't play it either. I've seen some examples of MediaPlayer and I thought it would only be like this:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(path); // + File.separator + fileName
mp.prepare();
mp.start();
My path is: /storage/emulated/0/recordings/music recordings/Audio recording 2017-02-06 00-17-12.amr
Logcat:
02-06 00:21:23.151 12308-12391/com.example.studerande.upg32 D/OpenGLRenderer: endAllActiveAnimators on 0x7aea6a7400 (RippleDrawable) with handle 0x7aea5d6fc0 02-06 00:21:28.633 12308-12308/com.example.studerande.upg32 E/recordapp: File exist 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: java.io.FileNotFoundException: /storage/emulated/0/recordings/music recordings/Audio recording 2017-02-06 00-21-23.amr (Permission denied) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at java.io.FileInputStream.open(Native Method) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at java.io.FileInputStream.(FileInputStream.java:146) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1088) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1039) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at com.example.studerande.upg32.MainActivity.audioPlayer(MainActivity.java:105) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at com.example.studerande.upg32.MainActivity.onActivityResult(MainActivity.java:95) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:6917) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:4049) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at android.app.ActivityThread.-wrap20(ActivityThread.java) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at android.os.Looper.loop(Looper.java:154) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077) 02-06 00:21:28.636 12308-12308/com.example.studerande.upg32 W/System.err: at java.lang.reflect.Method.invoke(Native Method) 02-06 00:21:28.637 12308-12308/com.example.studerande.upg32 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 02-06 00:21:28.637 12308-12308/com.example.studerande.upg32 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 02-06 00:21:28.637 12308-12308/com.example.studerande.upg32 E/MYAPP: exception java.io.FileNotFoundException: /storage/emulated/0/recordings/music recordings/Audio recording 2017-02-06 00-21-23.amr (Permission denied) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:146) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1088) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1039) at com.example.studerande.upg32.MainActivity.audioPlayer(MainActivity.java:105) at com.example.studerande.upg32.MainActivity.onActivityResult(MainActivity.java:95) at android.app.Activity.dispatchActivityResult(Activity.java:6917) at android.app.ActivityThread.deliverResults(ActivityThread.java:4049) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096) at android.app.ActivityThread.-wrap20(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
My java code of the mainactivity:
package com.example.studerande.upg32;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import static android.app.Activity.RESULT_OK;
public class MainActivity extends AppCompatActivity {
private static final int RECORDING_ACTIVE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button recordbutton = (Button) findViewById(R.id.record_button);
recordbutton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Button record_button = (Button) findViewById(R.id.record_button);
record_button.setText("starting recording..");
startRecord();
}
});
}
public File getTempFile(Context context, String url) {
File file = null;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
} catch (IOException e) {
// Error while creating file
}
return file;
}
public void startRecord()
{
// NOTE THAT THE USER MUST HAVE A RECORDING SOFTWARE. NOT ALL DEVICES HAVE THIS.
Intent recordingApplicationIntent = new Intent();
recordingApplicationIntent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(recordingApplicationIntent, RECORDING_ACTIVE);
}
private String getAudioFilePathFromUri(Uri uri) {
Cursor cursor = getContentResolver()
.query(uri, null, null, null, null);
cursor.moveToFirst();
int index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
return cursor.getString(index);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == RECORDING_ACTIVE && resultCode == RESULT_OK)
{
Context context;
Toast.makeText(this, "Sound recoding intent success", Toast.LENGTH_SHORT).show();
String thefilename = "";
Bundle extras = data.getExtras();
Uri uri = data.getData();
String filePath = getAudioFilePathFromUri(uri);
thefilename = filePath;
Button record_button = (Button) findViewById(R.id.record_button);
record_button.setText(thefilename);
File sourceFile = new File(filePath);
if (!sourceFile.isFile()) {
Log.e("recordapp", "file does not exist");
// return 0;
}
else
{
Log.e("recordapp", "File exist");
audioPlayer(filePath);
}
}
}
public void audioPlayer(String path){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path); // + File.separator + fileName
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
Log.e("MYAPP", "exception", e);
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
I have the permissions of
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
in my android manifest file.
Upvotes: 0
Views: 1810
Reputation: 1474
I suppose you run app on android API 23+. Because all permissions from manifest are dangerous permissions so you need to request it in runtime. Tutorial.
Some example:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
// PERMISSION_REQUEST_CODE is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
Upvotes: 1