使用 AVPlayer 进行音频流

发布于 2024-10-18 00:23:31 字数 835 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你好,陌生人 2024-10-25 00:23:31

我认为 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.

ぶ宁プ宁ぶ 2024-10-25 00:23:31
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:fileURL];
//AVPlayer *avPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:url]] retain];
avPlayer = [[AVPlayer playerWithPlayerItem:playerItem] retain];
//AVPlayerLayer *avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];
[avPlayer play];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

我用它来播放mp3,但它不支持停止。

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:fileURL];
//AVPlayer *avPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:url]] retain];
avPlayer = [[AVPlayer playerWithPlayerItem:playerItem] retain];
//AVPlayerLayer *avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];
[avPlayer play];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

I use it this way to play mp3, but it doesn't support stop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文