English 中文(简体)
AVAudio 运动员立即与阿农发委玩s
原标题:AVAudioPlayer stops playing immediately with ARC

我试图通过<条码>AVAudioPlayer<>条码”来操演MP3,我认为这很简单。 不幸的是,它没有很好地发挥作用。 在此,我做了以下工作:

  • For the sake of testing, I created a new iOS application (Single View) in Xcode.
  • I added the AVFoundation framework to the project as well as the #import <AVFoundation/AVFoundation.h> to the ViewController.m

  • 我在申请文件夹中增加了MP3文件。

  • 页: 1 ViewControllersviewDidLoad: to the following:

法典:

- (void)viewDidLoad
{
    [super viewDidLoad];        

    NSString* recorderFilePath = [NSString stringWithFormat:@"%@/MySound.mp3", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];    

    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:recorderFilePath] error:nil];
    audioPlayer.numberOfLoops = 1;

    [audioPlayer play];

    //[NSThread sleepForTimeInterval:20];
}

不幸的是,声音在开始游戏之后显然停止了。 如果我不赞同<条码> 暂时开放<> 代码>,则该编码为20秒钟,其后停刊。 这一问题只有在与阿农法委员会进行汇编时才会发生,否则,它便徒劳地运作。

最佳回答

问题是,在汇编。 你们需要确保经常提及你希望活下来的事例,因为汇编者将自动通过插入<代码>release/code>而固定“unbalanced”>。 电话(至少从概念上看,改为 Mikes Ash blog员额,详见。 你们可以通过将案件分配给财产或案件变数来解决这一问题。

在Phlibbo案中,该法典将转化为:

- (void)viewDidLoad
{
    [super viewDidLoad];        
    NSString* recorderFilePath = [NSString stringWithFormat:@"%@/MySound.mp3", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];    
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:recorderFilePath] error:nil];
    audioPlayer.numberOfLoops = 1;
    [audioPlayer play];
    [audioPlayer release]; // inserted by ARC
}

则在没有提及的情况下立即停止营业。

我过去曾用过非洲排雷中心,只是简单地阅读了它。 如果你对此有了更多了解,请就我的回答发表评论,我将提供更多信息。

More ARC information:
Transitioning to ARC Release Notes
LLVM Automatic Reference Counting

问题回答

如果你同时需要多台AVAudioPlayers,则会创建一台NSMutableDictionary。 将钥匙定为档案名称。 通过代表休息从假肢解:

-(void)playSound:(NSString*)soundNum {


    NSString* path = [[NSBundle mainBundle]
                      pathForResource:soundNum ofType:@"m4a"];
    NSURL* url = [NSURL fileURLWithPath:path];

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

    audioPlayer.delegate = self;

    if (_dictPlayers == nil)
        _dictPlayers = [NSMutableDictionary dictionary];
    [_dictPlayers setObject:audioPlayer forKey:[[audioPlayer.url path] lastPathComponent]];
    [audioPlayer play];

}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {        
   [_dictPlayers removeObjectForKey:[[player.url path] lastPathComponent]];
}

Use the AVAudioPlayer as an ivar in the header file with strong:

@property (strong,nonatomic) AVAudioPlayer *audioPlayer




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...