如何从iPhone上的在线网址播放音频

如何从iPhone上的在线网址播放音频,第1张

概述参见英文答案 > How to play a music file using URL link in iPhone?                                    5个 我知道很多人之前已经问过这个问题,但是我不知道为什么这在x-code和objective c中我的应用程序不起作用! 基本上,我想通过网址在应用程序中播放音频.只需使用Apple网站上的测试音频,我的代码 参见英文答案 > How to play a music file using URL link in iPhone?                                    5个
我知道很多人之前已经问过这个问题,但是我不知道为什么这在x-code和objective c中我的应用程序不起作用!

基本上,我想通过网址在应用程序中播放音频.只需使用Apple网站上的测试音频,我的代码就是:

Nsstring *playString = @"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";//Converts songURL into a playable NSURLNSURL *playURL = [NSURL URLWithString:playString];AVAudioPlayer *backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:playURL error:&error];if (backgroundMusicPlayer == nil) {    NSLog(@"%@",[error description]);}else {    [backgroundMusicPlayer preparetoPlay];    [backgroundMusicPlayer play];}

它出现以下错误:

错误域= NSOsstatusErrorDomain代码= -43“无法完成 *** 作.(Osstatus错误-43.)

当我Google它时,我找不到任何东西.
有任何想法吗?我应该尝试其他音频流吗?或者有没有人有一个有效的例子?是因为我使用iPhone模拟器而不是真正的iPhone吗?

编辑:

请注意 – 当我说URL时,我的意思是来自一个真实的网站,比如“http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8”
我不想先下载这首歌,我希望能够在下载时播放它!谢谢

解决方法 使用AVPlayer从url播放mp3文件

-(voID)playselectedsong{    AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];    self.songPlayer = player;    [[NSNotificationCenter defaultCenter] addobserver:self                                             selector:@selector(playerItemDIDReachEnd:)                                                 name:AVPlayerItemDidplayToEndTimeNotification                                               object:[songPlayer currentItem]];    [self.songPlayer addobserver:self forKeyPath:@"status" options:0 context:nil];    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];    [self.songPlayer play];}- (voID)observeValueForKeyPath:(Nsstring *)keyPath ofObject:(ID)object change:(NSDictionary *)change context:(voID *)context {    if (object == songPlayer && [keyPath isEqualToString:@"status"]) {        if (songPlayer.status == AVPlayerStatusFailed) {            NSLog(@"AVPlayer Failed");        } else if (songPlayer.status == AVPlayerStatusReadytoplay) {            NSLog(@"AVPlayerStatusReadytoplay");        } else if (songPlayer.status == AVPlayerItemStatusUnkNown) {            NSLog(@"AVPlayer UnkNown");        }    }}- (voID)playerItemDIDReachEnd:(NSNotification *)notification { //  code here to play next sound file}
总结

以上是内存溢出为你收集整理的如何从iPhone上的在线网址播放音频全部内容,希望文章能够帮你解决如何从iPhone上的在线网址播放音频所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1069220.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存