2016-04-09 13 views
1

einen MP3 vom Telefon gesendet:Audiodatei in WatchKit OS2 Wiedergabe

NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 
NSString *documentsDirectory = [pathArray objectAtIndex:0]; 

NSString *yourSoundPath = [documentsDirectory stringByAppendingPathComponent:@"MyMusic.mp3"]; 
NSURL *url = [NSURL fileURLWithPath:yourSoundPath isDirectory:NO]; 
[self.session transferFile:url metadata:nil]; 

Wie ich versucht habe, um die Datei auf die Uhr zu empfangen und spielen:

-(void)session:(WCSession *)session didReceiveFile:(WCSessionFile *)file { 
    dispatch_async(dispatch_get_main_queue(), ^{ 

     NSLog(@"URL%@" , file.fileURL.filePathURL); 

     NSDictionary *options = @{ 
            WKMediaPlayerControllerOptionsAutoplayKey : @YES 
            }; 

     [self presentMediaPlayerControllerWithURL:file.fileURL.filePathURL 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); 
      } 
     }]; 
    }); 
} 

Diese die URL der Datei:

file:///var/mobile/Containers/Data/PluginKitPlugin/-------/Documents/Inbox/com.apple.watchconnectivity/------/Files/----/MyMusic.mp3

Dies ist der Fehler:

There was an error with playback: Error Domain=com.apple.watchkit.errors Code=4 "The requested URL was not found on this server." UserInfo={NSUnderlyingError=0x16d4fa10 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}, NSLocalizedDescription=The requested URL was not found on this server.}.

Wie kann ich diese Datei in watchOS 2 abspielen?

Antwort

3

Damit die Überwachungs-App die Audiodatei abspielen kann, müssen Sie sie in einen freigegebenen App-Gruppencontainer verschieben.

Sie müssen eine freigegebene App-Gruppe zwischen der WatchKit-Erweiterung und der WatchKit-App aktivieren.

Wenn Sie die Datei nicht im freigegebenen Container ablegen, wird erwartet, dass die Audiowiedergabe fehlschlägt. Sie sollten auch erst nach dem Verschieben der Datei aus der Rückrufwarteschlange entfernt werden, da WCSession die Datei löscht, wenn der Rückruf zurückgegeben wird.

Hier ist eine tutorial, die erläutert, wie es geht, und some information von Apple zu diesem Thema.

Edit: Hinzugefügt Beispiel

So etwas sollte funktionieren, wenn Sie die App Gruppencontainer für Ihre WatchKit App und Erweiterung aktiviert haben:

- (void)session:(WCSession * __nonnull)session didReceiveFile:(WCSessionFile * __nonnull)file 
{ 
    NSLog(@"received file %@, metadata: %@", file, file.metadata); 

    NSFileManager *fm = [NSFileManager defaultManager]; 
    NSError *error = nil; 
    NSURL *containerURL = [fm containerURLForSecurityApplicationGroupIdentifier:@"<YOUR APP GROUP HERE>"]; 
    NSString *documentsPath = [containerURL path]; 
    NSString *dateString = [[[NSDate date] description] stringByAppendingString:@"-"]; 
    NSString *fileNameWithDate = [dateString stringByAppendingString:file.fileURL.lastPathComponent]; 
    documentsPath = [documentsPath stringByAppendingPathComponent:fileNameWithDate]; 
    if ([fm moveItemAtPath:[file.fileURL.path stringByExpandingTildeInPath] toPath:documentsPath error:&error]) { 
     if ([fm fileExistsAtPath:documentsPath isDirectory:nil]) { 
      NSLog(@"moved file %@ to %@", file.fileURL.path, documentsPath); 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       NSDictionary *options = @{ WKMediaPlayerControllerOptionsAutoplayKey : @YES }; 
       [self presentMediaPlayerControllerWithURL:[NSURL fileURLWithPath:documentsPath] options:options completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable playAudioError) { 
        if (!didPlayToEnd) { 
         NSLog(@"The player did not play all the way to the end. The player only played until time - %.2f.", endTime); 
        } 

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


     } else { 
      NSLog(@"failed to confirm move of file %@ to %@ (%@)", file.fileURL.path, documentsPath, error); 
     } 
    } else { 
     NSLog(@"failed to move file %@ to %@ (%@)", file.fileURL.path, documentsPath, error); 
    } 
} 
+1

hatte ich nicht realisiert, dass App-Gruppen geteilt waren notwendig auf der Wache. Danke für eine sehr lehrreiche Antwort! –