24

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

どんな助けでも大歓迎です!

4

7 に答える 7

54

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>
于 2012-07-17T18:16:42.053 に答える
30

「AVAudioPlayer」への強力な参照を保存する必要があります

@property (strong) AVAudioPlayer *audioPlayer;

また、オーディオ ファイルが次のバンドル リソースにあることが確実な場合は、再生する必要があります。

プロジェクト>ビルドフェーズ>コピー

于 2013-09-17T17:42:58.293 に答える
6

最初にこのフレームワークをファイルにインポートします

#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];
于 2013-06-20T12:26:40.263 に答える
0

システム サウンド ID を生成する場合、以下のコードを使用してバンドル オーディオ ファイルを再生するには

for(int i = 0 ;i< 5;i++)
{
    SystemSoundID   soundFileObject;
    NSString *audioFileName = [NSString stringWithFormat:@"Alarm%d",i+1];

    NSURL *tapSound   = [[NSBundle mainBundle] URLForResource: audioFileName
                                            withExtension: @"caf"];

    // Store the URL as a CFURLRef instance
    CFURLRef soundFileURLRef = (__bridge CFURLRef) tapSound;

    // Create a system sound object representing the sound file.
    AudioServicesCreateSystemSoundID (

                                  soundFileURLRef,
                                  &soundFileObject
                                  );
    [alarmToneIdList addObject:[NSNumber numberWithUnsignedLong:soundFileObject]];
}

以下のコードを使用してサウンドを再生できます

AudioServicesPlaySystemSound([[alarmToneIdList objectAtIndex:row]unsignedLongValue]);

以下のフレームワークを実現するには、Xcode を追加する必要があります

オーディオツールボックス

最後に、以下のヘッダー ファイルをコントローラーにインポートする必要があります

#import AudioToolbox/AudioToolbox.h
#import AudioToolbox/AudioServices.h
于 2014-11-07T19:43:37.297 に答える