Android:How 使用 MediaPlayer 播放 WAV 文件
Android:How to play a WAV file using MediaPlayer
当我尝试使用 MediaPlayer
播放 wav 文件时,这会在 player.start()
处导致 NullPointerException
。
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/Notate/"+MainActivity.filepath);
MainActivity.filepath,Toast.LENGTH_LONG).show();
Uri uri=Uri.fromFile(file);
MediaPlayer player=MediaPlayer.create(getActivity(),uri);
try {
player.prepare();
player.start();
}
catch(IOException e){
e.printStackTrace();
}
以下是可用于 Android 中 .wav 文件格式的录制、停止和播放功能的方法,我添加了所有必要的检查:
使用此导入:
以 .wav 文件格式录制:
public class AudioRecord {
private void record() {
if (isExternalStorageWritable() == true) {
File sampleDir = Environment.getExternalStorageDirectory();
File dir = new File(sampleDir, "/VoiceRecorder");
if (!dir.isDirectory())
dir.mkdirs();
try {
audiofile = File.createTempFile("Recording", ".wav", dir);
} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
// set up media recorder
if (mediarecorder == null) {
mediarecorder = new MediaRecorder();
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediarecorder
.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB);
mediarecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
mediarecorder.setOutputFile(audiofile.getAbsolutePath());
// prepare media recorder
try {
mediarecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// start media recorder
mediarecorder.start();
} catch (Exception e) {
stopTask();
Toast.makeText(
this.getActivity(),
" Audio Recorder not Available,Try some other time",
Toast.LENGTH_LONG).show();
}
}
} else {
Toast.makeText(this.getActivity(),
" Unable to Access External Storage!", Toast.LENGTH_LONG)
.show();
}
}
停止录制:
private void stopRecord() {
// stop media recorder
mediarecorder.stop();
mediarecorder.reset();
// reset media recorder
mediarecorder.release();
mediarecorder = null;
}
开始播放:
import java.io.IOException;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
public class PlayMedia extends AsyncTask<Void, Void, Void>
implements MediaPlayer.OnPreparedListener {
private MediaPlayer mediaPlayer;
@Override
protected Void doInBackground(Void... params) {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(FilePath);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.start();
}
}
}
当我尝试使用 MediaPlayer
播放 wav 文件时,这会在 player.start()
处导致 NullPointerException
。
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/Notate/"+MainActivity.filepath);
MainActivity.filepath,Toast.LENGTH_LONG).show();
Uri uri=Uri.fromFile(file);
MediaPlayer player=MediaPlayer.create(getActivity(),uri);
try {
player.prepare();
player.start();
}
catch(IOException e){
e.printStackTrace();
}
以下是可用于 Android 中 .wav 文件格式的录制、停止和播放功能的方法,我添加了所有必要的检查: 使用此导入:
以 .wav 文件格式录制:
public class AudioRecord {
private void record() {
if (isExternalStorageWritable() == true) {
File sampleDir = Environment.getExternalStorageDirectory();
File dir = new File(sampleDir, "/VoiceRecorder");
if (!dir.isDirectory())
dir.mkdirs();
try {
audiofile = File.createTempFile("Recording", ".wav", dir);
} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
// set up media recorder
if (mediarecorder == null) {
mediarecorder = new MediaRecorder();
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediarecorder
.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB);
mediarecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
mediarecorder.setOutputFile(audiofile.getAbsolutePath());
// prepare media recorder
try {
mediarecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// start media recorder
mediarecorder.start();
} catch (Exception e) {
stopTask();
Toast.makeText(
this.getActivity(),
" Audio Recorder not Available,Try some other time",
Toast.LENGTH_LONG).show();
}
}
} else {
Toast.makeText(this.getActivity(),
" Unable to Access External Storage!", Toast.LENGTH_LONG)
.show();
}
}
停止录制:
private void stopRecord() {
// stop media recorder
mediarecorder.stop();
mediarecorder.reset();
// reset media recorder
mediarecorder.release();
mediarecorder = null;
}
开始播放:
import java.io.IOException;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
public class PlayMedia extends AsyncTask<Void, Void, Void>
implements MediaPlayer.OnPreparedListener {
private MediaPlayer mediaPlayer;
@Override
protected Void doInBackground(Void... params) {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(FilePath);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.start();
}
}
}