gpt4 book ai didi

ios - 从nsbundle播放mp3

转载 作者:行者123 更新时间:2023-12-01 18:30:23 24 4
gpt4 key购买 nike

设置

我将mp3放入捆绑包中只是为了进行测试,然后将它们下载到documents目录中。但是现在我要解决这个问题。

我找到了一些教程,但没有满足我的需求。以下内容绝对是几种解决方案的混合,可能不是最佳方法。

问题

如何从捆绑中取出歌曲并在应用程序中播放?

//Create an instance of MPMusicPlayerController
MPMusicPlayerController* myPlayer = [MPMusicPlayerController applicationMusicPlayer];

//Read the song off the bundle.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"LittleMoments" ofType:@"mp3"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];

if (myData) {

//This wont work for me because i don't want to query songs from the iPod,
i want to play songs out of the Bundle.

//Create a query that will return all songs by The Beatles grouped by album
MPMediaQuery* query = [MPMediaQuery songsQuery];
[query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:@"The Beatles" forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonEqualTo]];
[query setGroupingType:MPMediaGroupingAlbum];

//Pass the query to the player
[myPlayer setQueueWithQuery:query];
/////**** how can i get nsdata into MyPlayer?*****//////

//Start playing and set a label text to the name and image to the cover art of the song that is playing
[myPlayer play];

}

最佳答案

尝试使用AVAudioPlayer播放文件

AVAudioPlayer *audioPlayer = [(AVAudioPlayer*)[AVAudioPlayer alloc] initWithData:myData error:nil];

audioPlayer.delegate = self;
[audioPlayer play];

关于ios - 从nsbundle播放mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9625155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com