Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within...

11
Basic Audio and Video

Transcript of Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within...

Page 1: Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.

Basic Audio and Video

Page 2: Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.

Audio

Page 3: Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.

Audio

• Primary components– Audio file

• stored in ‘raw’ folder within ‘res’ folder• ‘raw’ directory must be created• while many formats supported, .mp3 is most stable across

devices

– Instance of MediaPlayer• android.media.MediaPlayer• key methods

– create» static convenience method for instantiation and preparation

– start

Page 4: Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.

Sample code - MediaPlayer

MediaPlayer audioPlayer;audioPlayer = MediaPlayer.create(this, R.raw.audio_file_name);//no extension on file nameaudioPlayer.setAudioStream(AudioManager.STREAM_MUSIC);audioPlayer.start();

Page 5: Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.

MediaPlayer – coding issues

• Other information– audioPlayer.stop(), pause(), and resume()

• audio does not automatically stop if activity pauses• only if application stops

– audioPlayer.release() should be called when done• MediaPlayers can be expensive• frees resources

– Can use a BroadcastReceiver to allow hardware control (e.g. play, pause, skip, etc. on headset)

– Can use Service to continue playback when App stops

Page 6: Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.

MediaPlayer – coding issues

• One of the prepare() methods must be called– prepare() method

• called automatically by create() method• OK if using a file

– prepareAsync () method• cannot use create() convenience method• starts MediaPlayer in a new thread• better for streaming audio• app does not ‘hang’ if waiting for buffered data

Page 7: Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.

Video

Page 8: Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.

Video

• Primary components– Video file• .3gp file

– new standard supported by 3G phones

• can be stored in res/raw folder– don’t supply extension in call

• can be stored within device’s file system (i.e. SD card)

– VideoView widget• typical widget in xml layout file• key methods

– setVideoPath (full path and file name with extension)– start

Page 9: Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.

Sample code - VideoView

//Video stored on SD CardVideoView vv = (VideoView)findViewById(R.id.vvVideo);vv.setVideoPath("/sdcard/filename.3gp");vv.start();

or:

//Video stored in raw directoryVideoView vv = (VideoView)findViewById(R.id.vvVideo);vv.setVideoPath("android.resource://edu.csci153/raw/filename"); vv.start();

Page 10: Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.

Internal file system

Emulator or attached device

Page 11: Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.

Internal file system

• Display files within eclipse– Window…Show View…Other…File Explorer• avoid ‘data’ directory

– emulator allows access– system directory – restricted access on an actual device

• Copy files to/from device within eclipse– icons in File Explorer• copy to: ‘Push a file onto the device’ icon• copy from: ‘Pull a file from the device’ icon• delete from device: ‘Delete the selection’ icon