私が過去 8 時間にわたって行ってきたのは、単純なレコーダー/再生アプリを考え出すことだけです。オーディオを 3GP で録音し、それを に自動的にロードして再生できるようにしたいと考えていMediaPlayer
ます。コードで指定された場所に記録および保存されていることは 80% 確信していますが、メディア プレーヤーにロードしようとすると、常にこのエラーが発生します。toto_africa.mp3
記録されたファイルの問題ではないことを確認するためにファイルもロードしようとしましたが、それでもこのエラーが発生します。私はどこでも答えを探したので、このエラーに関する助けをいただければ幸いです。より具体的な質問は次のようになると思います: どうすれば自分MediaPlayer
とMediaRecorder
コミュニケーションを適切に取り、正しく機能させることができますか?
LogCat
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());
}
}
}
}