iphone - 音频 (mp3) 文件问题,按按钮次数不播放文件

标签 iphone ios

我在内存游戏应用程序中设置了点击按钮时的音乐文件。

音乐正常播放了一段时间,但在点击按钮数量后,应用程序的声音效果消失了。

-(void)setButtons_AsPerTheMatrixSelection
{
    for ( i = 0 ; i < cv ; ++i )
    {
        for ( j = 0 ; j < ch ; ++j )
        {
            btnMatrix = [[[UIButton alloc] initWithFrame:CGRectMake(10+pw*j, 51+ph*i, width, height)] autorelease];
            btnMatrix.tag = i*ch+j;
            btnMatrix.userInteractionEnabled = TRUE;
            bulImageStatus = FALSE;
            [btnMatrix addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
            [btnMatrix setImage:imgDefaultBG forState:UIControlStateNormal];
            [viewWithButtons addSubview:btnMatrix];

            [arrButton addObject:btnMatrix];
        }
    }
}    

-(void)changeImage:(id)sender
{
    NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"mp3"];
    if (musicPath) 
        TapSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath] error:nil];

    [TapSoud setDelegate:self];
    [TapSoud prepareToPlay];
    [TapSoud play];
}

这对于按下次数非常有效,但在某些点击后我无法播放此按钮点击音。

可能是什么问题以及如何解决这个问题。

有没有其他方法可以播放音频文件?

请提出合适的解决方案。

谢谢。

最佳答案

AVAudioPlayer 被反复分配但从未释放。

试试这个。

-(void)changeImage:(id)sender
{
    NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"mp3"];
    if (musicPath) {
        if(TapSoud){ // TapSound ? 
             [TapSoud release];
              TapSoud = nil;
        }
        TapSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL 
fileURLWithPath:musicPath] error:nil];
}

[TapSoud setDelegate:self];
[TapSoud prepareToPlay];
[TapSoud play];

关于iphone - 音频 (mp3) 文件问题,按按钮次数不播放文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10877909/

相关文章:

iphone - iOS6 UIPickerView 内存泄漏问题。

iphone - 如何从 NSMutableString 中取出 NSString?

iphone - 调整 subview 的大小

ios - UIKeyboardWillHideNotification 上无法识别的选择器

ios - Google+ iOS SDK 获取好友列表?

ios - Alamofire 文件上传出现错误 "JSON text did not start with array or object and option to allow fragments not set"

iphone - MDM:在配置文件中 checkin 3 次

ios - 在 iOS WebView 中获取 PDF 文档高度

iphone - 在 3D 中包裹/扭曲 CALayer/UIView(或 OpenGL)(iPhone)

iphone - 使用 avaudioplayer 从 iPod 音乐库播放 .m4a 文件