diff --git a/AndroidManifest.xml b/AndroidManifest.xml index a25a88c0..2d75fb20 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -14,6 +14,7 @@ + diff --git a/assets/gap.js b/assets/gap.js index 778209a9..f6f147ce 100644 --- a/assets/gap.js +++ b/assets/gap.js @@ -351,6 +351,12 @@ var Device = { }, getDuration: function(file) { return window.DroidGap.getDurationAudio(file); + }, + setAudioOutputDevice: function(output){ + window.DroidGap.setAudioOutputDevice(output); + }, + getAudioOutputDevice: function (){ + return window.DroidGap.getAudioOutputDevice(); } }, information: { diff --git a/assets/index.html b/assets/index.html index 1e35cb50..456cea0a 100644 --- a/assets/index.html +++ b/assets/index.html @@ -146,7 +146,17 @@ audio = function(func) } else if (func == 'getDuration') { $('posdur').value = Device.audio.getDuration($('audioFile').value); - } + }else + if (func == 'setEarpiece') { + Device.audio.setAudioOutputDevice(func); + } else + if (func == 'setSpeaker') { + Device.audio.setAudioOutputDevice(func); + }else + if (func == 'getAudioOutputDevice') { + $('audoutput').value = Device.audio.getAudioOutputDevice(); + } + } phInformation = function(){ @@ -400,6 +410,18 @@ addLoadEvent(initGap); getPosition +
+ +
+
+
+ Audio Output + +
+
diff --git a/src/com/nitobi/phonegap/AudioHandler.java b/src/com/nitobi/phonegap/AudioHandler.java index 7a2934bf..7ff2953a 100644 --- a/src/com/nitobi/phonegap/AudioHandler.java +++ b/src/com/nitobi/phonegap/AudioHandler.java @@ -2,6 +2,8 @@ package com.nitobi.phonegap; import java.io.File; +import android.content.Context; +import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.media.MediaPlayer.OnCompletionListener; @@ -13,9 +15,11 @@ public class AudioHandler implements OnCompletionListener { private boolean isPlaying = false; private String recording; private String saveFile; + private Context mCtx; - public AudioHandler(String file) { + public AudioHandler(String file, Context ctx) { this.recording = file; + this.mCtx = ctx; } public void startRecording(String file){ @@ -99,4 +103,25 @@ public class AudioHandler implements OnCompletionListener { return duration; } else { return -1; } } + + protected void setAudioOutputDevice(String output){ + System.out.println ("Change audio setting to be "+output); + AudioManager audiMgr = (AudioManager) mCtx.getSystemService(Context.AUDIO_SERVICE); + if (output.contains("Speaker")) + audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL); + else if (output.contains("Earpiece")){ + audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); + }else + System.out.println("input error"); + + } + protected int getAudioOutputDevice(){ + AudioManager audiMgr = (AudioManager) mCtx.getSystemService(Context.AUDIO_SERVICE); + if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_EARPIECE) + return 1; + else if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_SPEAKER) + return 2; + else + return -1; + } } diff --git a/src/com/nitobi/phonegap/PhoneGap.java b/src/com/nitobi/phonegap/PhoneGap.java index d5e72fba..c8657be9 100644 --- a/src/com/nitobi/phonegap/PhoneGap.java +++ b/src/com/nitobi/phonegap/PhoneGap.java @@ -51,6 +51,7 @@ public class PhoneGap{ protected LocationProvider provider; SmsListener mSmsListener; DirectoryManager fileManager; + AudioHandler audio; public PhoneGap(Context ctx, Handler handler, WebView appView) { this.mCtx = ctx; @@ -60,6 +61,7 @@ public class PhoneGap{ mNetwork = new NetworkListener(ctx); mSmsListener = new SmsListener(ctx,mAppView); fileManager = new DirectoryManager(); + audio = new AudioHandler("/sdcard/tmprecording.mp3", ctx); } public void updateAccel(){ @@ -285,7 +287,7 @@ public class PhoneGap{ * AUDIO * TODO: Basic functions done but needs more work on error handling and call backs, remove record hack */ - AudioHandler audio = new AudioHandler("/sdcard/tmprecording.mp3"); + public void startRecordingAudio(String file) { /* for this to work the recording needs to be specified in the constructor, @@ -321,6 +323,13 @@ public class PhoneGap{ return(audio.getDuration(file)); } + public void setAudioOutputDevice(String output){ + audio.setAudioOutputDevice(output); + } + public int getAudioOutputDevice(){ + return audio.getAudioOutputDevice(); + } + public String getLine1Number() { TelephonyManager tm = (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);