解决方案
10
// .h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface eyeMusicVC : UIViewController
{
AVAudioPlayer *myBackMusic;
}
//.m
//初始化音乐
//创建音乐文件路径
NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"myMusic" ofType:@"mp3"];
//判断文件能否存在
if ([[NSFileManager defaultManager] fileExistsAtPath:musicFilePath])
{
NSURL *musicURL = [NSURL fileURLWithPath:musicFilePath];
NSError *myError = nil;
//创建播放器
myBackMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:&myError];
if (myBackMusic == nil)
{
NSLog(@"error === %@",[myError description]);
}
[myBackMusic setVolume:1]; //设置音量大小
myBackMusic.numberOfLoops = -1;//设置音乐播放次数 -1为一直循环
[myBackMusic prepareToPlay];
}
//设置锁屏仍能继续播放
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[myBackMusic play]; //播放
[myBackMusic pause]; //暂停
30
- (void) QueryAllMusic
{
MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSLog(@"Logging items from a generic query...");
NSArray *itemsFromGenericQuery = [everything items];
NSLog(@"count = %lu", (unsigned long)itemsFromGenericQuery.count);
for (MPMediaItem *song in itemsFromGenericQuery)
{
NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
NSString *songArtist = [song valueForProperty:MPMediaItemPropertyArtist];
NSLog (@"Title:%@, Aritist:%@", songTitle, songArtist);
}
}