Multimedia

Audio and Video

Audio

  • Android support lots of common audio format

  • MPEG-4 (.mp4, .m4a)

  • MP3 (.mp3)

  • Ogg (.ogg)

  • WAVE (.wav)

  • Matroska (.mkv)

Vedio

  • H264                                                      

    3GPP (.3gp)

    MPEG-4 (.mp4) 

  • H265
    MPEG-4 (.mp4)

  • VP8

  • VP9
    WebM (.webm)

MediaPlayer

  • MediaPlayer class can be used to control playback of audio/video files and streams. 

MediaPlayer

  • MediaPlayer class can be used to control playback of audio/video files and streams. 

MediaPlayer

  • Idle

  • Initialized

  • Preparing

  • Prepared

  • Started

  • Paused

  • Playback Completed                          

  • Stopped

  • End

MediaPlayer(Audio)

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.example);
mediaPlayer.setDataSource(uri);
// the uri can be system file or by http, rtsp protocal
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.pause();
mediaPlayer.start();
mediaPlayer.stop();
mediaPlayer.reset();
// If going to stop stage, must reset to go to preparing
mediaPlayer.release();

MediaPlayer(Vedio)

  • Most same as play audio

  • Need a SurfaceView to show video

MediaPlayer mp = MediaPlayer.create(this, R.raw.ex);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
// holder is a class to deal with screen
mp.setDisplay(surfaceHolder);

Advance-Audio

  • AudioTrack provides a much more direct method of playing audio

  • Can control the PCM code

  • frequence, channel, samplebit

AudioTrack

  • Mode

  • static

  • streaming                                             

AudioTrack

// AudioTrack(int streamType, int sampleRateInHz, 
//            int channelConfig, int audioFormat, 
//            int bufferSizeInBytes, int mode)
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,  
                            frequency,   
                            Channel,   
                            SampBit,   
                            minBufSize,  
                            AudioTrack.MODE_STREAM);  

Advance-Video

  • VideoView is subclass of serfaceView

  • Can use mediaController directly

Advance-Video

// create the view (in this case it is already included in the layout resource)
VideoView videoview = (VideoView) findViewById(R.id.videoview);
videoview.setKeepScreenOn(true);
// used if streaming
if (videouri != null) videoview.setVideoURI(videouri);
// absolute path if it is a file
else videoview.setVideoPath(videopath);
// let's add a media control so we can control the playback
mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(videoview);
videoview.setMediaController(mediacontroller);
if (videoview.canSeekForward())
videoview.seekTo(videoview.getDuration()/2);
// start the playback

Media recoding

<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.CAMERA"/>

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

Audio Recording

  • MediaRecorder

  • AudioRecorder                        

MediaRecorder

// initialize the MediaRecorder
MediaRecorder mediarecorder = new MediaRecorder();
// configure the data source
// the source of the audio input
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// output format
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// encoding
mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// use absolute path to file where output is stored
mediarecorder.setOutputFile("/sdcard/audiorecordexample.3gpp");
// prepare to record
mediarecorder.prepare();
Then when the recoding needs to
mediarecorder.start();
mediarecorder.pause();
mediarecorder.start();
mediarecorder.stop();
mediarecorder.release();

AudioRecorder

short[] buffer = new short[10000];
recorder = new AudioRecord( // source to record from
MediaRecorder.AudioSource.MIC,
11025,
// channel config—mono, stereo, etc.
AudioFormat.CHANNEL_CONFIGURATION_MONO,
// audio encoding
AudioFormat.ENCODING_PCM_16BIT,
// buffer size
buffer.length
);
recorder.startRecording();
while(recordablestate) {
    try {
    // read in up to buffer size
    int readBytes = recorder.read(buffer, 0, buffer.length);
    // do something with the bytes that are read
    } catch (Exception t) {
        recordablestate = false;
    }
}

Video Recording

  • MediaRecorder only                  

Video Recording

// initialize
MediaRecorder mediarecorder = new MediaRecorder();
// set data source
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediarecorder.setOutputFile("/sdcard/someexamplevideo.mp4");
// provide a surface to show the preview in. in this case a VideoView 
is used
videoview = (VideoView) findViewById(R.id.videosurface);
SurfaceHolder holder = videoview.getHolder();
mediarecorder.setPreviewDisplay(holder.getSurface());
// prepare
mediarecorder.prepare();
// start recording
mediarecorder.start();

MediaStore

  • MediaStore is a content provider to provide media source                     

  • Android will scan the media file to MediaStore when umount or mount sdcard

  • However when we record something, we need to add MediaStore that can be used by other application

MediaStore

ContentValues content = new ContentValues();
// VERY IMPORTANT! Must reference the absolute path of the data.
content.put(MediaStore.MediaColumns.DATA, "/sdcard/AudioExample.3gpp");
content.put(MediaStore.MediaColumns.TITLE, "AudioRecordExample");
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/amr");
content.put(MediaStore.Audio.Media.ARTIST, "Me");
content.put(MediaStore.Audio.Media.IS_MUSIC, true);
// get the Content Resolver
ContentResolver resolve = getContentResolver();
// insert into the content resolver
Uri uri = resolve.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, content);
// announce to everyone that cares that it was inserted
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));

SwipeRefreshLayout

  • A Layout that can display refresh animation                 

SwipeRefreshLayout

  • Xml
    add SwipeFreshLayout to xml source             

  • java code

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // dosomething
            swipeRefreshLayout.setRefreshing(false);
        }
    });

FloatingActionButton

  • More cool than old button                       

MultiMedia

By zlsh80826

MultiMedia

  • 514