使用AVPlayer流式传输音频 [英] Stream Audio with AVPlayer

查看:212
本文介绍了使用AVPlayer流式传输音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to play audio from a URL using AVPlayer when a row is selected in a table view but it doesn't play. What am I doing wrong?

以下是代码:

- (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。

An example of streamingURL when I log it is this.

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


推荐答案

在.h文件中全局声明AVAudioPlayer

declare AVAudioPlayer globally in your .h file

喜欢这个 AVPlayer *播放器;

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

这篇关于使用AVPlayer流式传输音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆