使用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
在.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; }
你会在GitHub周围找到许多音频流套件。
https://github.com/tumtumtum/StreamingKit
叫这个方法
[player prepareToPlay];
在打电话之前
[player play];
这可能对你有所帮助。