android 之MediaPlayer MP3播放,VideoView 视频播放,MediaRecorder 录音

1》MeidaPlayer播放指定路径下的MP3文件;

2》MeidaPlayer播放网络上的MP3文件;

3》VideoView 视频播放;

4》MediaRecorder 录音;

 

1》MeidaPlayer播放指定路径下的MP3文件;

MediaPlayer mp=new MediaPlayer();

mp.setDataSource("path");

mp.prepare();

mp.start();

 mp.isLooping;

mp.pause();

mp.reset();

mp.release();

 

MediaPlayer mp=new MediaPlayer();

mp=MediaPlayer.create(demomdplayer01.this,R.Raw.test);    //res/Raw/test.mp3

mp.prepare();

mp.start();

 

 

2》MeidaPlayer播放网络上的MP3文件;

MediaPlayer mp=new MediaPlayer();

Uri uri=Uri.parse(www.cnblogs.com/team/ShenzhenAndroidClub/android.mp3);

mp=MediaPlayer.create(demomdplayer01.this,uri);  

mp.prepare();

mp.start();

 

 

3》VideoView 视频播放

 

VideoView mvd=new VideoView();

  mvd.setVideopath("path");//这两种方法都可以 mvd.setVideoPath("file:///sdcard/love_480320.mp4");

                         //mvd.setVideoURI(Uri.parse("/sdcard/love_480320.mp4"));

 //定义MediaController对象 

 MediaController mediaController = new MediaController(this); 

//把MediaController对象绑定到VideoView上 

 mediaController.setAnchorView(mvd); 

//设置VideoView的控制器是mediaController 

 mvd..setMediaController(mediaController); 

  mvd.requestFocus();

  mvd.start();

  mvd.pause();

   

4> MediaRecorder 录音;

MediaRecorder mr = new MediaRecorder(); 

 mr.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 

 mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 

 mr.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);  
 mr.setOutputFile(file.getAbsolutePath()); 

                    mr.prepare(); 

                    mr.start(); 

                    mr.stop(); 

                    mr.release(); 

 

// 创建录音对象 

    MediaRecorder mr = new MediaRecorder(); 

        // 从麦克风源进行录音 
              mr.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 

             // 设置输出格式 
               mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 

                // 设置编码格式 

                 mr.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);  
                 // 设置输出文件 

                 mr.setOutputFile(file.getAbsolutePath()); 

               try { 

                     // 创建文件 

                   file.createNewFile(); 

                  // 准备录制 

                   mr.prepare(); 

                } catch (IllegalStateException e) { 

                     e.printStackTrace(); 

                } catch (IOException e) { 

                   e.printStackTrace(); 

                } 

                // 开始录制 

               mr.start(); 
               recordButton.setText("录音中……"); 

             } 

        }); 
   

        // 停止按钮点击事件 
         stopButton.setOnClickListener(new View.OnClickListener() { 

            @Override

             public void onClick(View v) { 

                if (mr != null) { 

                    mr.stop(); 

                    mr.release(); 
                    mr = null; 

                   recordButton.setText("录音"); 

                     Toast.makeText(getApplicationContext(), "录音完毕", Toast.LENGTH_LONG).show(); 

                } 

             } 

        });

 

posted @ 2011-06-16 17:39  大树2  阅读(1406)  评论(0编辑  收藏  举报