MediaPlayer setDataSource为String

时间:2014-07-27 13:56:19

标签: android android-mediaplayer android-mediarecorder

我正在尝试使用MediaRecorder和MediaPlayer,

我已经以这种方式设置了MediaRecorder:

mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setOutputFile(getFilename());
            try {
                mRecorder.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Context context = getApplicationContext();
            CharSequence text = "Start recording";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            mRecorder.start();
            String filename = getFilename();

以这种方式取消了它:

    if (null != mRecorder) {
                mRecorder.stop();
                mRecorder.reset();
                mRecorder.release();
                mRecorder = null; }

我以这种方式使用getFilename作为outputFile:

private String getFilename() {
    File file = new File("/sdcard/20MinRecord/");
    if (!file.exists()) {
        file.mkdirs();
    }

    String mydate = java.text.DateFormat.getDateTimeInstance().format(
            Calendar.getInstance().getTime());
    return (file.getAbsolutePath() + "/" + mydate + ".mp3");

}

我尝试将MediaPlayer用于录制的文件:

    public String filename;
    .... 
    // When I started my recorder I got the file name into String like this:
    String filename = getFilename();
    ....
    // Starting the MediaPlayer:
    MediaPlayer mediaPlayer = new MediaPlayer();
                mediaPlayer.setDataSource(filename);
                mediaPlayer.prepare();
                mediaPlayer.start();

我遇到了MediaPlayer的问题,我使用try / catch解决了它们:

null != mRecorder) {
                MediaPlayer mediaPlayer = new MediaPlayer();
                try {
                    mediaPlayer.setDataSource(filename);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    mediaPlayer.prepare();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.start();

但是当我尝试使用MediaPlayer,LOGCAT:

时,我的应用程序崩溃了
07-27 16:55:33.156: D/MediaPlayer[Native](11856): calling notify_player_state: 1
07-27 16:55:33.156: V/MediaPlayer[Native](11856): setListener
07-27 16:55:33.156: E/ExtMediaPlayer-JNI(11856): QCMediaPlayer could not be located....
07-27 16:55:33.156: E/MediaPlayer-JNI(11856): QCMediaPlayer mediaplayer NOT present
07-27 16:55:33.156: D/AndroidRuntime(11856): Shutting down VM
07-27 16:55:33.156: W/dalvikvm(11856): threadid=1: thread exiting with uncaught exception (group=0x417bee48)
07-27 16:55:33.156: I/ViewRootImpl(11856): ViewRoot's Touch Event : ACTION_UP
07-27 16:55:33.166: V/AudioSystem(11856): ioConfigChanged() event 0, ioHandle 3
07-27 16:55:33.166: V/AudioSystem(11856): ioConfigChanged() new output samplingRate 48000, format 1 channel mask 0x3 frameCount 960 latency 50
07-27 16:55:33.166: E/AndroidRuntime(11856): FATAL EXCEPTION: main
07-27 16:55:33.166: E/AndroidRuntime(11856): Process: com.FAD.recordthelast20minutes, PID: 11856
07-27 16:55:33.166: E/AndroidRuntime(11856): java.lang.NullPointerException: uriString
07-27 16:55:33.166: E/AndroidRuntime(11856):    at android.net.Uri$StringUri.<init>(Uri.java:467)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at android.net.Uri$StringUri.<init>(Uri.java:457)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at android.net.Uri.parse(Uri.java:429)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1059)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1024)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at com.FAD.recordthelast20minutes.RecordActivity$3.onClick(RecordActivity.java:139)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at android.view.View.performClick(View.java:4461)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at android.view.View$PerformClick.run(View.java:18525)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at android.os.Handler.handleCallback(Handler.java:733)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at android.os.Looper.loop(Looper.java:136)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at android.app.ActivityThread.main(ActivityThread.java:5118)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at java.lang.reflect.Method.invokeNative(Native Method)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at java.lang.reflect.Method.invoke(Method.java:515)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
07-27 16:55:33.166: E/AndroidRuntime(11856):    at dalvik.system.NativeStart.main(Native Method)
07-27 16:55:33.176: V/AudioSystem(11856): ioConfigChanged() event 3, ioHandle 669

全部谢谢:)

1 个答案:

答案 0 :(得分:1)

您确定getFilename()方法是否正常工作(例如创建目录?)您可以尝试使用静态路径(存在)和文件名进行测试。