MPMediaPickerController选择不播放的歌曲

时间:2012-03-22 05:52:49

标签: iphone objective-c ios

- (void) showMediaPicker
{ 
  MPMediaPickerController *picker =
  [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAnyAudio];

  [[picker view] setFrame:CGRectMake(0, 0, 320, 480)];

  picker.delegate      = self;
  picker.allowsPickingMultipleItems = YES;
  picker.prompt      = NSLocalizedString (@"AddSongsPrompt", @"Prompt to user to choose some songs to play");

 [self presentModalViewController:picker animated: YES];
 [picker release];
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker
didPickMediaItems: (MPMediaItemCollection *) collection 
{   
   [self dismissModalViewControllerAnimated: YES];
   [self playSelectedMediaCollection: collection];
}
- (void) playSelectedMediaCollection: (MPMediaItemCollection *) collection {

    if (collection.count == 1) {
    NSArray *items = collection.items;
    MPMediaItem *mediaItem =  [items objectAtIndex:0];
    if ([mediaItem isKindOfClass:[MPMediaItem class]]) {
        NSURL *url = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
        AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
        AVMutableAudioMix *fadeMix = [AVMutableAudioMix audioMix];
        AVMutableAudioMixInputParameters *params = [AVMutableAudioMixInputParameters audioMixInputParameters];
        [params setVolumeRampFromStartVolume:0 toEndVolume:1 timeRange:
         CMTimeRangeMake(CMTimeMakeWithSeconds(0, 1), CMTimeMakeWithSeconds(5,1))];
        [fadeMix setInputParameters:[NSArray arrayWithObject:params]];
        [playerItem setAudioMix:fadeMix];
        AVPlayer *newAvPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
        [newAvPlayer play];
     }
   }
}

此代码构建时错误是架构i3​​86的未定义符号:   “_CMTimeMakeWithSeconds”,引自:        - SongFileViewVC.o中的[SongFileViewVC playSelectedMediaCollection:]   “_CMTimeRangeMake”,引自:        - SongFileViewVC.o中的[SongFileViewVC playSelectedMediaCollection:] ld:找不到架构i386的符号 clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

我是如何解决的。

2 个答案:

答案 0 :(得分:4)

尝试将CoreMedia框架添加到项目中,或者确保在必要时正确添加和导入。

答案 1 :(得分:1)

我是用一种简单的方式做到的

添加MediaPlayer.framework

你的.h

#import <MediaPlayer/MPMusicPlayerController.h>
#import <MediaPlayer/MPMediaPickerController.h>

@interface libraryAccessViewController : UIViewController <MPMediaPickerControllerDelegate>
{
    MPMusicPlayerController *player;
    MPMediaPickerController *picker;
}

- (IBAction)pickMedia:(id)sender;
- (IBAction)playMedia:(id)sender;
- (IBAction)stopMedia:(id)sender;

你的.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    player=[MPMusicPlayerController iPodMusicPlayer];

    picker=[[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];

    [picker setDelegate:self];

    picker.prompt=@"Add an audio to your MyVision";
}

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{    
   [player setQueueWithItemCollection:mediaItemCollection];

   [self dismissModalViewControllerAnimated:YES];
}

- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
   [self dismissModalViewControllerAnimated:YES];
}

//bind below actions with three UIButtons
- (IBAction)pickMedia:(id)sender
{
   [self presentModalViewController:picker animated:YES];
}

- (IBAction)playMedia:(id)sender
{
   [player play];
}

- (IBAction)stopMedia:(id)sender
{
   [player stop];
}