2016-05-14 7 views
2

私は音楽アプリを作成しています。アプリケーションはトラック、アルバム、アーティストの情報を取得する必要があります。最後のFMですが、Spotifyを追加してトラックを再生できるようにしたいのですがSpotify Api:Objective Cの最後のfmアプリにSpotify「プレビュートラック」を追加する方法

トラックのプレビューを追加すると、ユーザがトラックをクリックして再生をクリックすると、それは曲(プレビュー)を再生します。私は、その曲のプレビューを表示する別のxibを開くには、私のアプリケーションのコードを提供しています。 Spotifyをどのように組み込むのか分かりません。 uchは助けることができる人に感謝します。

最後のFmアルバム、トラック、アーティスト情報の検索。

#import "MusicSearchServices.h" 


@implementation MusicSearchServices 

@synthesize searchTerm; 
@synthesize delegate; 

@synthesize results; 

- (void)main { 
    NSString *api_key = @"API HERE"; 
    NSString *search_term = [searchTerm stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
    NSString *url = [NSString stringWithFormat:@"http://ws.audioscrobbler.com/2.0/?method=album.search&album=%@&api_key=%@&format=json", search_term, api_key]; 

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url] 
               cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; 

    if (responseData !=nil) { 
     NSError *error = nil; 
     NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 

     if (error) { 
      [delegate serviceFinished:self withError:YES]; 
     } else { 
      results = (NSArray *) [[[json valueForKey:@"results"] valueForKey:@"albummatches"] valueForKey:@"album"]; 
      [delegate serviceFinished:self withError:NO]; 
     } 

    } else { 
     [delegate serviceFinished:self withError:YES]; 
    } 
} 


@end 

詳細サービス

#import "DetailService.h" 
@implementation DetailService 
@synthesize musicname; 
@synthesize artistname; 
@synthesize delegate; 
@synthesize details; 


- (void)main { 
    NSString *api_key = @"Api"; 
    NSString *music_Id= [musicname stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
    NSString *artist_Id= [artistname stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
    NSString *url = [NSString stringWithFormat:@"http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=%@&artist=%@&album=%@&format=json", api_key,artist_Id,music_Id]; 
    NSLog(@"%@", url); 

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url] 
               cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; 

    if (responseData !=nil) { 
     NSError *error = nil; 
     NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 

     if (error) { 
      [delegate serviceFinished:self withError:YES]; 
     } else { 
      details = [json valueForKey:@"album"]; 
      [delegate serviceFinished:self withError:NO]; 
     } 

    } else { 
     [delegate serviceFinished:self withError:YES]; 
    } 
} 

@end 

MusicDeatilsViewer

- (void)viewWillAppear:(BOOL)animated{ 

    if ([self music] !=nil){ 
     [self setTitle:[[self music]valueForKey:@"name"]]; 
     [txtFilmText setText:[[self music] valueForKey:@"name"]]; 
     [txtFilmYear setText:[[[self music] valueForKey:@"artist"]description]]; 

    [txtReleaseDate setText:[[[self music] valueForKey:@"releasedate"]description]]; 
    [txtlis setText:[[self music] valueForKey:@"listeners"]]; 
    [count setText:[[self music] valueForKey:@"playcount"]]; 
    [txtalbumurl setText:[[self music] valueForKey:@"url"]]; 

    //Artist info web sevuces 
    [txtSummary setText:[[[[self music] valueForKey:@"bio"] valueForKey:@"summary"] description]]; 

    [txtSummary setText:@""]; 

    //Artist Details Animation 

    [_lbText setAlpha:0.0]; 
    [_lbText setCenter:CGPointMake(95, 85)]; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDelay:1]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationWillStartSelector:@selector(start)]; 

    [_lbText setAlpha:1.0]; 
    [_lbText setCenter:CGPointMake(180,85)]; 


    [UIView commitAnimations]; 


    //Images Animation 

    //Track Image 

    [imgFilm setAlpha:0.0]; 
    [imgFilm setCenter:CGPointMake(97, 200)]; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDelay:1]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationDelegate:self]; 

    [imgFilm setAlpha:1.0]; 
    [imgFilm setCenter:CGPointMake(97,200)]; 


    [UIView commitAnimations]; 


    //Artist Image 


    [imgArtist setAlpha:0.0]; 
    [imgArtist setCenter:CGPointMake(80, 390)]; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDelay:1]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationDelegate:self]; 

    [imgArtist setAlpha:1.0]; 
    [imgArtist setCenter:CGPointMake(80,410)]; 


    [UIView commitAnimations]; 


    //Details Animation 

    [txtSummary setAlpha:0.0]; 
    [txtSummary setCenter:CGPointMake(230, 410)]; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDelay:1]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationDelegate:self]; 

    [txtSummary setAlpha:1.0]; 
    [txtSummary setCenter:CGPointMake(230,410)]; 


    [UIView commitAnimations]; 

    [txtFilmYear setAlpha:0.0]; 
    [txtFilmYear setCenter:CGPointMake(240, 340)]; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDelay:1]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationDelegate:self]; 

    [txtFilmYear setAlpha:1.0]; 
    [txtFilmYear setCenter:CGPointMake(240,340)]; 


    [UIView commitAnimations]; 


    //imagefilm shadow effect 
    imgFilm.layer.shadowColor = [UIColor purpleColor].CGColor; 
    imgFilm.layer.shadowOffset = CGSizeMake(4, 4); 
    imgFilm.layer.shadowOpacity = 0.5; 
    imgFilm.layer.shadowRadius = 0.9; 
    imgFilm.clipsToBounds = NO; 

    //imageArtist shadow effect 
    imgArtist.layer.shadowColor = [UIColor purpleColor].CGColor; 
    imgArtist.layer.shadowOffset = CGSizeMake(4, 3); 
    imgArtist.layer.shadowOpacity = 0.9; 
    imgArtist.layer.shadowRadius = 1.0; 
    imgArtist.clipsToBounds = NO; 


    //ServiceQue 
    serviceQueue = [[NSOperationQueue alloc] init]; 
    [serviceQueue setMaxConcurrentOperationCount:1]; 

    ArtistInfo *aservice = [[ArtistInfo alloc] init]; 
    [aservice setArtistinfo:[[[self music] valueForKey:@"artist"] description]]; 
    [aservice setDelegate:self]; 
    [serviceQueue addOperation:aservice]; 

    DetailService *service = [[DetailService alloc]init]; 
    [service setMusicname:[[self music] valueForKey:@"name"]]; 
    [service setArtistname:[[[self music] valueForKey:@"artist"] description]]; 
    [service setDelegate:self]; 
    [serviceQueue addOperation:service]; 



    //check if album image is downloaded 
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@%@.png", docDir, [[self music] valueForKey:@"name"], [[self music] valueForKey:@"artist"]]; 
    NSLog(@"Show url_Img_FULL2: %@", pngFilePath); 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:pngFilePath]){ 
     UIImage *pic = [UIImage imageWithData:[NSData dataWithContentsOfFile:pngFilePath]]; 
     [imgFilm setImage:pic]; 
    } else { 
     DetailService *service = [[DetailService alloc]init]; 
     [service setMusicname:[[self music] valueForKey:@"name"]]; 
     [service setArtistname:[[[self music] valueForKey:@"artist"] description]]; 
     [service setDelegate:self]; 
     [serviceQueue addOperation:service]; 
     // NSLog(@"Show error1: %@", artistservice); 
    } 




    //check if image is downloaded 
    NSString *docDir2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; 
    NSString *pngFilePath2 = [NSString stringWithFormat:@"%@/%@%@.png", docDir2, [[self music] valueForKey:@"name"], [[self music] valueForKey:@"artist"]]; 
    NSLog(@"Show url_Img_FULL2: %@", pngFilePath2); 

    NSFileManager *fileManager2 = [NSFileManager defaultManager]; 
    if ([fileManager2 fileExistsAtPath:pngFilePath]){ 
     UIImage *pic = [UIImage imageWithData:[NSData dataWithContentsOfFile:pngFilePath]]; 
     [imgFilm setImage:pic]; 
     NSLog(@"Show url_Img_FULL: %@",pngFilePath); 
    } 

    //check if artist image shows 
    NSString *docDir1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *pngFilePath1 = [NSString stringWithFormat:@"%@/%@.png", docDir1, [[self music] valueForKey:@"artist"] ]; 
    NSLog(@"Show url_Img_FULL2: %@", pngFilePath); 

    NSFileManager *fileManager1 = [NSFileManager defaultManager]; 
    if ([fileManager1 fileExistsAtPath:pngFilePath1]){ 
     UIImage *pic = [UIImage imageWithData:[NSData dataWithContentsOfFile:pngFilePath1]]; 
     [imgArtist setImage:pic]; 
    } 

} 

if ([self music] != nil) { 
    [txtFilmText setText:[[self music] valueForKey:@"name"]]; 
    [txtFilmYear setText:[[[self music] valueForKey:@"artist"] description]]; 
    [txtReleaseDate setText:[[[self music] valueForKey:@"releasedate"] description]]; 
    [txtlis setText:[[[self music] valueForKey:@"listeners"] description]]; 
    [count setText:[[[self music] valueForKey:@"playcount"] description]]; 
    [txtalbumurl setText:[[[self music] valueForKey:@"url"] description]]; 
    [txtSummary setText:[[[self music] valueForKey:@"summary"] description]]; 



    //check if image is downloaded 
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; 
    NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@%@.png", docDir, [[self music] valueForKey:@"name"], [[self music] valueForKey:@"artist"]]; 
    NSLog(@"Show url_Img_FULL2: %@", pngFilePath); 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:pngFilePath]){ 
     UIImage *pic = [UIImage imageWithData:[NSData dataWithContentsOfFile:pngFilePath]]; 
     [imgFilm setImage:pic]; 
     NSLog(@"Show url_Img_FULL: %@",pngFilePath); 
    } 

} else { 

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@.png", docDir, [[self music] valueForKey:@"artist"] ]; 
    NSLog(@"Show url_Img_FULL2: %@", pngFilePath); 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:pngFilePath]){ 
     UIImage *pic = [UIImage imageWithData:[NSData dataWithContentsOfFile:pngFilePath]]; 
     [imgArtist setImage:pic]; 
    } 
} 


} 

これらはスクリーンショットです。

Screenshot when search finds the artist's track Screenshot when user clicks searched item

答えて

0

のiOSやMacOSで楽しんでるを統合するためのCocoaLibSpotifyライブラリを使用してください。

GitHubのCocoaLibSpotifyプロジェクトの "Guess The Intro"サンプルプロジェクトをチェックしてください。このプロジェクトは、曲のリストから15秒のサンプルを再生します。それをあなたのニーズに適応させることができるはずです。

関連する問題