首页 文章

AVPlayer的音频流

提问于
浏览
6

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加载它时,我正在使用的真实网址正常工作 .

如果有人能指出一个好的样本(不是来自Apple的AVFlayer或AVTouchController项目的AVFlayer文档),我们将非常感激 .

谢谢 !

2 回答

  • 0

    urlAddress release 正在引发我认为的问题 .

    你没有使用alloc创建你的NSString,因此通过释放它你会过度释放它并获得EXC_BAD_ACCESS .

    除非您使用alloc和init显式创建NSString,否则创建字符串的便捷方法是自动释放的 .

  • 5
    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,但它不支持停止 .

相关问题