09-15 Camera (攝像機),SoundPool(播放提示音等小音頻),VideoView(播放視頻),SurfaceView(播放視頻),MediaRecorder(錄製音頻)

Camera(攝像機)

//**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;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章