Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用AVAudioPlayer播放音频时出错_Iphone_Cocoa Touch_Ios4_Avaudioplayer - Fatal编程技术网

Iphone 使用AVAudioPlayer播放音频时出错

Iphone 使用AVAudioPlayer播放音频时出错,iphone,cocoa-touch,ios4,avaudioplayer,Iphone,Cocoa Touch,Ios4,Avaudioplayer,我在播放音频时出错。 这就是我不播放录音的原因吗 Error '!obj' trying to fetch default input device's sample rate Error getting audio input device sample rate: '!obj' - (void)record{ AVAudioSession *audioSession = [AVAudioSession sharedInstance]; NSError *e

我在播放音频时出错。 这就是我不播放录音的原因吗

Error '!obj' trying to fetch default input device's sample rate
    Error getting audio input device sample rate: '!obj'

- (void)record{    

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *err = nil;
    [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
    if(err){
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }
    [audioSession setActive:YES error:&err];
    err = nil;
    if(err){
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }

    recordSetting = [[NSMutableDictionary alloc] init];

    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

    [recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

    NSString *fileName = nil;
    if(![myItem audioURL]){
        fileName = [fileFormatter stringFromDate:[NSDate date]];
        [myItem setAudioURL:fileName];
        NSLog(@"file name :%@",fileName);
        NSLog(@"audio url: %@",[myItem audioURL]);

    } else{
        fileName = [myItem audioURL];}

    NSString *pathString = [NSString stringWithFormat:@"%@/%@", [delegate applicationDocumentsDirectory], fileName];

    recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:pathString] settings:recordSetting error:&err];

    // Create a new dated file
    //NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
   // NSString *caldate = [now description];
  //  recorderFilePath = [[NSString stringWithFormat:@"%@/%@.caf", DOCUMENTS_FOLDER, caldate] retain];

 //   NSURL *url = [NSURL fileURLWithPath:recorderFilePath];


    if(!recorder){
        NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        UIAlertView *alert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: [err localizedDescription]
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }

    //prepare to record
    [recorder setDelegate:self];
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;

    BOOL audioHWAvailable = audioSession.inputIsAvailable;
    if (! audioHWAvailable) {
        UIAlertView *cantRecordAlert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: @"Audio input hardware not available"
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [cantRecordAlert show];
        [cantRecordAlert release]; 
        return;
    }

    // start recording
    [recorder recordForDuration:(NSTimeInterval) 10];


        recording = YES;

    [pauseButton setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];
    [pauseButton setEnabled:YES];
    [playButton setEnabled:NO];
    [recordButton setEnabled:NO];


    [self beginAnimation];
    //    NSError *error=nil;
    //    if(![context save: &error])
    //    {
    //        //Couldn't save
    //    }
}





- (void)play{         
            NSString *fileName = nil;
        if(![myItem audioURL]){
            return;
        } else
            fileName = [myItem audioURL];
        NSLog(@"file name :%@",fileName);


        NSString *pathString = [NSString stringWithFormat:@"%@/%@", [delegate applicationDocumentsDirectory], fileName];
        NSLog(@"Path : %@",pathString);


        player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL UrlwithString:pathString] error:nil] ;


    //    if (player==nil) {
    //        NSLog(@"%@",[error description]);
    //    }
    //    else
    //    {
            player.volume=1.0;
            NSLog(@"about to play");
            [player prepareToPlay];
            [player play];
            NSLog(@"player play");
            [player setDelegate:self];
            playing = YES;  

            [recordButton setEnabled:NO];
            [pauseButton setEnabled:YES];
            [playButton setEnabled:NO];


            [self beginAnimation];
       // }
    }
试试这个代码

NSURL *url=[NSURL URLWithString:appDelegate.songName];
            NSLog(@"SongURL :%@",url);

            NSData *soundData = [NSData dataWithContentsOfURL:url];


            appDelegate.player = [[AVAudioPlayer alloc] initWithData:soundData error: nil];         
            appDelegate.player.delegate = self;
            appDelegate.player.volume=1.0;  

            // Play the audio
            [appDelegate.player prepareToPlay];             
            [appDelegate.player play];  
这可能对你有帮助


还可以添加框架和头文件(AVFoundation.framework和AVToolbox.framework)

您可以使用以下代码播放声音:

 NSString *path = [[NSBundle mainBundle] pathForResource:@"adriantnt_release_click" ofType:@"mp3"];

NSURL *url = [NSURL URLWithString:path];

NSData *data = [NSData dataWithContentsOfURL:url];


AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithData:data error: nil];
theAudio.delegate = self;
theAudio.volume=1.0;

// Play the audio
[theAudio prepareToPlay];
[theAudio play];

@希娜·戴夫:试着分享你的代码。这样只有我们才能进一步帮助你。有必要把appDelegate带到这里吗?