我正在尝试复制Apple的avTouch中的一些功能,但我无法让AVAudioPlayer
播放曲目。
记录Could not play (null)
。 (null)表示AVAudioplayer player.url
。但是,当我记录player.url
时,它会正确返回。我还测试了流URL(来自soundcloud)。
我几乎只从示例代码中做了两处更改。
我必须在以下代码中添加__bridge
(我实际上并不知道它是什么)
OSStatus result = AudioSessionInitialize(NULL, NULL, NULL, NULL);
if (result)
NSLog(@"Error initializing audio session! %@", result);
[[AVAudioSession sharedInstance] setDelegate: self];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
NSLog(@"Error setting category! %@", setCategoryError);
result = AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, RouteChangeListener, (__bridge void *)self);
if (result)
NSLog(@"Could not add property listener! %@", result);
void RouteChangeListener( void * inClientData,
AudioSessionPropertyID inID,
UInt32 inDataSize,
const void * inData)
{
SongsViewController* This = (__bridge SongsViewController*)inClientData;
if (inID == kAudioSessionProperty_AudioRouteChange) {
CFDictionaryRef routeDict = (CFDictionaryRef)inData;
NSNumber* reasonValue = (__bridge NSNumber*)CFDictionaryGetValue(routeDict, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
int reason = [reasonValue intValue];
if (reason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
[This pausePlaybackForPlayer:This.player];
}
}
}
修改:我认为这种情况正在发生b / c AVAudioPlayer
不适用于网络流。我可以简单地将AVAudioPlayer更改为AVPLayer吗?我觉得这并不容易。
答案 0 :(得分:0)
AVAudioPlayer不适用于网络流。 Apple文档:
Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency.