保存后视频无法播放

时间:2012-05-22 11:00:45

标签: android

 MediaRecorder m_recorder = new MediaRecorder();
    m_recorder.setPreviewDisplay(s);
    m_recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    m_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    m_recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    m_recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    m_recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    m_recorder.setMaxDuration(20000); // length of video in MS
    m_recorder.setVideoSize(320, 240);
    m_recorder.setVideoFrameRate(15);
    m_recorder.setOutputFile(video.getPath());
    m_recorder.prepare();

mp4文件保存在SD卡上但该文件没有播放。

2 个答案:

答案 0 :(得分:1)

请参阅以下内容,

public class AudioRecordingActivity extends Activity implements OnClickListener{

    MediaRecorder recorder;
    final String fileNameStr = "audio-android.3gp";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ((Button) findViewById(R.id.btn_record)).setOnClickListener(this);
        ((Button) findViewById(R.id.btn_stop)).setOnClickListener(this);
        ((Button) findViewById(R.id.btn_play)).setOnClickListener(this);
    }

    public void onClick(View v) {
        int id = v.getId();

         switch (id) {
         case R.id.btn_record: {
             System.out.println("Record pressed.");
             try {

                 recorder = new MediaRecorder();
                 FileOutputStream fos = openFileOutput(fileNameStr, MODE_PRIVATE);

                 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                 recorder.setOutputFile(fos.getFD());
                 recorder.prepare();
                 recorder.start();

             } catch (IOException e) {
                 System.out.println("IOException caught during recording.");
                 e.printStackTrace();
             }
             return;
         }
         case R.id.btn_stop: {
             recorder.stop();
             recorder.release();
             System.out.println("Stop pressed.");
             return;
         }
         case R.id.btn_play: {
             System.out.println("Play pressed.");

             MediaPlayer mp = new MediaPlayer();
             try {
                 FileInputStream fis = openFileInput(fileNameStr);
                 mp.setDataSource(fis.getFD());
                 mp.prepare();
             } catch (IllegalArgumentException e) {
                 e.printStackTrace();
             } catch (IllegalStateException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
             mp.start();

             return;
         }
         }
    }
}

<强> main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/btn_record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start" />


<Button
    android:id="@+id/btn_stop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/stop" />


<Button
    android:id="@+id/btn_play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/play" />

</LinearLayout>

答案 1 :(得分:-1)

也试试这个,

public class AudioActivity extends Activity{

    public static int RECORD_REOUEST = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intt = new Intent(Media.RECORD_SOUND_ACTION);
        startActivityForResult(intt, RECORD_REOUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {   
         if (resultCode == RESULT_OK && requestCode == RECORD_REOUEST) 
         {
             Uri recordedAudioPath = data.getData();
             String FullPath=recordedAudioPath.toString();
                  }
         }
 }