当前位置:  编程技术>移动开发
本页文章导读:
    ▪MediaRecorder摄制音频        MediaRecorder录制音频 一个录音的小程序!!Main.xml<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.recorder" android:versionCode="1" android:version.........
    ▪ App播发Music        App播放Music /** * 播放录音文件 * @param file */ public void PlayMusic(File file){ Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTI.........
    ▪ 游戏中点染线程与更新线程交替执行       游戏中渲染线程与更新线程交替执行 private final State mThreadLocker = new State(); private UpdateThread updateThread; private DrawThread drawThread;  @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(save.........

[1]MediaRecorder摄制音频
    来源: 互联网  发布时间: 2014-02-18
MediaRecorder录制音频
一个录音的小程序!!

Main.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.recorder"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".RecorderActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
	<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
</manifest>


RecorderActivity.java
package com.recorder;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class RecorderActivity extends Activity {
	/** Called when the activity is first created. */  
    private ListView mListView = null;  
    private Button btn_start = null;  
    private Button btn_stop = null;  
    private MediaRecorder mMediaRecorder = null;  
    private List<String> rec = new ArrayList<String>();// 存放录音文件  
    private File home = null;  
    private File path = null;  
    private String temp = "recaudio_";// 临时文件前缀  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        mListView = (ListView) this.findViewById(R.id.listView1);  
        btn_start = (Button) this.findViewById(R.id.start);  
        btn_stop = (Button) this.findViewById(R.id.stop);  
        // 是否存在SD卡  
        if (Environment.getExternalStorageState().equals(  
                Environment.MEDIA_MOUNTED)) {  
            home = Environment.getExternalStorageDirectory();  
            MusicList();  
        } else {  
            Toast.makeText(this, "请先插入SD卡", Toast.LENGTH_LONG).show();  
            return;  
        }  
        btn_start.setOnClickListener(new Button.OnClickListener() {  
  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                try {  
                    // 创建录音临时文件  
                    path = File.createTempFile(temp, ".amr", home);  
                    setTitle("=="+path.getAbsolutePath());  
                    mMediaRecorder = new MediaRecorder();  
                      
                    mMediaRecorder  
                            .setAudioSource(MediaRecorder.AudioSource.MIC);// 设置数据来源,麦克风  
                    mMediaRecorder  
                            .setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);// 设置格式  
                    mMediaRecorder  
                            .setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);// 设置编码  
                    mMediaRecorder.setOutputFile(path.getAbsolutePath());// 设置输出文件路径  
                    mMediaRecorder.prepare();  
                    mMediaRecorder.start();  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }  
  
        });  
        btn_stop.setOnClickListener(new Button.OnClickListener() {  
  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                mMediaRecorder.stop();  
                mMediaRecorder.release();  
                mMediaRecorder = null;  
                MusicList();  
            }  
  
        });  
        mListView.setOnItemClickListener(new OnItemClickListener(){  
  
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
                    long arg3) {  
                // TODO Auto-generated method stub  
                String path = home+File.separator+rec.get(arg2);  
                File f = new File(path);  
                PlayMusic(f);  
            }  
              
        });  
    }  
  
    /** 
     * 显示列表 
     */  
    public void MusicList() {  
        File[] f = home.listFiles(new MusicFilter());  
                  rec.clear();  
        for (int i = 0; i < f.length; i++) {  
            File file = f[i];  
            rec.add(file.getName());  
        }  
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  
                android.R.layout.simple_list_item_1, rec);  
        mListView.setAdapter(adapter);  
    }  
  
    /** 
     * 播放录音文件 
     * @param file 
     */  
    public void PlayMusic(File file){  
        Intent intent = new Intent();  
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        intent.setAction(Intent.ACTION_VIEW);  
        intent.setDataAndType(Uri.fromFile(file), "audio");  
        this.startActivity(intent);  
    }  
    class MusicFilter implements FilenameFilter {  
  
        public boolean accept(File dir, String filename) {  
            // TODO Auto-generated method stub  
            return (filename.endsWith(".amr"));  
        }  
  
    } 
}

    
[2] App播发Music
    来源: 互联网  发布时间: 2014-02-18
App播放Music
/** 
     * 播放录音文件 
     * @param file 
     */  
    public void PlayMusic(File file){  
        Intent intent = new Intent();  
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        intent.setAction(Intent.ACTION_VIEW);  
        intent.setDataAndType(Uri.fromFile(file), "audio");  
        this.startActivity(intent);  
    }

    
[3] 游戏中点染线程与更新线程交替执行
    来源: 互联网  发布时间: 2014-02-18
游戏中渲染线程与更新线程交替执行

private final State mThreadLocker = new State();
 private UpdateThread updateThread;
 private DrawThread drawThread;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  updateThread = new UpdateThread();
  updateThread.start();
  drawThread = new DrawThread();
  drawThread.start();
 }

 protected void onUpdate() throws InterruptedException {
  final State threadLocker = this.mThreadLocker;
  threadLocker.waitUntilCanUpdate();
  System.out.println("Update");
  threadLocker.notifyCanDraw();

 }

 public void onDrawFrame() throws InterruptedException {
  final State threadLocker = this.mThreadLocker;
  threadLocker.waitUntilCanDraw();
  System.out.println("Draw");
  threadLocker.notifyCanUpdate();

 }

 private class UpdateThread extends Thread {
  private boolean flag; // 线程执行标志位

  public UpdateThread() {
   super("UpdateThread");
   this.flag = true;
  }

  @Override
  public void run() {
   try {
    while (flag) {
     onUpdate();
    }
   } catch (final InterruptedException e) {
    this.interrupt();
    e.printStackTrace();
   }
  }

  /*
   * 停止线程
   */
  public void requestStop() {
   flag = false;
  }
 }

 private class DrawThread extends Thread {
  private boolean flag; // 线程执行标志位

  public DrawThread() {
   super("DrawThread");
   this.flag = true;
  }

  @Override
  public void run() {
   try {
    while (flag) {
     onDrawFrame();
    }
   } catch (final InterruptedException e) {
    this.interrupt();
    e.printStackTrace();
   }
  }

  /*
   * 停止线程
   */
  public void requestStop() {
   flag = false;

  }
 }

 private static class State {
  boolean mDrawing = false;

  public synchronized void notifyCanDraw() {
   this.mDrawing = true;
   this.notifyAll();
  }

  public synchronized void notifyCanUpdate() {
   this.mDrawing = false;
   this.notifyAll();
  }

  public synchronized void waitUntilCanDraw() throws InterruptedException {
   while (this.mDrawing == false) {
    this.wait();
   }
  }

  public synchronized void waitUntilCanUpdate()
    throws InterruptedException {
   while (this.mDrawing == true) {
    this.wait();
   }
  }
 }

 @Override
 protected void onStart() {
  super.onStart();
 }

 @Override
 protected void onResume() {
  super.onResume();
 }

 @Override
 protected void onPause() {
  super.onPause();
 }

 @Override
 protected void onStop() {
  super.onStop();
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  updateThread.requestStop();
  drawThread.requestStop();
 }

 @Override
 public String toString() {
  return "MultiThreadAndSync";
 }


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
java/j2ee iis7站长之家
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3