Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within...
-
Upload
daniela-malone -
Category
Documents
-
view
218 -
download
1
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.](https://reader036.fdocuments.in/reader036/viewer/2022071807/56649dcf5503460f94ac4163/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071807/56649dcf5503460f94ac4163/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071807/56649dcf5503460f94ac4163/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071807/56649dcf5503460f94ac4163/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071807/56649dcf5503460f94ac4163/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071807/56649dcf5503460f94ac4163/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071807/56649dcf5503460f94ac4163/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071807/56649dcf5503460f94ac4163/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071807/56649dcf5503460f94ac4163/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071807/56649dcf5503460f94ac4163/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071807/56649dcf5503460f94ac4163/html5/thumbnails/11.jpg)
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