mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-31 17:32:51 +08:00
added audio output setting for android
This commit is contained in:
parent
c96e369351
commit
61078ac946
@ -14,6 +14,7 @@
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_SMS" />
|
||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
|
||||
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name"
|
||||
android:debuggable="true">
|
||||
|
@ -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: {
|
||||
|
@ -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);
|
||||
<a class="button blueButton" type="submit" onclick="audio('getCurrentPosition');">getPosition</a>
|
||||
</div>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
<a class="button leftButton" type="submit" onclick="audio('setEarpiece');">Earpiece</a>
|
||||
<a class="button blueButton" type="submit" onclick="audio('setSpeaker');">Speaker</a>
|
||||
</div>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
<a class="button leftButton" type="submit" onclick="audio('getAudioOutputDevice');">Audio Output</a>
|
||||
<input disabled="enabled" id="audoutput" type="text" style="width: 30ex"/>
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
<div id="pi" title="Phone Information" class="panel">
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user