- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个音乐应用程序,用户可以浏览精选的轨道并将它们保存到播放列表中。我遇到一个问题,如果一个轨道被保存到播放列表中两次,一次在另一个之后,播放控件不会按预期工作,即 2x track1 被添加到播放列表的开头 - 如果选择第一个 track1 - 播放和暂停好的,那么如果我选择第二个轨道 1(如果已经选择了第一个轨道 1,反之亦然),它将继续播放/暂停,就好像它是第一个轨道 1 一样。
有没有办法复制该项目而不是引用相同的项目?下面是一些代码,显示了我如何填充播放列表和数据结构等。
数据结构:
class Data: NSObject, NSCoding {
var title: String
var body: String
var colour: UIColor
var url: String
var isPlaying : Bool = false
init(title: String, body: String, colour: UIColor, url: String) {
self.title = title
self.body = body
self.colour = colour
self.url = url
}
class func createTrackArray() -> [Data] {
var array: [Data] = []
let track1 = Data(title: "Track 1 Title", body: "Track 1 Body", colour: .white, url: "track1url")
let track2 = Data(title: "Track 2 Title", body: "Track 2 Body", colour: .white, url: "track2url")
array.append(track1)
return array
}
}
显示要浏览的音乐:
//Global Variable
var musicDataArray: [Data] = []
class MusicVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
musicDataArray = Data.createTrackArray()
}
}
//MARK: - CollectionView Cell Configuration
extension MusicVC: UICollectionViewDelegate, UICollectionViewDataSource, CellDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return musicDataArray.count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: .musicCell, for: indexPath) as! MusicCell
cell.cellData = musicDataArray[indexPath.item]
cell.song = musicDataArray[indexPath.item]
cell.cellDelegate = self
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
currentIndexPathItem = indexPath.item
guard let cellToPlay = musicCollectionView.cellForItem(at: IndexPath(item: currentIndexPathItem, section: 0)) as? MusicCell else {
print("Cell not here")
return
}
didSelectCell(for: cellToPlay)
prepareTrackForSavingToPlaylist(track: indexPath.item)
}
func didSelectCell(for cell: MusicCell) {
cell.play()
}
该函数将索引(和关联的数组项??)保存到临时数组中。然后我通过 segue 将其发送到另一个 View Controller ,从而将其保存到播放列表中。即 playlistArray 中只会有一个项目传递给下一个 View Controller 。
func prepareTrackForSavingToPlaylist(track: Int) {
playlistArray.removeAll()
playlistArray.append(musicDataArray[track])
}
下一个 View Controller :传递的数据(来自前一个 View Controller 的临时数组的信息)然后添加到他们在 Collection View 中选择的任何播放列表中
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let passedData = Data(title: dataFromMusicVC.title, body: dataFromMusicVC.body, colour: dataFromMusicVC.colour, url: dataFromMusicVC.url)
playlistArray[indexPath.item].append(passedData)
}
最后用户可以选择一个播放列表,它将显示该播放列表中所有已保存的轨道
extension UserPlaylistsVC: UICollectionViewDelegate, UICollectionViewDataSource, UserDelegate {
func didSelectCell(for cell: UserPlaylistsCell) {
cell.play()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return playlistArray[playlistIndex].count
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let createID = "UserCell"
let userCell = collectionView.dequeueReusableCell(withReuseIdentifier: createID, for: indexPath) as! UserPlaylistsCell
//playlistIndex is just an Int that is based on which cell is selected in a different view controller so that the right playlist is accessed.
userCell.song = playlistArray[playlistIndex][indexPath.item]
userCell.cellData = playlistArray[playlistIndex][indexPath.item]
userCell.userDelegate = self
userCell.delegate = self
return userCell
}
这是用于播放列表中轨道的单元格:
protocol UserDelegate: class {
func didSelectCell (for cell: UserPlaylistsCell)
}
class UserPlaylistsCell: UICollectionViewCell {
@IBOutlet weak var titleLbl: UILabel!
@IBOutlet weak var bodyLbl: UILabel!
@IBOutlet weak var colourView: UIView!
var song : TypeData!
var audio = Audio()
weak var delegate: UserPlaylistDelegate?
weak var userDelegate: UserDelegate?
override func prepareForReuse() {
super.prepareForReuse()
}
override var isSelected: Bool {
didSet {
self.contentView.backgroundColor = isSelected ? UIColor.UMLightGrey : UIColor.white
}
}
override func awakeFromNib() {
super.awakeFromNib()
titleLbl.textColor = UIColor.UMDarkGrey
bodyLbl.textColor = UIColor.UMDarkGrey
colourView.layer.cornerRadius = colourView.layer.frame.size.width / 2
colourView.clipsToBounds = true
}
var cellData: TypeData! {
didSet {
titleLbl.text = cellData.title
bodyLbl.text = cellData.body
colourView.backgroundColor = cellData.colour
}
}
func play() {
if !(song?.isPlaying)! {
song?.isPlaying = true
//Checking whether the global variable is the same as the selected song url so that I don't have to fetch the asset again (fetching the asset plays the track from the beginning again)
if urlString == song.url {
player.play()
} else {
urlString = song.url
audio.fetchAsset()
audio.playAsset()
}
return
}
song?.isPlaying = false
player.pause()
print("Stop", (song?.title)!)
}
}
您是否知道为什么如果同一轨道连续两次保存到播放列表中并被选择播放 - 控件的行为不会像它是不同的轨道一样,即所需的功能是它会被视为完全不同的轨道,选择它会再次从头开始播放,同时还保留单独暂停和播放它们的能力。
如果您需要更多说明或额外代码,请随时询问。我真的很感谢您花时间研究这个问题。
编辑
这是请求的 didSelectItemAt 方法:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
currentItemIndex = indexPath.item
guard let cellToPlay = userPlaylistsCollectionView.cellForItem(at: IndexPath(item: currentItemIndex, section: 0)) as? UserPlaylistsCell else {
return
}
didSelectCell(for: cellToPlay)
}
编辑
didDeselect方法:
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let cellToStop = userPlaylistsCollectionView.dataSource?.collectionView(userPlaylistsCollectionView, cellForItemAt: indexPath) as? UserPlaylistsCell {
if (cellToStop.song.isPlaying) {
didSelectCell(for: cellToStop)
print(cellToStop.song.isPlaying)
}
}
}
最佳答案
问题可能出在这里
if urlString == song.url {
player.play()
} else {
urlString = song.url
audio.fetchAsset()
audio.playAsset()
}
这是基于以下事实:两首歌曲具有不同的 url,但如果轨道相同,则它们肯定具有相同的 url。尝试不同的缓存系统,例如包含先前/当前播放/正在播放的歌曲 ID 的数组(例如,基于单元格索引路径)。
无论如何,让用户在播放列表中多次使用同一轨道是一种不好的行为。
关于ios - 如何判断我是否使用同一项来填充多个 UICollectionViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52383815/
判断置顶文章 is_sticky() 函数用来判断一篇文章是否为置顶文章。 用法 ?
判断结构要求程序员指定一个或多个要评估或测试的条件,以及条件为真时要执行的语句(必需的)和条件为假时要执行的语句(可选的)。 下面是大多数编程语言中典型的判断结构的一般形式: 判断语句 C
我经常这样写: (if (nil? a-value) another-value a-value) 是否有更简单的功能可用,例如: (if-nil? a-value another-value) 最佳
MySQL IF 语句允许您根据表达式的某个条件或值结果来执行一组 SQL 语句。 要在 MySQL 中形成一个表达式,可以结合文字,变量,运算符,甚至函数来组合。表达式可以返回 TRUE,FA
也就是说,是否有一种工具可以自动显示给定语法的完整语言,包括突出歧义(如果有)? 最佳答案 BNF 风格的文法可能有一些特殊性,但总的来说,确定给定的上下文无关文法(例如 BNF)是否有歧义是不可能的
有没有办法确定像下面这样的 Axios 请求是否收到了答案并完成了? axios.get('/api') .then(response => this.data = response.data); 最
我想请大家禁用 Firebug 。如何确定自己安装了firebug?所以它是一个跨浏览器,并在 Chrome、Mozilla 和 IE8 + 中确定 最佳答案 两步: 如果 window.consol
我有一个看起来像这样的对象: var searchFilter = {_id: XXX, approved: true} 用于驱动 Meteor 集合搜索过滤器。然后,我有一对文本框,允许用户输入一系
我正在循环并向我的数据库中插入几百万条记录。性能是第一要务。 我想利用无状态 session ,但您可能知道它们不支持在更复杂的实体上级联对象。 是否有一种通用方法可以确定实体是否具有级联记录?如果是
我正在使用 pdfminer 解析一些 PDF 文件。图书馆。 我需要知道文档是否是扫描文档,扫描机将扫描图像放在顶部,将 OCR 提取的文本放在背景中。 有没有办法识别文本是否可见,因为 OCR 机
我正在寻找一种方法来找出当前为浏览器游戏 TribalWars 编写的脚本打开的页面。 URL 的设置非常相似,对于知道自己在做什么的人来说这应该很容易(我显然不知道)。 URL 如下所示: http
我在 C# 中使用包装的 C 库,需要将图像从该库转换为位图并返回,但没有复制像素缓冲区。 转换为位图很简单: Bitmap WrapAsBitmap(CImage image) { retu
有没有办法检查调用方法的Controller是否来自Area内的Controller? 例如,我有一个继承自 AuthorizeAttribute 的类,例如 public class CustomA
是否可以找到MySQL View 中某列所属的表名? 如果 View 构造为 CREATE VIEW alpha_view AS SELECT alpha.col1, alpha.col2,
如何判断 .Net 应用程序是作为桌面应用程序运行还是作为服务运行? 我们正在尝试使用 Fitnesse 测试我们的应用程序,它将应用程序作为服务加载,然后调用它。但是当一个模式错误框被按下时,它就会
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及
我试图计算出 iframe 内容的大小,以便调整 iframe 元素的大小以包含其内容。 如何确定 iFrame 是否已加载以及我是否可以可靠地测量它的内容尺寸。 注意:onload 事件不会执行,因
这个问题在这里已经有了答案: How to write portable code in c++? (12 个答案) 关闭 9 年前。 我正在尝试编写可以用任何现代版本的 g++ 编译的代码,但遇到
这个问题在这里已经有了答案: distinguish shared objects from position independent executables (2 个答案) 关闭 4 年前。 我有
我的目标是如果 dte 与当前时间相差不到 1 小时,则停止循环。是否有“ ruby 方式”来做到这一点? #THIS IS AN INFINITE LOOP, DONT RUN THIS dte=D
我是一名优秀的程序员,十分优秀!