Android:如何使用MediaPlayer播放WAV文件

时间:2015-01-08 06:02:20

标签: android media-player wav

当我尝试使用MediaPlayer播放wav文件时,会在NullPointerException处生成player.start()

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();
}

1 个答案:

答案 0 :(得分:1)

以下是您可以在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();

}

}

}