ios没有允许继续播放怎么办

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios没有允许继续播放怎么办相关的知识,希望对你有一定的参考价值。

var Xut = ;
var isAudio = false;
var audioOnce = null;
var chatid = '';
var fixaudio = function()
if (!isAudio)
isAudio = true;
Xut.newaudio = new Audio();
document.removeEventListener('touchstart', fixaudio, false);

;
document.addEventListener('touchstart', fixaudio, false);

只要碰到页面就创建的了一个audio对象,然后再像这个对象中添加src地址,然后使用play()就可以播放了
if (Xut.newaudio)
audio = Xut.newaudio;
audio.src = url;
else
Xut.newaudio = new Audio();
audio = Xut.newaudio;
audio.src = url;


这样通过动态添加src,就可以实现循环播放了
var clickplaying = 0;
function toplay(list, index, allUrl,allMsgID,chatId)
if (index > list.length - 1)
clickplaying = 1;
return;

var icon = list[index].children[0];
var url = allUrl[index];
if (index === audioOnce) //点击的是同一个
if (audio.paused) //暂停状态
audio.autoplay = true;
audio.play();
icon.setAttribute('class', 'voice-icon played');
else
audio.pause();
icon.setAttribute('class', 'voice-icon paused');

else //点击的不是同一个
//其他暂停
toPause()
//储存播放后状态
var readId = window.localStorage.getItem(chatId)
if(readId)
var setReadId = readId.split(',')
setReadId.push(allMsgID[index]).toString()
else
var setReadId = [allMsgID[index]].toString()

window.localStorage.setItem(chatId,setReadId)

var setRead = document.getElementsByClassName('setread')
if(setRead[index].children[0])
setRead[index].children[0].setAttribute('class', '')

audioOnce = index;
icon.setAttribute('id', 'anchor');
console.log(url);
console.log(Xut.newaudio);

if (Xut.newaudio)
audio = Xut.newaudio;
audio.src = url;
else
Xut.newaudio = new Audio();
audio = Xut.newaudio;
audio.src = url;

audio.load();
audio.onloadstart = function()
icon.setAttribute('class', 'voice-icon playloading');
;
audio.oncanplaythrough = function()
icon.setAttribute('class', 'voice-icon played');
audio.autoplay = true;
audio.play();
;
audio.onended = function()
icon.setAttribute('class', 'voice-icon paused');
toplay(list, index + 1, allUrl,allMsgID,chatId)




我这个地方使用的vue1.0写的。所以使用了watch来监听数据的变化,如果数据有变化,分别给每个页面的ui播放按钮绑定点击事件,并且如果没有播放的情况下,来的数据是音频,那么就自动播放
watch:
talkList: function()
var that = this;
var list = document.getElementsByClassName("voiceSize");
var listLength = this.talkList;
var allUrl = [];
var allSize = [];
var allMsgID = [];
for (j = 0; j < listLength.length; j++)
if (listLength[j].MSG_TYPE == 'VOICE')
this.showonce = 1;
allUrl.push(listLength[j].MEDIA_URL)
allSize.push(listLength[j].MEDIA_SIZE)
allMsgID.push(listLength[j].MSG_ID)

;
for (var i = 0, len = list.length; i < len; i++)
list[i].style.width = size(parseFloat(allSize[i])) + '%';
list[i].onclick = (function(index)
return function()
toplay(list, index, allUrl,allMsgID,that.radioID)

)(i);
;
if(listLength.length)
if(listLength[listLength.length-1].MSG_TYPE == 'VOICE' && clickplaying==1)
toplay(list, list.length-1, allUrl,allMsgID,that.radioID)


,
rightTalkList:function ()
this.rightScroll()

,

当然点击不同的播放按钮的其它的都应该暂停,其实这里只需要换一下页面上的显示而已,因为页面中就只有一个audio标签,所以点击其它的不存在暂停一说,src替换了就不可能还会播放了。
//点击其他播放按钮暂停时页面状态显示
function toPause()
var icon = document.getElementsByClassName('voice-icon')
for (n = 0; n < icon.length; n++)
icon[n].setAttribute('class', 'voice-icon paused');
icon[n].removeAttribute('id')

参考技术A 如下:
工具:iPhone 8
操作系统:ios 14.4.1
操作软件:网易云版本10.05
1、首先打开苹果手机上的网易云音乐,点击左上方的三条杠图标。
2、然后在打开的侧边栏中,点击下方的设置按钮,如下图所示。
3、接着在打开的设置页面中,找到并点击允许与其他应用同时播放设置。
4、这时在弹出的确定窗口中,如下图所示,点击确定按钮,即可完成网易云开启同时播放功能。

允许用户继续在 ios 应用中收听他们的音乐

【中文标题】允许用户继续在 ios 应用中收听他们的音乐【英文标题】:Allow user to continue listening to their music in ios app 【发布时间】:2014-04-17 04:22:31 【问题描述】:

我已经搜索过这个,但到目前为止还没有发现任何有用的东西。在我的应用程序中,任何已经在设备上播放的音乐都会淡出,以替换为我的游戏音乐。

我从经验中知道这真的很烦人,甚至可能成为根本不玩游戏的理由。我怎样才能避免这种情况?更具体地说,如果用户还没有播放音乐,我希望播放游戏的音乐和音效 - 否则游戏音乐应该静音并且用户的音乐应该继续播放。

如果完全相关,我正在使用 AVAudioPlayer 播放我的声音 - 背景音乐和音效。

【问题讨论】:

【参考方案1】:

我从我的 git 历史中提取了这个:

    // Set audio category to ambient - allows music to coexist with the application
     NSError *error;
     AVAudioSession *session = [AVAudioSession sharedInstance];
     [session setCategory: AVAudioSessionCategoryAmbient error: &error];
     if (error) 
        NSLog(@"setCategory failed %@",error);
     
  

希望对你有帮助。

【讨论】:

非常有帮助,谢谢!我想现在唯一的问题是如何查询用户是否已经在播放音乐,以便应用知道是否将自己静音。 对不起 - 应该更仔细地阅读。不知道如何做到这一点,并记住它会随着时间的推移而改变,因此需要某种监听器或轮询。我很想只有一个用户控制的音乐开关和另一个完全静音的开关。

以上是关于ios没有允许继续播放怎么办的主要内容,如果未能解决你的问题,请参考以下文章

iphone app允许背景音乐继续播放

iOS设置蓝牙外设播放音频

iOS:使用 Spotify SDK 在后台继续播放音乐

允许后台进程时无法在 iOS 中获取 Force Quit 事件,怎么办?

Unity用代码控制音乐暂停后怎么继续播放?

iOS动画暂停和继续