Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-23455] Android: expose audio focus #8896

Merged
merged 1 commit into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public void handleCreationDict(KrollDict options) {
if (options.containsKey(TiC.PROPERTY_ALLOW_BACKGROUND)) {
setProperty(TiC.PROPERTY_ALLOW_BACKGROUND, options.get(TiC.PROPERTY_ALLOW_BACKGROUND));
}
if (options.containsKey(TiC.PROPERTY_AUDIO_FOCUS)) {
boolean audioFocus = TiConvert.toBoolean(options.get(TiC.PROPERTY_AUDIO_FOCUS));
setProperty(TiC.PROPERTY_AUDIO_FOCUS, audioFocus);
TiSound.audioFocus = audioFocus;
}
Log.i(TAG, "Creating audio player proxy for url: " + TiConvert.toString(getProperty(TiC.PROPERTY_URL)),
Log.DEBUG_MODE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public class TiSound
private boolean stopPending = false;
private boolean playPending = false;
private boolean prepareRequired = false;

public static boolean audioFocus;

public TiSound(KrollProxy proxy)
{
this.proxy = proxy;
Expand Down Expand Up @@ -582,6 +583,7 @@ public void onResume()
{
if (mp != null) {
if (playOnResume) {
requestAudioFocus(audioFocus);
play();
playOnResume = false;
}
Expand Down Expand Up @@ -630,6 +632,7 @@ private void startPlaying()
paused = false;
startProgressTimer();
}
requestAudioFocus(audioFocus);
setState(STATE_PLAYING);
}
}
Expand All @@ -655,4 +658,10 @@ public void onPrepared(MediaPlayer mp)
pausePending = false;
stopPending = false;
}

private boolean requestAudioFocus(boolean focus) {
if (!focus) return false;
AudioManager audioManager = (AudioManager) TiApplication.getInstance().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
return audioManager != null && audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}
}
5 changes: 5 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,11 @@ public class TiC
*/
public static final String PROPERTY_AUDIO_STREAM_TYPE = "audioStreamType";

/**
* @module.api
*/
public static final String PROPERTY_AUDIO_FOCUS = "audioFocus";

/**
* @module.api
*/
Expand Down
67 changes: 38 additions & 29 deletions apidoc/Titanium/Media/AudioPlayer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name: Titanium.Media.AudioPlayer
summary: |
An audio player object used for streaming audio to the device, and low-level control of the audio playback.
description: |
On Android, when you are done playing a given audio file, you must call the
[release](Titanium.Media.AudioPlayer.release) method to stop buffering audio data and
On Android, when you are done playing a given audio file, you must call the
[release](Titanium.Media.AudioPlayer.release) method to stop buffering audio data and
release associated system resources.

On iOS, you can control how the audio stream interacts with other system sounds
Expand All @@ -14,43 +14,43 @@ description: |
extends: Titanium.Proxy
since: "0.9"
platforms: [android, iphone, ipad]
examples:
examples:
- title: Audio Streaming
example: |
The following example demonstrates using the `AudioPlayer` object to stream audio.

var win = Titanium.UI.createWindow({
var win = Titanium.UI.createWindow({
title:'Audio Test',
backgroundColor:'#fff',
layout: 'vertical'
});

var startStopButton = Titanium.UI.createButton({
title:'Start/Stop Streaming',
top:10,
width:200,
height:40
});

var pauseResumeButton = Titanium.UI.createButton({
title:'Pause/Resume Streaming',
top:10,
width:200,
height:40,
enabled:false
});

win.add(startStopButton);
win.add(pauseResumeButton);
// allowBackground: true on Android allows the
// player to keep playing when the app is in the

// allowBackground: true on Android allows the
// player to keep playing when the app is in the
// background.
var audioPlayer = Ti.Media.createAudioPlayer({
var audioPlayer = Ti.Media.createAudioPlayer({
url: 'www.example.com/podcast.mp3',
allowBackground: true
});
});

startStopButton.addEventListener('click',function() {
// When paused, playing returns false.
// If both are false, playback is stopped.
Expand All @@ -59,17 +59,17 @@ examples:
audioPlayer.stop();
pauseResumeButton.enabled = false;
if (Ti.Platform.name === 'android')
{
{
audioPlayer.release();
}
}
}
else
{
audioPlayer.start();
pauseResumeButton.enabled = true;
}
});

pauseResumeButton.addEventListener('click', function() {
if (audioPlayer.paused) {
audioPlayer.start();
Expand All @@ -78,20 +78,20 @@ examples:
audioPlayer.pause();
}
});

audioPlayer.addEventListener('progress',function(e) {
Ti.API.info('Time Played: ' + Math.round(e.progress) + ' milliseconds');
});

audioPlayer.addEventListener('change',function(e)
{
Ti.API.info('State: ' + e.description + ' (' + e.state + ')');
});

win.addEventListener('close',function() {
audioPlayer.stop();
if (Ti.Platform.osname === 'android')
{
{
audioPlayer.release();
}
});
Expand All @@ -101,22 +101,22 @@ examples:
methods:
- name: getPaused
summary: Returns the value of the [paused](Titanium.Media.AudioPlayer.paused) property.
returns:
returns:
type: Boolean
platforms: [iphone, ipad]
- name: isPaused
summary: Returns the value of the [paused](Titanium.Media.AudioPlayer.paused) property.
returns:
returns:
type: Boolean
platforms: [android]
- name: getPlaying
summary: Returns the value of the [playing](Titanium.Media.AudioPlayer.playing) property.
returns:
returns:
type: Boolean
platforms: [iphone, ipad]
- name: isPlaying
summary: Returns the value of the [playing](Titanium.Media.AudioPlayer.playing) property.
returns:
returns:
type: Boolean
platforms: [android]
- name: pause
Expand All @@ -138,7 +138,7 @@ methods:
summary: Sets the value of the [paused](Titanium.Media.AudioPlayer.paused) property.
description: |
On iOS, this method can be used to pause and unpause playback. For portability,
it is preferable to use the [pause](Titanium.Media.AudioPlayer.pause) and
it is preferable to use the [pause](Titanium.Media.AudioPlayer.pause) and
[start](Titanium.Media.AudioPlayer.start) methods instead.
parameters:
- name: paused
Expand All @@ -153,7 +153,7 @@ methods:
platforms: [android]
- name: getAudioSessionId
summary: Returns the audio session id.
returns:
returns:
type: Number
since: "5.4.0"
platforms: [android]
Expand All @@ -163,7 +163,7 @@ methods:
summary: |
Converts a [state](Titanium.Media.AudioPlayer.state) value into a text description
suitable for display.
parameters:
parameters:
- name: state
summary: State value to convert.
type: Number
Expand Down Expand Up @@ -274,6 +274,15 @@ properties:
default: false
availability: creation
platforms: [android]
- name: audioFocus
summary: Focuses on the current audio player and stops other audio playing.
description: |
If true then all other audio sources will be stopped
when Titanium.AudioPlayer is started or resumed.
type: Boolean
default: false
availability: creation
platforms: [android]
- name: bitRate
summary: Bit rate of the current playback stream.
type: Number
Expand All @@ -289,7 +298,7 @@ properties:
- name: idle
summary: Boolean indicating if the player is idle.
description: |
`true` if the player is in the initialized state: that is, not playing, paused,
`true` if the player is in the initialized state: that is, not playing, paused,
or waiting for data.
type: Boolean
permission: read-only
Expand All @@ -304,7 +313,7 @@ properties:
type: Boolean
permission: read-only
- name: progress
summary: Current playback progress, in milliseconds.
summary: Current playback progress, in milliseconds.
description: |
Returns zero if `bitRate` has not yet been detected.
type: Number
Expand Down