使用AVPlayer流式传输音频

时间:2014-05-15 11:24:13

标签: ios objective-c avplayer

我正在尝试使用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

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

在.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 :(得分:-4)

调用此方法

[player prepareToPlay];

之前打电话

[player play];

这对你有帮助。