使用AVPlayer流式传输音频

3

我想在表格视图中选择一行时通过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

这可能会有帮助 https://dev59.com/xW_Xa4cB1Zd3GeqP6u0z - DipakSonara
3个回答

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;
}

这段代码对我不起作用。你知道有什么替代方法吗? - Moxarth


-4

调用这个方法

[player prepareToPlay];

在调用之前

[player play];

这可能会有所帮助,您。


3
他正在使用AVPlayer,而不是AVAudioPlayer,因此“prepareToPlay”方法调用将无效。 - Felix Lapalme

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接