Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

how to play streaming sound when i click on button in notification

$
0
0

I'm trying develope a streaming music player so to allow playing music when app is running in background i made a costom notification with play and pause button the problem is when i have to press play or pause button when app is open to play or pause sound and if i close the app and press play or pause button it only open the app so i want when i press the play/pasue button in notification to only play or pause sound without opening app

here is my code so far for custom notification

index.js
Titanium.Media.audioSessionMode = Titanium.Media.AUDIO_SESSION_MODE_PLAYBACK;
Titanium.App.idleTimerDisabled = true;
// Create an AudioPlayer.
var duration = 8000;
var sec = "0",
    min = "0",
    hour = "0";
var anAudioPlayer = Ti.Media.createAudioPlayer({
    url : 'http://www.mediacollege.com/downloads/sound-effects/nature/forest/jungle-04.wav',
    allowBackground: true
});
calcTime(duration, $.lblDuration);
function calcTime(ms, controller) {
    sec = Math.round(ms / 1000);
    if (sec >= 60) {
        min = (parseInt(sec / 60)).toString();
        sec = (Math.round(((sec / 60) - min) * 60)).toString();
        Ti.API.info(sec);
        if (min >= 60) {
            hour = (parseInt(min / 60)).toString();
            min = (parseInt(((min / 60) - hour) * 60)).toString();
        }
    } else {
        sec = sec.toString();
    }
    if (hour.length == 1) {
        hour = "0" + hour;
    }
    if (min.length == 1) {
        min = "0" + min;
    }
    if (sec.length == 1) {
        sec = "0" + sec;
    }
    Ti.API.info(hour + ":" + min + ":" + sec);
    controller.text = hour + ":" + min + ":" + sec;
}
 
function play() {
 
    Ti.Android.NotificationManager.notify(1, notification);
    Ti.API.info(anAudioPlayer.STATE_BUFFERING);
    Ti.API.info(anAudioPlayer.STATE_INITIALIZED);
    anAudioPlayer.start();
}
 
function pause() {
    Titanium.Android.NotificationManager.cancel(1);
    anAudioPlayer.pause();
    Ti.API.info(anAudioPlayer.playing + "||" + anAudioPlayer.paused);
}
 
anAudioPlayer.addEventListener('progress', function(e) {
    Ti.Media.defaultAudioSessionMode = Ti.Media.AUDIO_SESSION_MODE_PLAYBACK;
    $.viewProgress.width = ((Math.round(e.progress) / duration) * 100) + "%";
    var progress = Math.round(Math.round(e.progress) / 1000);
    calcTime(e.progress, $.lblElapsed);
});
anAudioPlayer.addEventListener('complete', function() {
    $.viewProgress.width = "100%";
    anAudioPlayer.release();
});
var intent1 = Ti.Android.createIntent({
    flags : Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP,
    className : 'co.ntime.audioPlayer.AudioplayerActivity',
    action : 1
});
intent1.putExtra('button', 'my first activity');
 
var intent2 = Ti.Android.createIntent({
    flags : Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP,
    className : 'co.ntime.audioPlayer.AudioplayerActivity',
    action : 2
});
intent2.putExtra('button', 'my second activity');
var pending1 = Ti.Android.createPendingIntent({
    activity : Ti.Android.currentActivity,
    intent : intent1,
    type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
    flags : Ti.Android.FLAG_UPDATE_CURRENT
});
var pending2 = Ti.Android.createPendingIntent({
    activity : Ti.Android.currentActivity,
    intent : intent2,
    type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
    flags : Ti.Android.FLAG_UPDATE_CURRENT
});
Ti.Android.currentActivity.addEventListener('newintent', function(e) {
    Ti.API.info('caught intent!');
    if (e.intent.action == 1) {
        anAudioPlayer.start();
        Ti.API.info('play');
    } else {
        anAudioPlayer.pause();
        Ti.API.info('pause');
    }
});
var customView = Ti.Android.createRemoteViews({
    layoutId : Ti.App.Android.R.layout.customview
});
// Reference elements in the layout by prefixing the IDs with 'Ti.App.Android.R.id'
customView.setTextViewText(Ti.App.Android.R.id.message, "audioPlayer");
customView.setTextViewText(Ti.App.Android.R.id.okbutton, "playSound");
customView.setOnClickPendingIntent(Ti.App.Android.R.id.okbutton, pending1);
customView.setTextViewText(Ti.App.Android.R.id.cancelbutton, "pauseSound");
customView.setOnClickPendingIntent(Ti.App.Android.R.id.cancelbutton, pending2);
var notification = Ti.Android.createNotification({
    contentView : customView
});
$.index.open();
index.xml
<Alloy>
    <Window class="container" backgroundColor="white">
        <View backgroundColor="black" borderRadius="25" width="90%" height="50%" top="10%" layout="vertical">
            <View top="5%" width="90%" height="10%" backgroundColor="white" borderColor="black" borderRadius="30">
                <View id="viewProgress" left="0" backgroundColor="#3A7FE1" height="100%" width="1%" borderRadius="30"></View>
            </View>
            <View layout="horizontal" top="5%" width="90%">
                <ImageView image="/images/Play-icon.png" onClick="play" left="5%"></ImageView>
                <ImageView image="/images/Pause-icon.png" left="5%" onClick="pause"></ImageView>
                <Label color="#3A7FE1" id="lblElapsed" left="25%">00:00:00</Label>
                <Label color="#3A7FE1" left="2%">/</Label>
                <Label color="#3A7FE1" id="lblDuration" left="2%"></Label>
            </View>
        </View>
    </Window>
</Alloy>
customview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal" >
    <TextView android:id="@+id/message"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="18dp"
              android:text="Default text" />
    <Button android:id="@+id/okbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OK" />
    <Button android:id="@+id/cancelbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel" />
</LinearLayout>


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>