AVPlayer / AudioPlayer:没有错误但没有声音

时间:2015-05-18 14:23:02

标签: ios objective-c avaudioplayer avplayer avaudiosession

我已经尝试了十几种不同的方法来尝试流式传输this radio station。然而,无论我尝试了多少种不同的方式,它们都没有奏效。我意识到提供的链接不是.mp3文件,或者实际上是任何类型的音频文件(我不知道在哪里可以找到流式广播电台的音频文件),但我尝试了其他的网址同样缺乏良好的效果。当我在我的viewdidload方法中尝试这个时,没有错误但没有结果:

NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

// Init player
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://tunein.com/radio/KOIL-1290-s33415/?streamid=61377300"]];
AVPlayer *audioPlayer = [AVPlayer playerWithPlayerItem: playerItem];
[audioPlayer play];

我也尝试了所有这些方法(每种方式都在它自己的/ * ...... * /):

/*
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://tunein.com/radio/KOIL-1290-s33415/"]];
NSError *error;

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];

if (audioPlayer == nil)
    NSLog(@"%@", [error description]);
else
    [audioPlayer play];
 */


/*
NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];

              // You may find a test stream at <http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8>.

              AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];

              //(optional) [playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];

              AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];

              player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];

              //(optional) [player addObserver:self forKeyPath:@"status" options:0 context:&PlayerStatusContext];

 */

/*
NSString* resourcePath = @"http://streema.com/radios/play/7146"; //your url
NSURL *url = [[NSURL alloc]initWithString:resourcePath];

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

audioPlayer.delegate = self;

[audioPlayer prepareToPlay];
[audioPlayer play];
 */
/*
//download file and play from disk
NSData *audioData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp3", docDirPath , @"listen"];
[audioData writeToFile:filePath atomically:YES];

NSError *error;
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (player == nil) {
    NSLog(@"AudioPlayer did not load properly: %@", [error description]);
} else {
    [player play];
}
 */

编辑:我也尝试了this,但未成功。

1 个答案:

答案 0 :(得分:0)

在您的每一次尝试中,问题是您的AVPlayer仅由本地变量引用:

AVPlayer *audioPlayer = [AVPlayer playerWithPlayerItem: playerItem];
[audioPlayer play];

因此,你告诉玩家玩家,然后你的代码结束,玩家在有机会开始玩之前就被摧毁了!如果你想让它玩,你需要让玩家坚持至少在它正在播放的时间内。使它成为视图控制器或其他将持续那么长的类实例的实例变量/属性。