- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这里有点不知所措。我正在使用 Xcode 7 和 Swift 2,我正在尝试寻找实际有效的流式音频示例。
本质上我希望能够流式传输,但我也试过这个:Swift Radio Streaming AVPlayer
它使用 AVAudioPlayer。 github 代码有效,但是使用相同的音频文件将代码复制到我的项目中会使应用程序崩溃:由于未捕获的异常 'NSInvalidArgumentException',原因:'-[SwiftBasicMVC_Git.AudioStreamProxy copyWithZone:]:
无法识别的选择器发送到实例 0x14fdea100'
我还尝试了一些 ACPlayer 示例和 AVQueuePlayer,但没有成功播放任何音乐。
最佳答案
我将单例用于我的基本 radio 应用程序,因为需要在每个页面上访问和处理它。创建起来非常简单,让我们从声明单例和变量开始吧。我会一步一步解释一切;
import AVKit
import AVFoundation
private var radioContext: UInt8 = 1
final class RadioModel: NSObject {
dynamic var radioItem:AVPlayerItem? = nil
dynamic var radioPlayer:AVPlayer? = nil
static let sharedModel = RadioModel()
private final let dataModel = BasicRadioApp.DataManager
private override init() {
super.init()
do {
_ = try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
_ = try AVAudioSession.sharedInstance().setActive(true)
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
} catch {
print("Error")
}
}
如果您想将 AirPlay 模式用于播客或广播,您必须使用 KVO(键值观察器)控制您的声音对象,它不支持 Swift。因此,我们的 AVPlayerItem
和 AVPlayer
对象由 dynamic
定义,这意味着根据文档;
Apply this modifier to any member of a class that can be represented by Objective-C. When you mark a member declaration with the dynamic modifier, access to that member is always dynamically dispatched using the Objective-C runtime. Access to that member is never inlined or devirtualized by the compiler.
Because declarations marked with the dynamic modifier are dispatched using the Objective-C runtime, they’re implicitly marked with the objc attribute.
我在我的另一个 Singleton 对象 BasicRadioApp
中从服务器获取数据。
我需要控制我的 Audio Session 与其他人(例如 Apple Watch)的交互,因此需要设置您的 AVAudioSessionCategory
。我还想在 Apple Watch 音乐应用程序的锁屏和 Glance 上远程控制我的音频。
在AVAudioSession
类中,这个函数是这样声明的;
/* set session category */
public func setCategory(category: String) throws
如您所见,它们会抛出异常,因此您需要在错误处理中设置它们,例如 >Swift 2.0 的 do/try
概念。
让我们尝试播放声音;
final func playRadio() {
do {
try removeObserverFromRadioItem()
} catch {
print("Can not remove observer from AVPlayerItem object!")
}
radioItem = AVPlayerItem(URL: NSURL(string: dataModel.radioStreamUrl)!)
radioPlayer = AVPlayer(playerItem: radioItem!)
radioItem!.addObserver(self, forKeyPath: "status", options: [.New, .Initial, .Old], context: &radioContext)
if MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo == nil {
let albumArt = MPMediaItemArtwork(image: UIImage(named: "AlbumArt")!)
let songInfo: Dictionary = [MPMediaItemPropertyTitle: "Now listening Hard Rock Bla Bla FM",
MPMediaItemPropertyArtist: "My Radyo",
MPMediaItemPropertyAlbumTitle: "105.5 FM",
MPMediaItemPropertyArtwork: albumArt]
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo
}
}
在声明我的 AVPlayerItem
之前,如果它存在,我需要删除它上面的 KVO。您将看到 removeObserverFromRadioItem()
接下来在做什么。我需要将我的 AVPlayerItem
注册到 KVO 对象,它会处理是否播放。正如我所说,我需要在锁定屏幕上远程控制我的音频,因此我为此使用了 MPNowPlayingInfoCenter
。
final func stopRadio() {
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = nil
radioPlayer?.pause()
}
final func removeObserverFromRadioItem() throws {
radioItem?.removeObserver(self, forKeyPath: "status", context: &radioContext)
}
最重要的部分在这里;
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if object as? AVPlayerItem == radioPlayer?.currentItem && keyPath! == "status" {
if self.radioPlayer?.currentItem!.status == .Failed {
print("STREAM FAILED")
} else if self.radioPlayer?.currentItem!.status == .ReadyToPlay {
self.radioPlayer!.play()
radioDelegate?.RadioDidStartPlay()
}
return
}
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
你如何从任何类(class)开始 radio ?
class RadioController: UIViewController {
let sharedRadioPlayer = RadioModel.sharedModel
..
override func viewDidLoad() {
super.viewDidLoad()
// check if radio is NOT playing
if sharedRadioPlayer.radioPlayer?.rate < 1 {
sharedRadioPlayer.playRadio()
}
}
最后一步是从锁屏上远程我的应用程序。停止、播放等。对于远程,我在 AppDelegate
的 RadioModel
中调用我的方法。在 AppDelegate 中;
override func remoteControlReceivedWithEvent(event: UIEvent?) {
RadioModel.sharedModel.handleRemoteControlEvent(event!)
}
并在 radio 模型
中;
func handleRemoteControlEvent(responseEvent: UIEvent) {
if responseEvent.type == UIEventType.RemoteControl {
switch(responseEvent.subtype) {
case UIEventSubtype.RemoteControlPlay:
playRadio()
break
case UIEventSubtype.RemoteControlPause:
stopRadio()
break
case UIEventSubtype.RemoteControlTogglePlayPause:
if self.radioPlayer!.rate > 0 {
stopRadio()
} else {
playRadio()
}
break
default:
print("Remote Error")
}
}
}
不要忘记在您的应用中 TARGETS > Capabilities > 打开背景模式并选择音频、AirPlay 和画中画。然后检查您的 info.plist 并搜索“需要背景模式”。如果它不存在,则创建一个新的数组并设置“App plays audio or streams audio/video using AirPlay”项。就是这样,您现在可以播放 airPlay 广播/流媒体了。
关于ios - 获取任何 AudioPlayer 来传输或播放音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34315602/
启动Android游戏时,我试图让一些音乐淡出。音乐在主菜单中播放,然后在播放器单击播放时淡出。我可以停止播放音乐,只是不会消失。 我试图用这个淡出: using UnityEngine; using
我正在 XNA 中创建一个游戏,它可以使用大量的音乐循环,但我似乎无法同步这些声音。 我总是错过几毫秒你能帮我吗? 这是我第一次尝试同步声音。请注意,我需要处理数十种声音...... 这个同步问题可能
我使用以下代码播放了一首歌,但它有 11 分钟长,我该如何停止它? 此代码位于 jFrame 1 中。如何使其停止在 jFrame 2 中? Input
在阅读我的书并浏览一些 YouTube 教程后,我对标准化的理解是,重要的事情之一就是不要有重复的值。更具体地说,主键 (ID) 不应重复。 因此,如果我正在使用音乐/音乐会数据库中的某些表,那么以下
我正在用 java 创建一个应用程序/游戏,其中包含背景音乐/声音。一切都按预期工作。如果播放某些系统声音/媒体声音/其他声音,我想静音/停止。 有什么建议...?? 最佳答案 我建议不要关心这个问题
只是尝试从图像 (1080p .png) + 音乐 (320Kb mp3) 为 youtube 制作视频。 ffmpeg -loop 1 -i image.png -i music.mp3 -
我正在GW-Basic中开发游戏,我想向其中添加音乐,但问题是我无法在后台播放,但是当我添加声音时先播放声音,然后在开始执行游戏和vICE之后vERSA。而我希望这两件事同时播放..所以知道怎么做吗?
我正在使用Xcode的Sprite套件开发iPhone游戏,想知道是否有一种简单或最佳实践的方法可以通过编程方式消除所有声音效果/音乐?对我来说,最明显的方法是创建一些 bool(boolean) 变
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 3年前关闭。 Improve this questi
我正在设计一个 Android 应用程序,需要依次播放各种短音乐文件。 我设置了一个“onClick”监听器来运行一个循环,一遍又一遍地播放这些声音文件,它们可能会播放 100 多次。 但是 我需要用
我有一个网站,其中包含集成到布局中的 Flash 音乐播放器。我希望用户能够在不中断音乐的情况下浏览网站。我做了很多研究和思考,以下是我提出的选项(请记住,我希望尽可能对 SEO 友好)。有人有其他想
我可以即时生成一些音调并播放声音吗?例如,如果我想生成 DO RI MI FA SO LA XI 音调并播放它们? 似乎声音 API 都是为了播放现有的音频文件。谢谢! 最佳答案 参见 Matt Ga
我一直在尝试将音乐添加到我的应用程序中,但没有特别成功。我一直在尝试使用 AVFoundation,我的代码如下: //MUSIC var audioPlayer = AVAudioP
MPMediaPickerController 返回一个 MPMediaItem 如果返回本地 url,则可以播放声音。 如果音乐没有下载到本地,则assetURL为空。 如何在本地下载 MPMedi
有什么方法可以检测是否正在播放 iPod 应用以外的其他来源的音乐或媒体? 我知道我可以检测到从 ipod 播放的音乐: [[MPMusicPlayerController iPodMusicPlay
我刚刚安装了 music21,我正在尝试开始使用它。我安装了 Musescore3,但我无法使 show() 函数工作。 我试过按照我在网上找到的一些说明更改路径,但无济于事 from music21
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我目前正在寻找某种方式来扩展SFML.Net以使用MP3。因此,ioctllr推荐了NLayer,我想尝试一下。这是我的方法: namespace AudioCuesheetEditor.AudioB
我包含了正确的库和所有内容,这部分代码由于某种原因给出了段错误: int numerator = atoi(&fraction[0]); int denominator = atoi(&fracti
是否可以播放 AVPLayer 实例(带有视频)并同时使用 MPMusicPlayerController 实例来播放 iTunes 播放列表? 我尝试了几种解决方案,但每次播放视频时音频都会停止,每
我是一名优秀的程序员,十分优秀!