xcode 如何播放音频文件ios

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12684176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:51:23  来源:igfitidea点击:

How to play audio file ios

iosxcodeavfoundationavaudioplayer

提问by Camus

I am trying to play an audio file but I can get it working. I imported the AVFoundationframework.

我正在尝试播放音频文件,但我可以让它工作。我导入了AVFoundation框架。

Here is the code:

这是代码:

NSString *fileName = [[NSBundle mainBundle] pathForResource:@"Alarm" ofType:@"caf"];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:fileName];
    NSLog(@"Test: %@ ", url);

    AVAudioPlayer *audioFile = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
    audioFile.delegate = self;
    audioFile.volume = 1;

    [audioFile play];

I am receiving an error nil string parameter

我收到一个错误 nil string parameter

I copied the file to the supporting files folder so the file is there.

我将文件复制到支持文件文件夹,所以文件在那里。

Can you guys help me?

你们能帮帮我吗?

Thanks

谢谢

回答by Rizwan Shah

Just follow two simple steps

只需遵循两个简单的步骤

Step1:

第1步:

UIViewController.h

UIViewController.h

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

@property(nonatomic, retain) AVAudioPlayer *player;

Step 2:

第2步:

UIViewController.m

UIViewController.m

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sound" 
                                                      ofType:@"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

 _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[_player setVolume:1.0];
[_player play];

Just make sure AVAudioPlayer property must be nonatomic and retain.

只需确保 AVAudioPlayer 属性必须是非原子的并保留。

回答by msweet168

Make sure you import your audiotoolbox framework and then write the following. This is what I use to play simple sounds

确保导入您的 audiotoolbox 框架,然后编写以下内容。这是我用来播放简单声音的东西

#import <AudioToolbox/AudioToolbox.h>

 CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Your sound here", CFSTR ("mp3"), NULL);

UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);

If you have any confusion with this let me know.

如果您对此有任何困惑,请告诉我。

回答by martin's

I use something like this when the file is in a specific folder:

当文件位于特定文件夹中时,我使用类似的东西:

// Build URL to the sound file we are going to play
NSURL *sound_file = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:sound_file_name ofType:@"mp3" inDirectory:directory]];   

// Play it
audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL:sound_file error:nil];
audio_player.delegate = self;
[audio_player play];
[sound_file release];

sound_file_nameis the file name name without the extension: @"success"

sound_file_name是不带扩展名的文件名: @"success"

directoryis something like: @"media/sound_effects"

directory是这样的: @"media/sound_effects"

回答by Mike Jaoudi

Add this and see if the program recognizes the file exists.

添加它并查看程序是否识别该文件存在。

NSLog(@"File Exists:%d",[[NSFileManager defaultManager] fileExistsAtPath:fileName]);

NSLog(@"File Exists:%d",[[NSFileManager defaultManager] fileExistsAtPath:fileName]);

It will print a 1 if it exists. If not make sure you have the proper filename (case matters).

如果存在,它将打印 1。如果不能确保您有正确的文件名(大小写很重要)。