通过通知按钮播放流声音而不打开应用程序

时间:2015-05-31 09:44:59

标签: android audio stream notifications titanium

尝试使用钛开发流媒体音乐播放器,我添加了一个带播放和暂停按钮的自定义通知,以便我们可以在应用关闭或在后台播放音乐。问题是当我按下播放应用程序打开的播放按钮,但是在启动画面上,我们需要的是在不打开应用程序的情况下播放音乐。 我正在使用titanium SDK 3.4.0

这是我的代码

index.js

Ti.Media.defaultAudioSessionMode = Ti.Media.AUDIO_SESSION_MODE_PLAYBACK;
Ti.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>

远程视图布局

<?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>

1 个答案:

答案 0 :(得分:0)

你需要的是后台服务(我猜) 看看这些链接: