【问题标题】:How to play mp3 file from url in iOS?如何在 iOS 中从 url 播放 mp3 文件?
【发布时间】:2015-08-17 04:53:02
【问题描述】:

我正在尝试播放位于远程服务器上某处的 mp3 文件。我有该音乐文件的 url 我已尝试使用 AVAudioPlayerAVPlayer 播放它,但我无法播放 mp3 文件。请告诉我怎样才能播放 mp3 文件。

播放音乐的代码:

 NSString *aSongURL = [NSString stringWithFormat:@"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
    // NSLog(@"Song URL : %@", aSongURL);

    AVPlayerItem *aPlayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:aSongURL]];
    AVPlayer *anAudioStreamer = [[AVPlayer alloc] initWithPlayerItem:aPlayerItem];
    [anAudioStreamer play];

    // Access Current Time
    NSTimeInterval aCurrentTime = CMTimeGetSeconds(anAudioStreamer.currentTime);

    // Access Duration
    NSTimeInterval aDuration = CMTimeGetSeconds(anAudioStreamer.currentItem.asset.duration);

请告诉我该怎么做?

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    试试下面的代码。

    NSURL *url = [NSURL URLWithString:@"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
    self.playerItem = [AVPlayerItem playerItemWithURL:url];
    self.player = [AVPlayer playerWithPlayerItem:playerItem];
    self.player = [AVPlayer playerWithURL:url];
    [self.player play];
    

    【讨论】:

    • 如何为此显示搜索栏和其他控件。请建议?
    • 您可以使用 AVPlayer 的 duration 属性将 AVPlayer 正在播放的文件的时长映射到 UISlider 的范围(最大值和最小值),并通过更改滑块位置来设置当前时间AVPlayer。
    • 你有一些示例代码或者可能是那个链接
    • 我的意思是它播放你给定的链接 url,但它不播放我的 mp3 url。那里应该有什么问题?
    【解决方案2】:

    尝试使用AVAudioPlayer,见下文

    NSString *aSongURL = [NSString stringWithFormat:@"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:aSongURL] error:nil];
    
    [audioPlayer play];
    

    希望它有效。

    干杯。

    【讨论】:

      猜你喜欢
      • 2016-04-06
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多