Android 通过通知按钮播放流音,无需打开应用程序

Android 通过通知按钮播放流音,无需打开应用程序,android,audio,stream,notifications,titanium,Android,Audio,Stream,Notifications,Titanium,为了开发一款使用钛合金的流媒体音乐播放器,我添加了一个带有播放和暂停按钮的自定义通知,这样我们可以在应用程序关闭或在后台播放音乐。问题是当我在通知中按播放按钮时,应用程序会打开,但会在启动屏幕上,我们需要的是在不打开应用程序的情况下播放音乐。 我使用的是Tianium SDK 3.4.0 这是我的密码 index.js index.xml 00:00:00 / 远程视图布局 我想你需要的是后台服务。 请查看以下链接: 首先感谢您的回答,其次问题是我想从serive文件控制应用程序中的音

为了开发一款使用钛合金的流媒体音乐播放器,我添加了一个带有播放和暂停按钮的自定义通知,这样我们可以在应用程序关闭或在后台播放音乐。问题是当我在通知中按播放按钮时,应用程序会打开,但会在启动屏幕上,我们需要的是在不打开应用程序的情况下播放音乐。 我使用的是Tianium SDK 3.4.0

这是我的密码 index.js index.xml

00:00:00
/
远程视图布局
我想你需要的是后台服务。
请查看以下链接:


首先感谢您的回答,其次问题是我想从serive文件控制应用程序中的音频,因此如何暂停服务文件中应用程序文件中的音频对象
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();
<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>