Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.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
Objective c 在Watchkit OS 2中播放最后一段录音_Objective C_Nsurlconnection_Watchkit_Nsurl_Nsbundle - Fatal编程技术网

Objective c 在Watchkit OS 2中播放最后一段录音

Objective c 在Watchkit OS 2中播放最后一段录音,objective-c,nsurlconnection,watchkit,nsurl,nsbundle,Objective C,Nsurlconnection,Watchkit,Nsurl,Nsbundle,我使用苹果的示例代码录制和播放最后一段录音 但我不能播放最后一段录音 这是密码 - (IBAction)playLastRecording { // Present the media player controller for the last recorded URL. NSDictionary *options = @{ WKMediaPlayerControllerOptionsAutoplayKey : @Y

我使用苹果的示例代码录制和播放最后一段录音 但我不能播放最后一段录音

这是密码

- (IBAction)playLastRecording {

    // Present the media player controller for the last recorded URL.
    NSDictionary *options = @{
                              WKMediaPlayerControllerOptionsAutoplayKey : @YES
                              };

    [self presentMediaPlayerControllerWithURL:self.lastRecordingURL options:options completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable error) {
        if (!didPlayToEnd) {
            NSLog(@"The player did not play all the way to the end. The player only played until time - %.2f.", endTime);
        }

        if (error) {
            NSLog(@"There was an error with playback: %@.", error);
        }
    }];



}
这里是错误

我认为我们需要使用NSbundle和nsurl连接,但是如何使用呢

用于self.lastRecordingURL

请为此问题编写正确的代码


可选(Error Domain=com.apple.watchkit.errors Code=4“操作无法完成”UserInfo={NSLocalizedFailureReason=发生未知错误(1),NSUnderlyingError=0x17d9bf50{Error Domain=NSPOSIXErrorDomain Code=1“操作不允许”},NSLocalizedDescription=操作无法完成})

我有这个问题。确保音频数据以正确的文件扩展名正确保存到应用程序组

如果您试图录制音频文件,请使用以下代码

- (void)startRecording
{
    // Creating a path for saving the recorded audio file.
    // We have to write the files to the shared group folder, as this is the only place both the app and extension can see.
    NSURL *container = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP_IDENTIFIER];

    NSURL *outputURL = [container URLByAppendingPathComponent:@"AudioFile.m4a"];
    // Setting the recorder options.
    NSDictionary *dictMaxAudioRec = @{@"WKAudioRecorderControllerOptionsMaximumDurationKey":MAX_REC_DURATION};
    // Presenting the default audio recorder.
    [self presentAudioRecorderControllerWithOutputURL:outputURL preset:WKAudioRecorderPresetWideBandSpeech options:dictMaxAudioRec completion:^(BOOL didSave, NSError * error) {
        if(didSave)
        {
            // Successfully saved the file.
            NSURL *extensionDirectory = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
            NSUInteger timeAtRecording = (NSUInteger)[NSDate timeIntervalSinceReferenceDate];
            NSString *dirName = [NSString stringWithFormat:@"AudioFile%d/",timeAtRecording];
            NSURL *outputExtensionURL = [extensionDirectory URLByAppendingPathComponent:dirName];

            // Move the file to new directory.
            NSError *moveError;
            BOOL success = [[NSFileManager defaultManager] moveItemAtURL:outputURL toURL:outputExtensionURL error:&moveError];

            if (!success) {
                NSLog(@"Failed to move the outputURL to the extension's documents direcotry: %@", moveError);
            }
            else {
                NSData *audioData = [NSData dataWithContentsOfURL:outputExtensionURL];
                NSLog(@"Actual Audio Data length: %lu", (unsigned long)audioData.length);
                if(audioData.length)
                {
                    // We have a valid audio data,do what ever you want to do with this data 
                }
            }
        }
        else
        {
            // Something went wrong.
            NSLog(@"%s - %@",__PRETTY_FUNCTION__,error);
        }
    }];
}
- (void)playAudio
{
    // Playing the audio from the url using the default controller
    [self presentMediaPlayerControllerWithURL:[self getAudioUrl] options:nil completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * _Nullable error) {
        NSLog(@"Error = %@",error);
    }];
}
或者,如果您试图播放从其他来源下载或从配对手机传送的视频,请先将音频数据写入具有文件扩展名的应用程序组。以下代码可能对您有所帮助

- (void)writeAudioToAppGroupsWithData:(NSData *)audioData
{
    // Writing the audio data to the App Groups
    NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP_IDENTIFIER];
    containerURL = [containerURL URLByAppendingPathComponent:[NSString stringWithFormat:DIRECTORY_PATH]];
    [audioData writeToURL:containerURL atomically:YES];
}
在这种情况下,请确保您的目录路径是库/Caches/filename.extension。 例如:Library/Caches/Audio.mp3

要播放保存的音频,请使用以下代码

- (void)startRecording
{
    // Creating a path for saving the recorded audio file.
    // We have to write the files to the shared group folder, as this is the only place both the app and extension can see.
    NSURL *container = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP_IDENTIFIER];

    NSURL *outputURL = [container URLByAppendingPathComponent:@"AudioFile.m4a"];
    // Setting the recorder options.
    NSDictionary *dictMaxAudioRec = @{@"WKAudioRecorderControllerOptionsMaximumDurationKey":MAX_REC_DURATION};
    // Presenting the default audio recorder.
    [self presentAudioRecorderControllerWithOutputURL:outputURL preset:WKAudioRecorderPresetWideBandSpeech options:dictMaxAudioRec completion:^(BOOL didSave, NSError * error) {
        if(didSave)
        {
            // Successfully saved the file.
            NSURL *extensionDirectory = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
            NSUInteger timeAtRecording = (NSUInteger)[NSDate timeIntervalSinceReferenceDate];
            NSString *dirName = [NSString stringWithFormat:@"AudioFile%d/",timeAtRecording];
            NSURL *outputExtensionURL = [extensionDirectory URLByAppendingPathComponent:dirName];

            // Move the file to new directory.
            NSError *moveError;
            BOOL success = [[NSFileManager defaultManager] moveItemAtURL:outputURL toURL:outputExtensionURL error:&moveError];

            if (!success) {
                NSLog(@"Failed to move the outputURL to the extension's documents direcotry: %@", moveError);
            }
            else {
                NSData *audioData = [NSData dataWithContentsOfURL:outputExtensionURL];
                NSLog(@"Actual Audio Data length: %lu", (unsigned long)audioData.length);
                if(audioData.length)
                {
                    // We have a valid audio data,do what ever you want to do with this data 
                }
            }
        }
        else
        {
            // Something went wrong.
            NSLog(@"%s - %@",__PRETTY_FUNCTION__,error);
        }
    }];
}
- (void)playAudio
{
    // Playing the audio from the url using the default controller
    [self presentMediaPlayerControllerWithURL:[self getAudioUrl] options:nil completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * _Nullable error) {
        NSLog(@"Error = %@",error);
    }];
}
您可以从应用程序组获取音频url

- (NSURL *)getAudioUrl
{
    // Getting the audio url from the App Groups
    NSURL *container = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP_IDENTIFIER];
    NSURL *outputURL = [container URLByAppendingPathComponent:[NSString stringWithFormat:DIRECTORY_PATH]];
    return outputURL;
}

希望这能解决您的问题。

您解决了这个问题吗?没有。如果您有解决方案,请在这里@Vishnu Kumar分享。请回答(Audio Get)中最后一部分的一个问题。如何获取目录路径您可以将其定义为@“Library/Caches/Audio.mp3”i receiving the error Domain=com.apple.watchkit.errors Code=4“请求的URL在此服务器上找不到。”UserInfo={NSUnderlyingError=0x145b9d20{error Domain=NSPOSIXErrorDomain=2“没有这样的文件或目录”},NSLocalizedDescription=在此服务器上找不到请求的URL。}@Vishnu Kumar.S