当前位置: 首页 > 知识库问答 >
问题:

Spotify Api:如何将Spotify“预览曲目”添加到Objective C中的最后一个fm应用程序中

吴唯
2023-03-14

我正在创建音乐应用程序。应用程序需要检索有关曲目、专辑和艺术家的信息。它当前显示的是atm。我用的是最后一台调频收音机。不过,我想添加Spotify,这样它就可以检索曲目进行播放。

我想添加一个曲目的预览,所以当用户点击应用程序上的曲目,他们点击播放,它播放歌曲(预览)。我想打开另一个xib,它将有这首歌的预览。我已经为我的应用程序提供了代码。截图来显示它是如何流动的。

这是我第一次申请,请帮忙。我不知道如何合并Spotify。非常感谢任何能帮忙的人。

最后一张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

音乐欣赏者

- (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];
    }
}


}

这些是截图。

搜索找到艺术家曲目时的屏幕截图用户单击搜索项目时的屏幕截图

共有1个答案

丌官远
2023-03-14

使用CocoaLibSpotify库将Spotify与iOS或macOS集成。

查看GitHub上CocoaLibSpotify项目中的“猜猜简介”示例项目。这个项目从歌曲列表中播放15秒的样本。你应该能够使它适应你的需要。

 类似资料:
  • 使用Spotify Web API的新endpoint/播放器,我们现在可以控制Spotify客户端的播放。endpointhttps://api.spotify.com/v1/me/player/play 我们可以添加一首要播放的歌曲。 我想知道是否有一种方法可以利用新的endpoint向玩家队列中添加一首歌。我计划写一个应用程序,让多个客人请求可以在派对上通过Spotify播放的歌曲。 有人有

  • 我目前正在尝试将Spotify应用程序迁移到api的新1.0版本。 我在搜索方面有问题。我想用搜索字符串搜索目录,并从搜索结果中获取所有曲目信息、艺术家姓名和专辑名称。但是,搜索结果中似乎不包括唱片集名称(仅URI:s)。 之后我是否必须异步加载所有结果曲目的所有唱片集名称?如果可能的话,我想避免这种情况。 我正在这样寻找

  • 我在跟踪AWS的文档,这里。除了apply插件之外,我可以将所有内容添加到项目的、和应用程序的中。当我补充:

  • 我已经在OpenShift上创建了Spring快速启动应用程序,它运行良好,显示了一个用户注册的网页,除了重新启动后它不记得新成员。我在想,这是因为我没有真正的数据库。所以我添加了一个PostgreSQL-9.2墨盒。所以我的问题是如何将postgresql准确地配置到OpenSift Spring应用程序中,以便这个web服务器能够存储重启后持续存在的数据。

  • 我想在我的应用程序中添加带有,但一旦我将插件添加到我的应用程序中。yaml,我的应用程序不再运行。下面是错误 失败:生成失败,出现异常。 > 错误:任务执行失败:app:transformDexArchiveWithExternalLibsDexMergerForDebug'。 通用域名格式。Android建设者德兴。DexArchiveMergerException:合并dex存档时出错:/Us

  • 我很难知道如何将我的JFXPanel(它是一个mp3播放器)集成到一个现有的Swing应用程序中,该应用程序是一个JFrame,其中添加了几个不同的JPanel。我对JavaFX是一个完全的noob,它与Swing非常不同。 我能够得到下面的代码来运行和做我想做的事情。它将播放mp3文件从一个目录,我有他们包含在。这是某人编写的示例中的代码。我的问题是,这段代码创建了JFrame,有main方法和