2012-10-02 79 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?