Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
App Development for Smart Devices CS 495/595 - Fall 2013 Lec #7: Audio, Video & Telephony Try It Out Tamer Nadeem Dept. of Computer Science Trt It Out • Example - SoundPool • Example - VideoView Page 2 Fall 2013 CS 495/595 - App Development for Smart Devices SoundPool • Android provides two API's for playing sounds: SoundPool and MediaPlayer. • SoundPool can be used for small audio clips. It can repeat sounds and play several sounds simultaneously. The sound files played with SoundPool should not exceed 1 MB. • SoundPool does load the file asynchronously. In recent Android SDK, it is possible to check if the loading is complete via OnLoadCompleteListener. • Mediaplayer is better suited for longer music and movies. Page 3 Fall 2013 CS 495/595 - App Development for Smart Devices Example of SoundPool • Create an application that will start playing a sound once the finger touches the display. Create an Android project “cs.edu.odu.cs495.soundpool" with the Activity "PlaySound“ • Get a free sound effect from http://hamsterrepublic.com/ohrrpgce/Free_Sound_Effects.html, put it into your "res/raw" folder under the name "sound1.ogg". main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="Click on the screen to start playing" android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="fill_parent"> </TextView> </LinearLayout> Page 4 Fall 2013 CS 495/595 - App Development for Smart Devices Example of SoundPool package edu.odu.cs.cs495.soundpool; import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.media.SoundPool.OnLoadCompleteListener; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class PlaySound extends Activity implements OnTouchListener { private SoundPool soundPool; private int soundID; boolean loaded = false; Page 5 Fall 2013 CS 495/595 - App Development for Smart Devices Example of SoundPool /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View view = findViewById(R.id.textView1); view.setOnTouchListener(this); // Set the hardware buttons to control the music this.setVolumeControlStream(AudioManager.STREAM_MUSIC); // Load the sound soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { loaded = true; } }); soundID = soundPool.load(this, R.raw.sound1, 1); } Page 6 Fall 2013 CS 495/595 - App Development for Smart Devices Example of SoundPool @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // Getting the user sound settings AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); float actualVolume = (float) audioManager. getStreamVolume(AudioManager.STREAM_MUSIC); float maxVolume = (float) audioManager. getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = actualVolume / maxVolume; // Is the sound loaded already? if (loaded) { soundPool.play(soundID, volume, volume, 1, 0, 1f); Log.e("Test", "Played sound"); } } return false; } } Page 7 Fall 2013 CS 495/595 - App Development for Smart Devices Example of VideoView • A simple example using VideoView to play 3gp from YouTube main.xml <?xml version="1.0" encoding="utf-‐8"?> <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <VideoView android:id="@+id/myvideoview" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> Page 8 Fall 2013 CS 495/595 - App Development for Smart Devices Example of VideoView package edu.odu.cs.cs495.MyVideoView; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.widget.MediaController; import android.widget.VideoView; public class MyVideoView extends Activity { String SrcPath = "rtsp://<REPLACE WITH PATH TO YouTube video (3gp)>"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview); myVideoView.setVideoURI(Uri.parse(SrcPath)); myVideoView.setMediaController(new MediaController(this)); myVideoView.requestFocus(); myVideoView.start(); } } Page 9 Fall 2013 CS 495/595 - App Development for Smart Devices