【问题标题】:Stream Audio with AVPlayer使用 AVPlayer 串流音频
【发布时间】:2014-05-15 11:24:12
【问题描述】:

我正在尝试使用 AVPlayer 播放来自 URL 的音频,当在表格视图中选择了一行但它没有播放时。我做错了什么?

代码如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *streamingString = [NSString stringWithFormat:@"%@.json?client_id=fc886d005e29ba78f046e5474e3fdefb", [streamURLArray objectAtIndex:indexPath.row]];
    NSURL *streamingURL = [NSURL URLWithString:streamingString];
    NSLog(@"%@", streamingURL);
    AVPlayer *player = [AVPlayer playerWithURL:streamingURL];
    [player play];
    player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
}

当我记录它时,streamingURL 的一个例子是这样的。

http://api.soundcloud.com/tracks/146814101/stream.json?client_id=fc886d005e29ba78f046e5474e3fdefb

【问题讨论】:

标签: ios objective-c avplayer


【解决方案1】:

在您的 .h 文件中全局声明 AVAudioPlayer

喜欢这个AVPlayer *player;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *streamingString = [NSString stringWithFormat:@"%@.json?client_id=fc886d005e29ba78f046e5474e3fdefb", [streamURLArray objectAtIndex:indexPath.row]];
    NSURL *streamingURL = [NSURL URLWithString:streamingString];
    NSLog(@"%@", streamingURL);
    player = [AVPlayer playerWithURL:streamingURL];
    [player play];
    player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
}

【讨论】:

  • 这段代码对我不起作用。你知道任何替代方法吗?
【解决方案2】:
【解决方案3】:

调用这个方法

[player prepareToPlay];

打电话之前

[player play];

可能对你有帮助。

【讨论】:

  • 他使用的是 AVPlayer,而不是 AVAudioPlayer,因此“prepareToPlay”方法调用不起作用
猜你喜欢
  • 2013-04-11
  • 2012-01-18
  • 2017-07-07
  • 2012-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-08
  • 2011-07-06
相关资源
最近更新 更多