我正在查询我遇到的问题。我正在尝试使用MediaRecorder录制实时流媒体音频但是在调用stoprecording功能时它显示成功Toast甚至 - 虽然我无法在我的内部存储中找到文件。希望文件不写。
MainActivity
public class MainActivity extends AppCompatActivity implements OnClickListener {
private final static String URL_Radio = "http:livestramingurl";
private FloatingActionButton buttonplay;
private FloatingActionButton buttonstop;
// private Button buttonrecord;
// private Button buttonrecordstop;
private MediaPlayer player;
private InputStream recordingStream;
// private RecorderThread recordingsystem;
private boolean isRecording = false;
private ImageView image, imagep;
private MediaRecorder myAudioRecorder;
private String outputfile = null;
RelativeLayout rlvmain;
public Dialog mBottomSheetDialog;
public static Toolbar mtoolbar;
public Boolean b = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtoolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mtoolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
image = (ImageView) findViewById(R.id.img);
imagep = (ImageView) findViewById(R.id.img1);
outputfile = getFilesDir().getPath()+ "downloadedFile.3gp";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputfile);
Picasso.with(this)
.load("myimageurl")
.into(image);
Picasso.with(this)
.load("myimageurl")
.into(imagep);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
buttonplay = (FloatingActionButton) findViewById(R.id.buttonPlay);
buttonplay.setVisibility(View.VISIBLE);
buttonplay.setOnClickListener(this);
buttonstop = (FloatingActionButton) findViewById(R.id.buttonStopPlay);
buttonstop.setEnabled(false);
buttonstop.setVisibility(View.GONE);
buttonstop.setOnClickListener(this);
//
// buttonrecord = (Button) findViewById(R.id.buttonRecord);
// buttonrecord.setOnClickListener(this);
//
// buttonrecordstop = (Button) findViewById(R.id.buttonStopRecord);
// buttonrecordstop.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonplay) {
startPlaying();
} else if (v == buttonstop) {
buttonplay.setVisibility(View.VISIBLE);
stopPlaying();
}
// else if (v == buttonrecord) {
//// recordingsystem = new RecorderThread();
//// recordingsystem.start();
// startRecording();
//
// buttonrecord.setEnabled(false);
// buttonrecordstop.setEnabled(true);
// buttonrecord.setVisibility(View.GONE);
// buttonrecordstop.setVisibility(View.VISIBLE);
// } else if (v == buttonrecordstop) {
//// stopRecording();
// myAudioRecorder.stop();
// myAudioRecorder.release();
// myAudioRecorder = null;
// buttonstop.setVisibility(View.VISIBLE);
// buttonplay.setVisibility(View.GONE);
// buttonrecord.setVisibility(View.VISIBLE);
// Toast.makeText(this, "Audio recorded successfully",Toast.LENGTH_LONG).show();
// }
}
private void startRecording() {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
private void startPlaying() {
buttonstop.setEnabled(true);
buttonplay.setVisibility(View.GONE);
buttonstop.setVisibility(View.VISIBLE);
try {
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
// buttonrecord.setEnabled(true);
}
});
} catch (IllegalStateException e) {
e.printStackTrace();
Log.e("player", "Slow Internet Connection");
Toast.makeText(getApplicationContext(), "Slow Internet Connection", Toast.LENGTH_LONG).show();
}
}
private void stopPlaying() {
buttonplay.setEnabled(true);
buttonstop.setEnabled(false);
buttonplay.setVisibility(View.VISIBLE);
buttonstop.setVisibility(View.GONE);
// buttonrecord.setVisibility(View.VISIBLE);
// buttonrecordstop.setVisibility(View.VISIBLE);
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
// stopRecording();
}
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource(URL_Radio);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// playSeekBar.setSecondaryProgress(percent);
// Log.i("Buffering", "" + percent);
}
});
}
@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
buttonstop.setVisibility(View.GONE);
}
}
// private void startRecording() {
//
// BufferedOutputStream writer = null;
// try {
// URL url = new URL(URL_Radio);
// URLConnection connection = url.openConnection();
// final String FOLDER_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
// + File.separator + "Songs";
//
// File folder = new File(FOLDER_PATH);
// if (!folder.exists()) {
// folder.mkdir();
// }
//
// writer = new BufferedOutputStream(new FileOutputStream(new File(FOLDER_PATH
// + File.separator + "sample.mp3")));
// recordingStream = connection.getInputStream();
//
// final int BUFFER_SIZE = 100;
//
// byte[] buffer = new byte[BUFFER_SIZE];
//
// while (recordingStream.read(buffer, 0, BUFFER_SIZE) != -1 && isRecording) {
// writer.write(buffer, 0, BUFFER_SIZE);
// writer.flush();
// }
//
// } catch (MalformedURLException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// try {
// recordingStream.close();
// writer.flush();
// writer.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
//
private void stopRecording() {
// try {
myAudioRecorder.stop();
myAudioRecorder.release();
// myAudioRecorder = null;
/* } catch (IllegalStateException e) {
e.printStackTrace();
Log.d("record","stop function failed");
}*/
Toast.makeText(getApplicationContext(), "Audio recorded successfully", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_record) {
if (b) {
b = false;
stopRecording();
} else {
b = true;
startRecording();
}
return true;
}
return super.onOptionsItemSelected(item);
}
// private class RecorderThread extends Thread {
// @Override
// public void run() {
// isRecording = true;
// startRecording();
// }
//
// };
}
给予Manifest中的权限,例如
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />