From 61078ac9467534224ad60dd7b31438f755938b85 Mon Sep 17 00:00:00 2001 From: addios Date: Mon, 2 Mar 2009 11:56:49 +0700 Subject: [PATCH 1/2] added audio output setting for android --- AndroidManifest.xml | 1 + assets/gap.js | 6 +++++ assets/index.html | 24 +++++++++++++++++++- src/com/nitobi/phonegap/AudioHandler.java | 27 ++++++++++++++++++++++- src/com/nitobi/phonegap/PhoneGap.java | 11 ++++++++- 5 files changed, 66 insertions(+), 3 deletions(-) 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); From e5934f6c582a809a3e194dd0ec6ee924e1c0adeb Mon Sep 17 00:00:00 2001 From: addios Date: Thu, 5 Mar 2009 15:31:58 +0700 Subject: [PATCH 2/2] Added comment about Directory management in Phonegap.java and changed Java Call backs to be int --- src/com/nitobi/phonegap/PhoneGap.java | 56 +++++++++++++++++++-------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/src/com/nitobi/phonegap/PhoneGap.java b/src/com/nitobi/phonegap/PhoneGap.java index c8657be9..906642f4 100644 --- a/src/com/nitobi/phonegap/PhoneGap.java +++ b/src/com/nitobi/phonegap/PhoneGap.java @@ -250,35 +250,59 @@ public class PhoneGap{ http.get(url, file); } - public String testSaveLocationExists(){ + /** + * Check if SD card is ready and already mounted + * TODO: JavaScript Call backs for success and error handling + */ + public int testSaveLocationExists(){ if (fileManager.testSaveLocationExists()) - return "SD Card available"; + return 0; else - return "SD Card unavailable"; + return 1; } - public String testDirOrFileExists(String file){ + + /** + * Check if a specific directory of file exists + * TODO: JavaScript Call backs for success and error handling + */ + public int testDirOrFileExists(String file){ if (fileManager.isDirtoryOrFileExists(file)) - return "File exists"; + return 0; else - return "No this file"; + return 1; } - public String deleteDirectory (String dir){ + /** + * Delete a specific directory. + * Everyting in side the directory would be gone. + * TODO: JavaScript Call backs for success and error handling + */ + public int deleteDirectory (String dir){ if (fileManager.deleteDir(dir)) - return "Completed"; + return 0; else - return "Not completed"; + return 1; } - public String deleteFile (String file){ + + /** + * Delete a specific file. + * TODO: JavaScript Call backs for success and error handling + */ + public int deleteFile (String file){ if (fileManager.deleteFile(file)) - return "Completed"; + return 0; else - return "Not completed"; + return 1; } - public String createDirectory(String dir){ - if (fileManager.createDirectory(dir)) - return "Completed"; + + /** + * Create a new directory. + * TODO: JavaScript Call backs for success and error handling + */ + public int createDirectory(String dir){ + if (fileManager.createDirectory(dir)) + return 0; else - return "Not completed"; + return 1; }