使用 AVPlayer 进行音频流
iOS 上有很多流媒体应用程序。他们都使用一个播放器,我认为是 AVPlayer。然而,似乎不可能找到一个像样的文档以及有效的示例代码!我很确定这只是几行代码,但我就是不知道出了什么问题......
在尝试调用“play”方法时出现 EXC_BAD_ACCESS 错误。但网址很好,并且有播放器的实例。
- (void)viewDidLoad {
[super viewDidLoad];
// Load the array with the sample file
NSString *urlAddress = @"http://mystreamadress.mp3";
//Create a URL object.
urlStream = [NSURL URLWithString:urlAddress];
self.player = [AVPlayer playerWithURL:urlStream];
[urlAddress release];
}
urlStream 是一个具有retain 属性的属性。然后我有一个 IBAction,当单击按钮时会触发并尝试播放它,这就是它崩溃的地方。
- (IBAction)playButtonPressed
{
[player play];
}
我的问题可能是因为我正在尝试播放 MP3 还是什么原因?当我使用 webview 加载它时,我使用的真实 url 地址工作正常。
如果有人能给我指出一个好的示例(不是来自 Apple 的 AVFoundation 或 AVPlayer 文档,也不是 AVTouchController 项目),我将非常感激。
谢谢 !
There's a lot of streaming apps out there for iOS. They all use a player, which I assume is the AVPlayer. Yet it seems impossible to find a decent documentation with sample code that works ! I'm pretty sure it's nothing but a few line of codes, but I just can't figure out what's wrong...
I have an EXC_BAD_ACCESS error when trying to call the "play" method. But the url is good and there is an instance of the player.
- (void)viewDidLoad {
[super viewDidLoad];
// Load the array with the sample file
NSString *urlAddress = @"http://mystreamadress.mp3";
//Create a URL object.
urlStream = [NSURL URLWithString:urlAddress];
self.player = [AVPlayer playerWithURL:urlStream];
[urlAddress release];
}
The urlStream is a property with retain attribute. Then I have an IBAction that fires when the button is clicked and that tries to play it, and that's where it crashes.
- (IBAction)playButtonPressed
{
[player play];
}
Can my problem be because I'm trying to play MP3 or what ? The real url adress I'm using works fine when I use a webview to load it.
If anyone could point me to a good sample (not the AVFoundation ou AVPlayer docs from Apple nor the AVTouchController project) it would be realy appreciated.
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为
urlAddress release
导致了这个问题。您没有使用 alloc, init 创建 NSString,因此通过释放它,您会过度释放它并获得 EXC_BAD_ACCESS。
除非您使用 alloc 和 init 显式创建 NSString,否则创建字符串的便捷方法会自动释放。
urlAddress release
is causing the issue I think.You didn't create your NSString with alloc, init so by releasing it you are overreleasing it and getting the EXC_BAD_ACCESS.
Unless you explicitly create an NSString with alloc and init then the convenience methods of creating the string are autoreleased.
我用它来播放mp3,但它不支持停止。
I use it this way to play mp3, but it doesn't support stop.