我有一个类似soundboard的应用程序。我不希望声音停止,即使用户点击另一个声音。但是,过了一段时间,像30点击(声音播放器),它停止。我认为它耗尽内存或Android不允许它创建超过30个示例。
如何更好地实现这一点,以便在声音结束时销毁媒体播放器示例?
import java.io.IOException;
public class AudioRecordTest {
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
private MediaRecorder mRecorder = null;
private MediaPlayer mPlayer = null;
public void startPlaying(int fileName) {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName + fileName + ".3gp");
mPlayer.prepare();
mPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopPlaying() {
try {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public MediaPlayer getPlayer() {
return mPlayer;
}
public void startRecording(int fileName) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName + fileName + ".3gp");
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
e.printStackTrace();
}
try {
mRecorder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopRecording() {
try {
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public AudioRecordTest() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/aaa";
}
}
4条答案
按热度按时间2wnc66cl1#
试试这个
8ulbf1ek2#
试试这个:
或者:
smtd7mpg3#
您可以使用medeiaplayer的
release(
)lnlaulya4#
我在Kotlin的所作所为: