Skip to content

Instantly share code, notes, and snippets.

@manumaticx
Last active March 15, 2017 18:43
Show Gist options
  • Save manumaticx/5894573 to your computer and use it in GitHub Desktop.
Save manumaticx/5894573 to your computer and use it in GitHub Desktop.
modified sample code from http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Media.AudioPlayer related to this question: http://developer.appcelerator.com/question/154232/how-to-check-if-audio-is-playing Also added an intent filter to launch Activity from an url while the stream is playing in background. See this question: http://devel…
var win = Titanium.UI.createWindow({
title:'Audio Test',
backgroundColor:'#fff',
layout: 'vertical'
});
var startStopButton = Titanium.UI.createButton({
title:'Start/Stop Streaming',
top:'10dp',
width:'200dp',
height:'40dp'
});
var pauseResumeButton = Titanium.UI.createButton({
title:'Pause/Resume Streaming',
top:'10dp',
width:'200dp',
height:'40dp',
enabled:false
});
var stateLabel = Ti.UI.createLabel({
text: 'state',
font: {
fontSize: '18sp',
fontWeight: 'bold'
}
});
var volumeBar = Ti.UI.createSlider({
min: 0,
max: 1,
value: 0.5
});
volumeBar.addEventListener('change', function(e){
audioPlayer.setVolume(e.value);
});
win.add(startStopButton);
win.add(pauseResumeButton);
win.add(stateLabel);
win.add(volumeBar);
// allowBackground: true on Android allows the
// player to keep playing when the app is in the
// background.
var audioPlayer = Ti.Media.createAudioPlayer({
url: 'https://www.example.com/podcast.mp3',
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('change', function(e){
switch (e.state){
case audioPlayer.STATE_BUFFERING:
stateLabel.setText('Audio data is being buffered from the network.'); break;
case audioPlayer.STATE_STARTING:
stateLabel.setText('Audio playback is starting.'); break;
case audioPlayer.STATE_PLAYING:
stateLabel.setText('Audio playback is active.'); break;
case audioPlayer.STATE_STOPPED:
stateLabel.setText('Audio playback is stopped.'); break;
case audioPlayer.STATE_PAUSED:
stateLabel.setText('Audio playback is paused.'); break;
default: stateLabel.setText(e.state);
}
});
win.addEventListener('close',function() {
audioPlayer.stop();
if (Ti.Platform.osname === 'android')
{
audioPlayer.release();
}
});
var activity = Ti.Android.currentActivity;
activity.addEventListener('newIntent', function(e){
var uri = e.intent.getData();
stateLabel.setText('launch url : '+uri);
});
win.open();
<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
<supports-screens android:anyDensity="true"/>
<uses-sdk android:maxSdkVersion="17"
android:minSdkVersion="8" android:targetSdkVersion="17"/>
<application android:theme="@android:style/Theme.Holo.Light" >
<activity android:launchMode="singleTask" android:configChanges="keyboardHidden|orientation" android:label="Test" android:name=".TestActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="http" android:host="www.appcelerator.com"/>
<data android:scheme="http" android:host="appcelerator.com"/>
</intent-filter>
</activity>
</application>
</manifest>
</android>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment