Lag*_*o87 23 audio xcode objective-c ios
我正在尝试在我的iOS应用程序中播放音频文件.这是我目前的代码
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];
我正在检查一堆其他帖子,但他们通常都做同样的事情.我没有收到错误,但我没有听到模拟器或设备上播放任何音频.
这是我在模拟器上获得音频文件的路径
/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/long-numbered-thing/BookAdventure.app/test.m4a
任何帮助表示赞赏!
bdd*_*ken 53
也许您应该尝试使用NSBundle方法pathForResource:ofType:方法来创建文件的路径.  
以下代码应成功播放音频文件.我只用过它mp3,但我想它也应该可以使用m4a.如果此代码不起作用,您可以尝试更改音频文件的格式.对于此代码,音频文件位于主项目目录中.
/* 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];
好的,试试这个:
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];
另外,请确保执行正确的导入:
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
小智 29
您需要存储对'AVAudioPlayer' 的强引用
@property (strong) AVAudioPlayer *audioPlayer;
如果您确定您的音频文件位于以下Bundle Resources中,则应该播放.
项目>构建阶段>复制
Sal*_*lim 20
播放扩展名为.caf,.m4a,.mp4,.mp3,.wav,.aif的文件
从GitHub下载以下两个文件
SoundManager.h
SoundManager.m
将这些文件添加到项目中
还将音频文件添加到资源文件夹(示例文件)
mysound.mp3, song.mp3
在所需文件中导入头文件
#import "SoundManager.h"
并在- (void)viewDidLoad中添加以下两行
[SoundManager sharedManager].allowsBackgroundMusic = YES;
[[SoundManager sharedManager] prepareToPlay];
使用以下行播放声音(mysound.mp3)
[[SoundManager sharedManager] playSound:@"mysound" looping:NO];
使用以下行停止声音(mysound.mp3)
[[SoundManager sharedManager] stopSound:@"mysound"];
你也可以播放音乐(song.mp3)
[[SoundManager sharedManager] playMusic:@"song" looping:YES];
并且可以将音乐停止为
[[SoundManager sharedManager] stopMusic];
完整的文档在GitHub上
首先将此框架导入到您的文件中
#import <AVFoundation/AVFoundation.h>
然后声明一个实例AVAudioPlayer。
AVAudioPlayer *audioPlayer;
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Name of your audio file" 
                                                          ofType:@"type of your audio file example: mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];