在过去的 8 小时里,我一直在做的只是想出一个简单的录音机/播放应用程序。我希望能够以 3GP 录制音频,然后将其自动加载到音频中MediaPlayer
以便我可以播放。我 80% 确定它正在录制并保存到代码中指定的位置,但是当我尝试将其加载到我的媒体播放器中时,我总是会收到此错误。我也尝试加载toto_africa.mp3
文件以确保它不是录制文件的问题,它仍然给我这个错误。任何有关此错误的帮助将不胜感激,因为我到处寻找答案。我想一个更具体的问题是:我如何正确地让我的MediaPlayer
沟通MediaRecorder
和运作正常?
日志猫
03-14 00:32:18.708: E/appPath:(4443): /data/data/com.jordan.tester/files
03-14 00:32:18.708: E/(1) Length of de file mon'?(4443): 0
03-14 00:32:24.408: E/Are ya dead maan?(4443): Kiss the Lucky egg
03-14 00:32:24.688: E/IS da file alive?(4443): true
03-14 00:32:24.688: E/IS da file a file?(4443): true
03-14 00:32:24.688: E/(2) Length of de file mon'?(4443): 4515
03-14 00:32:28.362: V/MediaPlayer(4443): constructor
03-14 00:32:28.362: V/MediaPlayer(4443): setListener
03-14 00:32:28.368: V/MediaPlayer(4443):setDataSource(/data/data/com.jordan.tester/files/temp.3gp)
03-14 00:32:28.368: V/MediaPlayer(4443): prepare
03-14 00:32:28.368: V/MediaPlayer(4443): message received msg=100, ext1=1, ext2=-2147483648
03-14 00:32:28.368: E/MediaPlayer(4443): error (1, -2147483648)
03-14 00:32:28.368: V/MediaPlayer(4443): signal application thread
03-14 00:32:28.368: V/MediaPlayer(4443): prepare complete - status=1
03-14 00:32:28.378: D/MediaPlayer(4443): create failed:
03-14 00:32:28.378: D/MediaPlayer(4443): java.io.IOException: Prepare failed.: status=0x1
03-14 00:32:28.378: D/MediaPlayer(4443): at android.media.MediaPlayer.prepare(Native Method)
03-14 00:32:28.378: D/MediaPlayer(4443): at android.media.MediaPlayer.create(MediaPlayer.java:638)
03-14 00:32:28.378: D/MediaPlayer(4443): at android.media.MediaPlayer.create(MediaPlayer.java:615)
03-14 00:32:28.378: D/MediaPlayer(4443): at com.jordan.tester.Main.startPlaying(Main.java:120)
03-14 00:32:28.378: D/MediaPlayer(4443): at com.jordan.tester.Main.onClick(Main.java:89)
03-14 00:32:28.378: D/MediaPlayer(4443): at android.view.View.performClick(View.java:2419)
03-14 00:32:28.378: D/MediaPlayer(4443): at android.view.View$PerformClick.run(View.java:8865)
03-14 00:32:28.378: D/MediaPlayer(4443): at android.os.Handler.handleCallback(Handler.java:587)
03-14 00:32:28.378: D/MediaPlayer(4443): at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 00:32:28.378: D/MediaPlayer(4443): at android.os.Looper.loop(Looper.java:143)
03-14 00:32:28.378: D/MediaPlayer(4443): at android.app.ActivityThread.main(ActivityThread.java:5061)
03-14 00:32:28.378: D/MediaPlayer(4443): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 00:32:28.378: D/MediaPlayer(4443): at java.lang.reflect.Method.invoke(Method.java:521)
03-14 00:32:28.378: D/MediaPlayer(4443): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-14 00:32:28.378: D/MediaPlayer(4443): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-14 00:32:28.378: D/MediaPlayer(4443): at dalvik.system.NativeStart.main(Native Method)
03-14 00:32:28.378: E/TESTER(4443): java.lang.NullPointerException
我的代码
package com.jordan.tester;
import java.io.File;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity implements OnClickListener {
private Button beginRecord, beginPlay, stopRecord, stopPlay;
private MediaPlayer player;
private MediaRecorder recorder;
private String TESTER_TAG = "TESTER";
private File file;
private String tempPath = "R.raw.toto_africa";
private String fn = "temp.3gp";
private File appPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
beginRecord = (Button) findViewById(R.id.beginRecord);
beginPlay = (Button) findViewById(R.id.beginPlay);
stopRecord = (Button) findViewById(R.id.stopRecord);
stopPlay = (Button) findViewById(R.id.stopPlay);
beginRecord.setOnClickListener(this);
beginPlay.setOnClickListener(this);
stopRecord.setOnClickListener(this);
stopPlay.setOnClickListener(this);
file = new File(this.getFilesDir(), fn);
file.delete();
appPath = this.getFilesDir();
Log.e("appPath:", appPath.toString());
file = new File(this.getFilesDir(), fn);
tempPath = file.getAbsolutePath();
Log.e("tempPath:", tempPath);
Log.e("(1) Length of de file mon'?", String.valueOf(file.length()));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.beginRecord:
try {
startRecording();
} catch (Exception e) {
Log.e(TESTER_TAG, e.toString());
}
break;
// ////////////////////////////////////////////////////////////
case R.id.beginPlay:
try {
startPlaying();
} catch (Exception e) {
Log.e(TESTER_TAG, e.toString());
}
break;
// ///////////////////////////////////////////////////////////
case R.id.stopRecord:
try {
stopRecording();
} catch (Exception e) {
Log.e(TESTER_TAG, e.toString());
}
break;
// /////////////////////////////////////////////////////////////
case R.id.stopPlay:
try {
stopPlaying();
} catch (Exception e) {
Log.e(TESTER_TAG, e.toString());
}
break;
}
}
private void startPlaying() {
ditchMediaPlayer();
try {
player = MediaPlayer.create(getBaseContext(), Uri.parse(tempPath));
player.start();
} catch (IllegalStateException e) {
Log.e(TESTER_TAG, e.toString());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}
private void stopPlaying() {
// /
}
private void stopRecording() {
if (recorder != null) {
Log.e("Are ya dead maan?", "Kiss the Lucky egg");
recorder.stop();
recorder.release();
Log.e("IS da file alive?", String.valueOf(file.exists()));
Log.e("IS da file a file?", String.valueOf(file.isFile()));
Log.e("(2) Length of de file mon'?", String.valueOf(file.length()));
}
}
private void startRecording() {
ditchMediaRecorder();
// File outFile = new File(OUTPUT_FILE);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(tempPath);
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
}
private void ditchMediaRecorder() {
if (recorder != null) {
recorder.release();
}
}
private void ditchMediaPlayer() {
if (player != null) {
try {
player.release();
} catch (Exception e) {
Log.e(TESTER_TAG, e.toString());
}
}
}
}