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

Conversation

frankieandone
Copy link
Contributor

Test Case:
Expect other audio sources to stop when start/resume button is clicked. You will require another music app running in the background and a sample.mp3.

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 
// background.
var audioPlayer = Ti.Media.createAudioPlayer({
	url: 'www.example.com/podcast.mp3',
	audioFocus: true,
	allowBackground: true
});

startStopButton.addEventListener('click', function () {
	// When paused, playing returns false.
	// If both are false, playback is stopped.
	if (audioPlayer.playing || audioPlayer.paused) {
		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();
	}
	else {
		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();
	}
});

win.open();

NOTE: Ticket asks to have audioFocus as a property however I think this should be default behavior and not a property. Let me know if we want to change this.
JIRA: https://jira.appcelerator.org/browse/TIMOB-23455

Copy link
Contributor

@antw12 antw12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CR & FR PASS 👍

Copy link
Contributor

@ssjsamir ssjsamir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FR Passed, was able to to only focus on the audio in the inside the application when pressing start or resume if the audio had been playing from a different application.

The audio file used in the application can be found at http://www.mfiles.co.uk/mp3-downloads/frederic-chopin-piano-sonata-2-op35-3-funeral-march.mp3 (Code for where link was usedis shown below)

var audioPlayer = Ti.Media.createAudioPlayer({
	url: 'http://www.mfiles.co.uk/mp3-downloads/frederic-chopin-piano-sonata-2-op35-3-funeral-march.mp3',
	audioFocus: true,
	allowBackground: true
});

Test steps

  • Copied the code in the test description in to the app.js of a titanium project
  • Changed
var audioPlayer = Ti.Media.createAudioPlayer({
	url: 'www.example.com/podcast.mp3',
	audioFocus: true,
	allowBackground: true
});

to

var audioPlayer = Ti.Media.createAudioPlayer({
	url: 'http://www.mfiles.co.uk/mp3-downloads/frederic-chopin-piano-sonata-2-op35-3-funeral-march.mp3',
	audioFocus: true,
	allowBackground: true
});
  • Ran the program on an android device (Nexus 6P)
  • Put the app in the background and opened a 3rd party application (Spotify) and played a random song
  • Put Spotify in the background so the song continued to play
  • Opened the original titanium application and pressed start
  • Saw that music from Spotify stop and the mp3 file from the titanium application start to play
  • Pressed the pause button
  • Switched back to Spotify and resumed the song
  • Switched back to the titanium application and pressed resume
  • Once again saw the song from Spotify stop and then saw the mp3 from the titanium application resume

Environement
Appcelerator Command-Line Interface, version 6.1.0
Google Nexus 6P (Android 7.1.1)
Operating System Name: Mac OS X El Capitan
Operating System Version: 10.11.6
Node.js Version: 4.6.0
npm: 4.2.8
Xcode: 8.2
Appcelerator Studio: 4.8.1.201612050850

@ssjsamir ssjsamir merged commit 5d494aa into tidev:master Mar 29, 2017
@frankieandone frankieandone deleted the timob-23455 branch April 5, 2017 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants