我想要录制声音,然后才能听到重新录制的声音。我得到了文件路径,但MediaPlayer也没有玩过它。我已经看过一些MediaPlayer的例子,我认为它只会是这样的:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(path); // + File.separator + fileName
mp.prepare();
mp.start();
我的路径是:/ storage / emulated / 0 /录音/音乐录音/录音2017-02-06 00-17-12.amr
logcat的:
02-06 00:21:23.151 12308-12391 / com.example.studerande.upg32 D / OpenGLRenderer:0x7aea6a7400(RippleDrawable)上的endAllActiveAnimators,句柄为0x7aea5d6fc0 02-06 00:21:28.633 12308-12308 / com.example.studerande.upg32 E / recordapp:文件存在 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(许可被拒绝) 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:例外 java.io.FileNotFoundException:/ storage / emulated / 0 /录音/音乐录音/录音2017-02-06 00-21-23.amr(Permission denied) at java.io.FileInputStream.open(Native Method) 在java.io.FileInputStream。(FileInputStream.java:146) 在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1088) 在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1039) 在com.example.studerande.upg32.MainActivity.audioPlayer(MainActivity.java:105) 在com.example.studerande.upg32.MainActivity.onActivityResult(MainActivity.java:95) 在android.app.Activity.dispatchActivityResult(Activity.java:6917) 在android.app.ActivityThread.deliverResults(ActivityThread.java:4049) 在android.app.ActivityThread.handleSendResult(ActivityThread.java:4096) 在android.app.ActivityThread.-wrap20(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1516) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
我的主要活动的java代码:
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();
}
}
}
我拥有
的权限<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" />
在我的Android清单文件中。
答案 0 :(得分:1)
我想你在android API 23+上运行应用程序。因为清单中的所有权限都是dangerous权限,因此您需要在运行时请求它。 Tutorial
一些例子:
// 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.
}
}