0

iOS4.3で正常に機能していたコードが少しあります。私はインターネットを調べました、私は他の人が私のために働いた答えなしで同じ問題を抱えているのを見つけました。録音はできると思いますが、再生できません。これが私のコードです:

DetailViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import <AudioToolbox/AudioServices.h>

@interface DetailViewController : UIViewController <UISplitViewControllerDelegate, AVAudioRecorderDelegate> {

id detailItem;
UILabel *detailDescriptionLabel;

IBOutlet UIButton *btnStart;
IBOutlet UIButton *btnPlay;

//Variables setup for access in the class:
NSURL * recordedTmpFile;
AVAudioRecorder * recorder;
BOOL toggle;
}

// Needed properties
@property (nonatomic, retain) IBOutlet UIButton *btnStart;
@property (nonatomic, retain) IBOutlet UIButton *btnPlay;
@property (strong, nonatomic) id detailItem;
@property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;

-(IBAction) start_button_pressed;
-(IBAction) play_button_pressed;
@end

DetailViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
toggle = YES;
btnPlay.hidden = YES;
NSError *error;

// Create the Audio Session
AVAudioSession *audioSession = [AVAudioSession sharedInstance];

// Set up the type of session
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

// Activate the session.
[audioSession setActive:YES error:&error];

[self configureView];    
}

-(IBAction) start_button_pressed{
if (toggle) {
    toggle = NO;
    [btnStart setTitle:@"Press to stop recording" forState:UIControlStateNormal];
    btnPlay.enabled = toggle;
    btnPlay.hidden = !toggle;
            NSError *error;

    NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];

    [recordSettings setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];

    [recordSettings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];

    [recordSettings setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];

    // Create a temporary files to save the recording. 
    recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];

    NSLog(@"The temporary file used is: %@", recordedTmpFile);

    recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSettings error:&error];

    [recorder setDelegate:self];

    [recorder prepareToRecord];
    [recorder record];
}
else {
    toggle = YES;
    [btnStart setTitle:@"Start recording" forState:UIControlStateNormal];
    btnPlay.hidden = !toggle;
    btnPlay.enabled = toggle;

    NSLog(@"Recording stopped and saved in file: %@", recordedTmpFile);
    [recorder stop];
}
}

-(IBAction) play_button_pressed{

NSError *error;
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];

   if (!error)
   {
       [avPlayer prepareToPlay];
       [avPlayer play];

       NSLog(@"File is playing");
   }

}

- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
                    successfully: (BOOL) flag {
     NSLog (@"audioPlayerDidFinishPlaying:successfully:");
}

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully: (BOOL)flag
{
    NSLog (@"audioRecorderDidFinishRecording:successfully:");
}

これが私のプログラムの実行中です:

2011-11-25 11:58:02.005 Bluetooth1 [897:707]使用される一時ファイルはfile:// localhost / private / var / mobile / Applications / D81023F8-C53D-4AC4-B1F7-14D66EB4844A / tmp/343915082005です。 caf 2011-11-25 11:58:05.956 Bluetooth1 [897:707]記録が停止し、ファイルに保存されました:file:// localhost / private / var / mobile / Applications / D81023F8-C53D-4AC4-B1F7-14D66EB4844A / tmp / 343915082005.caf 2011-11-25 11:58:05.998 Bluetooth1 [897:707] audioRecorderDidFinishRecording:successfully:2011-11-25 11:58:11.785 Bluetooth1 [897:707]ファイルが再生されています

何らかの理由で、関数audioPlayerDidFinishPlayingが呼び出されることはありません。しかし、何かが記録されているようです。今のところ、どの部分が機能していないのかわかりませんが、これはAVAudioPlayerと関係があると思います。

[編集]それはますます奇妙になっています。何かが録音されていることを確認したかったので、録音の長さを調べます。新しい再生機能は次のとおりです。

-(IBAction) play_button_pressed{

    NSError *error;
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: recordedTmpFile error:&error];

    if (!error)
    {
        AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:recordedTmpFile options:nil];
        CMTime audioDuration = audioAsset.duration;
        float audioDurationSeconds = CMTimeGetSeconds(audioDuration);

        [avPlayer prepareToPlay];
        [avPlayer play];

        NSString *something = [NSString stringWithFormat:@"%f",audioDurationSeconds]; 
        NSLog(@"File is playing: %@", something);
    }
    else
    {
         NSLog(@"Error playing.");
    }   
}

これで、レコードの長さが記録され、それは理にかなっています(10秒間記録すると、約10秒間表示されます)。ただし、これらのコード行を初めて配置したときに、フロートをNSStringに変換するのを忘れました。クラッシュしました...そしてアプリがサウンドを再生します...さまざまなテストの結果、アプリはサウンドを録音して再生できるが、クラッシュして録音されたサウンドを再生できると結論付けることができます。何が問題になるのかわかりません。AVPlayerは非同期であることがわかりましたが、それと関係がありますか?私は完全に迷子になっています...

4

3 に答える 3

1

urlpath を次のコードに置き換えます。

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(
         NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filepath = [documentsDirectory stringByAppendingPathComponent:@"urfile.xxx"];

NSURL *url = [NSURL fileURLWithPath:filepath];
于 2012-03-18T19:57:52.973 に答える
0

OK、あなた自身の質問に答えるのは本当にクールではありません。さらに、答えがきれいではないが機能している場合...記録したものを再生するために、次のコードブロックを使用しました。

    AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:recordedTmpFile options:nil];
    CMTime audioDuration = audioAsset.duration;
    float audioDurationSeconds = CMTimeGetSeconds(audioDuration);

    [avPlayer prepareToPlay];
    [avPlayer play];

    // Block for audioDurationSeconds seconds
    [NSThread sleepForTimeInterval:audioDurationSeconds];

記録されたファイルの長さを計算していて、この時間を待っています... 汚れていますが、うまく機能しています。さらに、別のスレッドで起動された場合、アプリケーションはブロックされません。

私は誰もが私が喜んでそれを取るだろう何かを持っています!

于 2011-12-09T11:43:06.290 に答える
0

ここで解決策を試してください:

録音と再生

于 2011-11-25T23:31:02.343 に答える