2012-10-02 150 views

回答

0

另一种解决方案:创建一个播放器类来包装初始化(使用AVFoundation.framework)。

来源:

#import "MyAVAudioPlayer.h" 

@implementation MyAVAudioPlayer 

-(id)initWithFile:(NSString*)strFile 
{ 
    NSError *err; 
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/"]; 
    resourcePath = [resourcePath stringByAppendingString:strFile]; 
    self = [super initWithContentsOfURL:[NSURL fileURLWithPath:resourcePath] 
            error:&err]; 
    if(err) 
    { 
     NSLog(@"Loading of %@ Failed with reason: %@", strFile, 
       [err localizedDescription]); 
    } 
    else 
    { 
     NSLog(@"Loaded %@", strFile); 
     [self prepareToPlay]; 
    } 
    return self; 
} 
@end 

页眉:

#import <AVFoundation/AVFoundation.h> 

@interface MyAVAudioPlayer : AVAudioPlayer 
{ 
} 
-(id)initWithFile:(NSString*)strFile; 
@end 

用法:

MyAVAudioPlayer *player = [[MyAVAudioPlayer alloc] initWithFile:@"yoursound.mp3"]; 
[player play] 
1

你说的是播放声音,而不从iPod /音乐应用中断的音乐?如果是这样,您需要配置音频会话类别以允许与kAudioSessionProperty_OverrideCategoryMixWithOthers属性混合。

有关示例代码,请参阅Is it possible to play sounds without stopping iPod music?