09-15 Camera (摄像机),SoundPool(播放提示音等小音频),VideoView(播放视频),SurfaceView(播放视频),MediaRecorder(录制音频)
2015-09-15 21:59
567 查看
Camera(摄像机)
SoundPool(播放提示音等小音频)
VideoView(播放视频)
SurfaceView(播放视频)
MediaRecorder(录制音频)
//**activitymian.xml** LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <Button android:id="@+id/button_camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="摄像机"/> <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> //**权限** <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> //**MainActivity** public class MainActivity extends AppCompatActivity { private Button mButtonCamera; private ImageView mImageView; private File file; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView= (ImageView) findViewById(R.id.imageview); mButtonCamera= (Button) findViewById(R.id.button_camera); mButtonCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); startActivityForResult(intent,1231); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==1231){ if(resultCode==RESULT_OK){ mImageView.setImageURI(Uri.fromFile(file)); } } } }
SoundPool(播放提示音等小音频)
//**activity_main.xml** RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">ti <Button android:id="@+id/button_soundpool" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提示音"/> </RelativeLayout> //**MainActivity** public class MainActivity extends AppCompatActivity { private Button mButtonSoundPool; private int VoiceId; private SoundPool soundPool=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonSoundPool= (Button) findViewById(button_soundpool); VoiceId=initSoundPool(); mButtonSoundPool.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { playSoundPool(); } }); } private int initSoundPool(){ if(Build.VERSION.SDK_INT<21){ soundPool=new SoundPool(2, AudioManager.STREAM_MUSIC,0); }else{ SoundPool.Builder builder=new SoundPool.Builder(); builder.setMaxStreams(2); AudioAttributes.Builder attributes=new AudioAttributes.Builder(); attributes.setLegacyStreamType(AudioManager.STREAM_MUSIC); builder.setAudioAttributes(attributes.build()); soundPool=builder.build(); } return soundPool.load(getApplicationContext(),R.raw.outgoing,1); } public void playSoundPool(){ soundPool.play(VoiceId,1,1,0,-1,1); } }
VideoView(播放视频)
//**activity_main.xml** <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <Button android:id="@+id/button_videoview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放"/> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1"/> </LinearLayout> //**MainActivity** public class MainActivity extends AppCompatActivity { private Button mButtonStart; private VideoView videoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoView= (VideoView) findViewById(R.id.videoView); mButtonStart= (Button) findViewById(R.id.button_videoview); mButtonStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { videoView.setVideoPath(Environment.getExternalStorageDirectory()+"/aa.mp4"); videoView.setMediaController(new MediaController(MainActivity.this)); videoView.start(); } }); } }
SurfaceView(播放视频)
//**activity_main.xml** <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <Button android:id="@+id/button_startSurface" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始播放"/> <Button android:id="@+id/button_endSurface" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止播放"/> <SurfaceView android:id="@+id/surface" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> //**权限** <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> //**MianActivity** public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button mButtonStart; private Button mButtonEnd; private MediaPlayer mediaPlayer; private SurfaceView surfaceView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonStart= (Button) findViewById(R.id.button_startSurface); mButtonStart.setOnClickListener(this); mButtonEnd= (Button) findViewById(R.id.button_endSurface); mButtonEnd.setOnClickListener(this); surfaceView= (SurfaceView) findViewById(R.id.surface); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button_startSurface: try { mediaPlayer=new MediaPlayer(); mediaPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/aa.mp4"); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDisplay(surfaceView.getHolder()); mediaPlayer.prepare(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.start(); } }); } catch (IOException e) { e.printStackTrace(); } break; case R.id.button_endSurface: mediaPlayer.stop(); mediaPlayer.reset(); mediaPlayer.release(); break; default: break; } } }
MediaRecorder(录制音频)
//**activity_main.xml** <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/button_startRecorder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="录制开始"/> <Button android:id="@+id/button_endRecorder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="录制结束"/> </LinearLayout> //**权限** <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> //**MainActivity** public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button mButtonStartRecord; private Button mButtonEndRecord; private MediaRecorder mediaRecorder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonStartRecord= (Button) findViewById(R.id.button_startRecorder); mButtonStartRecord.setOnClickListener(this); mButtonEndRecord= (Button) findViewById(R.id.button_endRecorder); mButtonEndRecord.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button_startRecorder: mediaRecorder=new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory()+"/myrecord.3gp"); try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } break; case R.id.button_endRecorder: mediaRecorder.stop(); mediaRecorder.reset(); mediaRecorder.release(); break; default: break; } } }
相关文章推荐
- Web APi入门之移除XML格式(一)
- Ubuntu配置jdk
- cocos2d-x 打开控制面板
- 让你不在害怕指针
- C++中使用clock()函数测试程序时间
- Javascript DOM
- linux安装jdk
- 华为OJ题7---挑7
- 从HelloWorld启航——数据库连接字符串的困惑
- C结构体数组赋值
- hadoop三个配置文件的参数含义说明core-site.xml,hdfs-site.xml,mapred-site.xml
- python 闭包中引用的变量值变更问题
- 递归调用
- C/C++ linux下光标定位和清屏函数(printf 实现)
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
- opencv基础mat类详解
- 学习java的道路有点难走啊
- Android多媒体---VideoView(播放视频)
- 【设计模式】Java 23种设计模式对比总结
- Java堆栈和程序计时器点滴