录制的文件无法播放

时间:2017-03-07 13:07:51

标签: android mediarecorder

我打算创建一个呼叫录音应用程序,下面我展示了代码,当我按下录音按钮打电话后,应用程序在存储中创建一个文件,但是mp3播放器没有播放该文件,我想知道我应该做些什么改变。

public class tab2 extends Fragment {

private Button play,stop,record;
private MediaRecorder myAudioRecorder;
private String outputfile;
public tab2() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View rootview=inflater.inflate(R.layout.fragment_tab2,container,false);


    Button record=(Button)rootview.findViewById(R.id.record);




    File directorysample=new File(Environment.getExternalStorageDirectory()+separator+"CallRecords");
    directorysample.mkdirs();
    String filename = "/recording_" + System.currentTimeMillis() + ".mp3";
    final  String directory = Environment.getExternalStorageDirectory()+ "/CallRecords";
    outputfile = directory + "/" + filename;



    record.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {


                myAudioRecorder=new MediaRecorder();
                myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
                myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                //myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.DEFAULT);
                myAudioRecorder.setOutputFile(outputfile);
                myAudioRecorder.prepare();
                myAudioRecorder.start();


            }
            catch (IllegalStateException ise){



            }catch (IOException ioe){



            }

        }

    });






    return rootview;
}

}

1 个答案:

答案 0 :(得分:0)

如果您的文件使用0kb创建,那么您可以尝试关注记录文件。

myAudioRecorder = new MediaRecorder();
        myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);


        myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myAudioRecorder.setOutputFile(outputFile);

        myAudioRecorder.prepare();
        myAudioRecorder.start();