播放音频的iOS的Objective-C [英] Play Audio iOS Objective-C

查看:325
本文介绍了播放音频的iOS的Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一个音频文件在我的iOS应用程序播放。这是我目前的code

I'm trying to get an audio file playing in my iOS app. This is my current code

NSString *soundFilePath = [NSString stringWithFormat:@"%@/test.m4a", [[NSBundle mainBundle] resourcePath]];

NSLog(@"%@",soundFilePath);
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

[audioPlayer setVolume:1.0];

audioPlayer.delegate = self;

[audioPlayer stop];
[audioPlayer setCurrentTime:0];
[audioPlayer play];

我已经被检查了一堆其他职位,但他们一般都做同样的事情。我没有得到一个错误,但我没有听到任何音频播放在模拟器或设备。

I've being checking a bunch of other posts but they all generally do the same thing. I'm not getting an error, but I don't hear any audio playing on the simulator or device.

这是我得到的仿真

/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/long-numbered-thing/BookAdventure.app/test.m4a

任何帮助AP preciated!

Any help is appreciated!

推荐答案

也许你应该尝试使用一个NSBundle 方法 pathForResource:ofType:方法来创建文件路径。

Maybe you should try using the NSBundle method pathForResource:ofType: method to create the path to the file.

以下code应该可以成功播放音频文件。我只用 MP3 使用它,但我想这也应该与 M4A 工作。如果这个code不起作用,你可以尝试改变音频文件的格式。对于这个code,音频文件位于主项目目录。

The following code should successfully play an audio file. I have only used it with an mp3, but I imagine it should also work with m4a. If this code does not work, you could try changing the format of the audio file. For this code, the audio file is located in the main project directory.

/* Use this code to play an audio file */
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"test" 
                                                          ofType:@"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL 
                                                               error:nil];
player.numberOfLoops = -1; //Infinite

[player play];


修改

好了,试试这个:


EDIT

Ok, so try this:

NSString *soundFilePath = [NSString stringWithFormat:@"%@/test.m4a", 
   [[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL 
                                                               error:nil];
player.numberOfLoops = -1; //Infinite

[player play];

此外,还要确保你做正确的进口:

Also, make sure you do the proper imports:

#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>

这篇关于播放音频的iOS的Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆