所有我已经做了近8个小时试图想出了一个简单的录音/播放应用程序。我希望能够记录在3GP的音频,然后让它自动加载到的MediaPlayer
,这样我可以打回来。我80%肯定它是记录和保存在code指定的位置,但是当我试图将其加载到我的媒体播放器,我总是得到这个错误。我曾尝试加载 toto_africa.mp3
文件太多,以确保它不是记录文件的问题,它仍然给了我这个错误。任何帮助,这个错误会大大AP preciated因为我到处去寻找答案。我想一个更具体的问题是:我如何正确地得到我的的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)长度(4443):0
03-14 00:32:24.408:E /雅是死马安(4443):亲吻幸运蛋
03-14 00:32:24.688:E /是岘文件还活着(4443):真
03-14 00:32:24.688:E / IS DA文件的文件(4443):真
03-14 00:32:24.688:中日文件星期一'E /(2)长度(4443):4515
03-14 00:32:28.362:V / MediaPlayer的(4443):构造函数
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):收到的消息味精= 100,外部1 = 1,EXT2 = -2147483648
03-14 00:32:28.368:E / MediaPlayer的(4443):错误(1,-2147483648)
03-14 00:32:28.368:V / MediaPlayer的(4443):信号应用程序线程
03-14 00:32:28.368:V / MediaPlayer的(4443):prepare完成 - 状态= 1
03-14 00:32:28.378:D / MediaPlayer的(4443):创建失败:
03-14 00:32:28.378:D / MediaPlayer的(4443):java.io.IOException异常:prepare失败:状态=为0x1
03-14 00:32:28.378:D / MediaPlayer的(4443):在android.media.MediaPlayer prepare(本机方法)
03-14 00:32:28.378:D / MediaPlayer的(4443):在android.media.MediaPlayer.create(MediaPlayer.java:638)
03-14 00:32:28.378:D / MediaPlayer的(4443):在android.media.MediaPlayer.create(MediaPlayer.java:615)
03-14 00:32:28.378:D / MediaPlayer的(4443):在com.jordan.tester.Main.startPlaying(Main.java:120)
03-14 00:32:28.378:D / MediaPlayer的(4443):在com.jordan.tester.Main.onClick(Main.java:89)
03-14 00:32:28.378:D / MediaPlayer的(4443):在android.view.View.performClick(View.java:2419)
03-14 00:32:28.378:D / MediaPlayer的(4443):在android.view.View $ PerformClick.run(View.java:8865)
03-14 00:32:28.378:D / MediaPlayer的(4443):在android.os.Handler.handleCallback(Handler.java:587)
03-14 00:32:28.378:D / MediaPlayer的(4443):在android.os.Handler.dispatchMessage(Handler.java:92)
03-14 00:32:28.378:D / MediaPlayer的(4443):在android.os.Looper.loop(Looper.java:143)
03-14 00:32:28.378:D / MediaPlayer的(4443):在android.app.ActivityThread.main(ActivityThread.java:5061)
03-14 00:32:28.378:D / MediaPlayer的(4443):在java.lang.reflect.Method.invokeNative(本机方法)
03-14 00:32:28.378:D / MediaPlayer的(4443):在java.lang.reflect.Method.invoke(Method.java:521)
03-14 00:32:28.378:D / MediaPlayer的(4443):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
03-14 00:32:28.378:D / MediaPlayer的(4443):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-14 00:32:28.378:D / MediaPlayer的(4443):在dalvik.system.NativeStart.main(本机方法)
03-14 00:32:28.378:E /测试仪(4443):显示java.lang.NullPointerException
包com.jordan.tester;
进口的java.io.File;
进口java.io.IOException异常;
进口android.media.MediaPlayer;
进口android.media.MediaRecorder;
进口android.net.Uri;
进口android.os.Bundle;
进口android.app.Activity;
进口android.util.Log;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
公共类主要活动扩展实现OnClickListener {
私人按钮beginRecord,beginPlay,STO precord,stopPlay;
私人的MediaPlayer播放器;
私人MediaRecorder记录;
私人字符串TESTER_TAG =测试;
私人文件的文件;
私人字符串TEMPPATH =R.raw.toto_africa;
私人字符串FN =temp.3gp;
私人文件APPPATH;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
beginRecord =(按钮)findViewById(R.id.beginRecord);
beginPlay =(按钮)findViewById(R.id.beginPlay);
STO precord =(按钮)findViewById(R.id.sto precord);
stopPlay =(按钮)findViewById(R.id.stopPlay);
beginRecord.setOnClickListener(本);
beginPlay.setOnClickListener(本);
STO precord.setOnClickListener(本);
stopPlay.setOnClickListener(本);
文件=新的文件(this.getFilesDir(),FN);
file.delete();
APPPATH = this.getFilesDir();
Log.e(APPPATH:,appPath.toString());
文件=新的文件(this.getFilesDir(),FN);
TEMPPATH = file.getAbsolutePath();
Log.e(TEMPPATH:,TEMPPATH);
Log.e((1)中日文件问'长?,将String.valueOf(file.length()));
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
@覆盖
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.beginRecord:
尝试 {
的StartRecording();
}赶上(例外五){
Log.e(TESTER_TAG,e.toString());
}
打破;
// //////////////////////////////////////////////// ////////////
案例R.id.beginPlay:
尝试 {
startPlaying();
}赶上(例外五){
Log.e(TESTER_TAG,e.toString());
}
打破;
// //////////////////////////////////////////////// ///////////
案例R.id.sto precord:
尝试 {
STO precording();
}赶上(例外五){
Log.e(TESTER_TAG,e.toString());
}
打破;
// //////////////////////////////////////////////// /////////////
案例R.id.stopPlay:
尝试 {
stopPlaying();
}赶上(例外五){
Log.e(TESTER_TAG,e.toString());
}
打破;
}
}
私人无效startPlaying(){
ditchMediaPlayer();
尝试 {
球员= MediaPlayer.create(getBaseContext(),Uri.parse(TEMPPATH));
player.start();
}赶上(IllegalStateException异常E){
Log.e(TESTER_TAG,e.toString());
}赶上(抛出:IllegalArgumentException E){
e.printStackTrace();
}赶上(SecurityException异常E){
e.printStackTrace();
}
}
私人无效stopPlaying(){
// /
}
私人无效STO precording(){
如果(录像机!= NULL){
Log.e(是雅死了马安?,吻幸运蛋);
recorder.stop();
recorder.release();
Log.e(是岘文件还活着吗?,将String.valueOf(file.exists()));
Log.e(是岘文件的文件?,将String.valueOf(file.isFile()));
Log.e((2)中去的文件问'长?,将String.valueOf(file.length()));
}
}
私人无效的startRecording(){
ditchMediaRecorder();
//文件不过outFile =新的文件(OUTPUT_FILE);
记录=新MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
recorder.setOutputFile(TEMPPATH);
尝试 {
。录音机prepare();
}赶上(IllegalStateException异常E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}
recorder.start();
}
私人无效ditchMediaRecorder(){
如果(录像机!= NULL){
recorder.release();
}
}
私人无效ditchMediaPlayer(){
如果(玩家!= NULL){
尝试 {
player.release();
}赶上(例外五){
Log.e(TESTER_TAG,e.toString());
}
}
}
}
解决方案
从你的logcat日志,我发现的的setDataSource
部分的创建
失败。从你的日志,
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):收到的消息味精= 100,外部1 = 1,EXT2 = -2147483648
03-14 00:32:28.368:E / MediaPlayer的(4443):错误(1,-2147483648)
由于部分 prepare
,玩家引擎实现等待 finishSetDataSource
。如果底层球员引擎无法设置数据源
,它会返回一个 UNKNOWN_ERROR
相当于为0x80000000
或 -2147483648
。
我相信这个问题从字符串所产生传递到的setDataSource
。从上面转载的日志,该字符串是 /data/data/com.jordan.tester/files/temp.3gp
,我觉得不符合任何已知的实现方法文件或数据流的来源。
要解决你的问题,我建议你要么追加文件://
您输入的字符串即字符串传递创造可能是文件:///data/data/com.jordan.tester/files/temp.3gp
或打开Java本身的文件,并通过文件描述符创建
。
All I have been doing for the past 8 hours is trying to come up with a simple recorder/ playback app. I want to be able to record the audio in 3GP and then have it automatically loaded into the MediaPlayer
so that I can play it back. I am 80% sure that it is recording and saving to the location specified in the code, but when I try and load it into my media player I always get this error. I have tried loading the toto_africa.mp3
file too to make sure it's not the recorded file's problem, and it still gives me this error. Any help with this error would be much appreciated because I have searched everywhere for the answer. I guess a more specific question would be: How do I properly get my MediaPlayer
and MediaRecorder
communicating and functioning correctly?
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());
}
}
}
}
解决方案
From your logcat logs, I find that the setDataSource
part of your Create
is failing. From your logs,
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)
As part of the prepare
, the player engine implementation waits for finishSetDataSource
. If the underlying player engine couldn't set a DataSource
, it will return an UNKNOWN_ERROR
which corresponds to 0x80000000
or -2147483648
.
I believe the problem is stemming from the string being passed to the setDataSource
. From the logs reproduced above, the string is /data/data/com.jordan.tester/files/temp.3gp
, which I feel doesn't match any of the known implementation methods for file or streaming sources.
To solve your problem, I would recommend you to either append a file://
to your input string i.e. the string being passed to create could be file:///data/data/com.jordan.tester/files/temp.3gp
or open the file in Java itself and pass a file descriptor to create
.