• 音频技术


     1 //播放资源文件下和scdard下的音频
     2 public class Main extends Activity implements OnClickListener,
     3         OnCompletionListener {
     4     private MediaPlayer mediaPlayer;
     5     private Button btnPause;
     6 
     7     @Override
     8     public void onCreate(Bundle savedInstanceState) {
     9         super.onCreate(savedInstanceState);
    10         setContentView(R.layout.main);
    11         // 播放资源文件的音频
    12         Button btnStart1 = (Button) findViewById(R.id.btnStart1);
    13         // 播放sdcard里边的音频文件
    14         Button btnStart2 = (Button) findViewById(R.id.btnStart2);
    15 
    16         Button btnStop = (Button) findViewById(R.id.btnStop);
    17 
    18         btnPause = (Button) findViewById(R.id.btnPause);
    19 
    20         btnStart1.setOnClickListener(this);
    21         btnStart2.setOnClickListener(this);
    22         btnStop.setOnClickListener(this);
    23         btnPause.setOnClickListener(this);
    24 
    25     }
    26 
    27     // 播放完成
    28     public void onCompletion(MediaPlayer mp) {
    29         mp.release();
    30         setTitle("资源已经释放");
    31 
    32     }
    33 
    34     public void onClick(View view) {
    35         try {
    36             switch (view.getId()) {
    37             case R.id.btnStart1: // 播放音频资源
    38                 mediaPlayer = MediaPlayer.create(this, R.raw.music);
    39                 mediaPlayer.setOnCompletionListener(this);
    40 
    41                 mediaPlayer.start();
    42 
    43                 break;
    44             case R.id.btnStart2:// 播放SD卡上的音频文件(/sdcard/music.mp3)
    45                 mediaPlayer = new MediaPlayer();
    46                 mediaPlayer.setDataSource("/sdcard/music.mp3");
    47 
    48                 mediaPlayer.prepare();
    49                 mediaPlayer.start();
    50                 break;
    51 
    52             case R.id.btnStop:
    53                 if (mediaPlayer != null) {
    54                     if (mediaPlayer.isPlaying())
    55                         mediaPlayer.stop();
    56                 }
    57                 break;
    58 
    59             case R.id.btnPause:
    60                 if (mediaPlayer != null) {
    61                     if ("播放".equals(btnPause.getText().toString())) {
    62                         mediaPlayer.start();
    63                         btnPause.setText("暂停");
    64 
    65                     } else if ("暂停".equals(btnPause.getText().toString())) {
    66                         mediaPlayer.pause();
    67                         btnPause.setText("播放");
    68                     }
    69                 }
    70                 break;
    71             default:
    72                 break;
    73             }
    74         } catch (Exception e) {
    75             // TODO: handle exception
    76         }
    77     }
    78 }

     1 //录制音频
     2 public class Main extends Activity implements OnClickListener {
     3     private File recordAudioFile;
     4     private MediaRecorder mediaRecorder;
     5     private MediaPlayer mediaPlayer;
     6 
     7     @Override
     8     public void onCreate(Bundle savedInstanceState) {
     9         super.onCreate(savedInstanceState);
    10         setContentView(R.layout.main);
    11         // 录制,停止,播放,删除
    12         Button btnRecord = (Button) findViewById(R.id.btnRecord);
    13         Button btnStop = (Button) findViewById(R.id.btnStop);
    14         Button btnPlay = (Button) findViewById(R.id.btnPlay);
    15         Button btnDelete = (Button) findViewById(R.id.btnDelete);
    16 
    17         btnRecord.setOnClickListener(this);
    18         btnStop.setOnClickListener(this);
    19         btnPlay.setOnClickListener(this);
    20         btnDelete.setOnClickListener(this);
    21 
    22     }
    23 
    24     public void onClick(View view) {
    25         try {
    26             switch (view.getId()) {
    27             case R.id.btnRecord: // 录制音频
    28                 recordAudioFile = File.createTempFile("record", ".amr");
    29                 mediaRecorder = new MediaRecorder();
    30                 // 指定音频采集源--麦克风
    31                 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    32                 // 指定录制格式 mpeg4
    33                 mediaRecorder
    34                         .setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    35                 // 设置编码
    36                 mediaRecorder
    37                         .setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    38                 // 录制生成文件
    39                 mediaRecorder.setOutputFile(recordAudioFile.getAbsolutePath());
    40                 mediaRecorder.prepare();
    41                 mediaRecorder.start();
    42                 Toast.makeText(this, "开始录音", Toast.LENGTH_LONG).show();
    43 
    44                 break;
    45             // 停止
    46             case R.id.btnStop:
    47                 if (mediaRecorder != null) {
    48                     mediaRecorder.stop();
    49                     mediaRecorder.release();
    50                     mediaRecorder = null;
    51                     Toast.makeText(this, "停止录音", Toast.LENGTH_LONG).show();
    52                 }
    53                 break;
    54             // 播放
    55             case R.id.btnPlay:
    56                 mediaPlayer = new MediaPlayer();
    57                 mediaPlayer.setDataSource(recordAudioFile.getAbsolutePath());
    58                 mediaPlayer.prepare();
    59                 mediaPlayer.start();
    60                 break;
    61             // 删除
    62             case R.id.btnDelete:
    63                 recordAudioFile.delete();
    64                 break;
    65             default:
    66                 break;
    67             }
    68 
    69         } catch (Exception e) {
    70             // TODO: handle exception
    71         }
    72     }
    73 }

  • 相关阅读:
    VS2010开发C的方法
    Activity.startManagingCursor方法
    application/xwwwformurlencoded、multipart/formdata、text/plain
    TCP三次握手/四次挥手详解
    hdu 2157 How many ways??
    zoj 2475 Benny's Compiler
    zoj 2744 Palindromes
    zoj 2750 Idiomatic Phrases Game
    zoj 2104 Let the Balloon Rise
    flash:学习ActionScript的一些提示
  • 原文地址:https://www.cnblogs.com/my334420/p/6921508.html
Copyright © 2020-2023  润新知