120 likes | 282 Views
Basic Audio and Video. Audio. 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
E N D
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
Sample code - MediaPlayer MediaPlayeraudioPlayer; audioPlayer = MediaPlayer.create(this, R.raw.audio_file_name); //no extension on file name audioPlayer.setAudioStream(AudioManager.STREAM_MUSIC); audioPlayer.start();
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
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
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
Sample code - VideoView //Video stored on SD Card VideoView vv = (VideoView)findViewById(R.id.vvVideo); vv.setVideoPath("/sdcard/filename.3gp"); vv.start(); or: //Video stored in raw directory VideoViewvv = (VideoView)findViewById(R.id.vvVideo); vv.setVideoPath("android.resource://edu.csci153/raw/filename"); vv.start();
Internal file system Emulator or attached device
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