iphone - iPhone,音频MP3或M4A音频文件不播放声音

标签 iphone audio ios7

我正在尝试在对象didSelectedIndex UITableViewController中播放声音方法

音频文件是扩展名为m4a的文件,但我也尝试使用mp3文件,结果没有改变。

下面,我发布在TableViewController方法中实现的代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *soundFilePath = [NSString stringWithFormat:@"%@/testaudio.m4a", [[NSBundle mainBundle] resourcePath]];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
    NSLog(@"soundFileURL %@",soundFileURL);
    NSError *error;
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];

    if(!player){
        NSLog(@"ERRORE: %@",error);
    }
    [player setDelegate:self];
    [player prepareToPlay];
    [player play];

}

我也直接在设备上尝试过,它仍然无法播放任何声音。

我还添加了一个AVSession类,如下所示:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];

结果没有改变,有人知道是什么问题吗?

最佳答案

试试这个(经过测试):

AVAudioPlayer中为.m对象设置属性:

@property(strong, nonatomic) AVAudioPlayer *player;

然后将其添加到您的:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *soundFilePath;

    if ((soundFilePath = [[NSBundle mainBundle] pathForResource:@"testaudio" ofType:@"m4a"]))
    {
        NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
        NSError *error;
        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];

        if(self.player)
        {
            [self.player setDelegate:self];
            [self.player prepareToPlay];
            [self.player play];
        }
        else
        {
            NSLog(@"ERRORE: %@", error);
        }
    }
}

在这种情况下,您不需要AVAudioSession

关于iphone - iPhone,音频MP3或M4A音频文件不播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21174047/

相关文章:

iphone - (iPhone SDK) 如何通过标签id修改UIButton?

javascript - scriptNode.onaudioprocess 已弃用,还有其他选择吗?

ios - Avspeechsynthesizer 语音音频文件,UILocalNotification

python - 无法在Python中使用AudioTools打开mp3文件

ios7 - IOS 7 sizeWithFont 已弃用

ios - 无法调用电话

ios - 使用 iOS 7 的自动布局的一些具体帮助

iphone - 根据用户所在国家/地区具有不同功能的应用程序

iphone - 扩展 NKIssue - NewsStandKit (iOS)

ios - spriteKit 提高性能