OGeek|极客世界-中国程序员成长平台

标题: ios - 如何使用 MPMediaPickerController 使用单个 UIButton 单击选择单个歌曲 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:38
标题: ios - 如何使用 MPMediaPickerController 使用单个 UIButton 单击选择单个歌曲

我有四个 UIButton 可以使用 MPMediaPickerController 从库中选择歌曲。 现在我想要的是 当我按下 UIButton1 时,它应该打开选择歌曲 1 的选择器。 当我按下 UIButton2 时,它应该打开选择歌曲 2 的选择器。 UIButton3 和 UIButton4 也一样。

我可以在哪里更改以实现上述内容。

这是我当前的代码。

-(IBAction)SelectSongClickid)sender
{
    MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
    [picker setDelegate:self];
    [picker setAllowsPickingMultipleItems: NO];

if((UIButton*)sender == btnSelect1)
{
    [playerView1 removeFromSuperview];
    [player pause];
    picker.title = @"1";
}
else if ((UIButton*)sender == btnSelect2)
{
    NSLog(@"select 2 selected");

    [playerView2 removeFromSuperview];
    [player pause];
    picker.title = @"2";
}
else if ((UIButton*)sender == btnSelect3)
{
    NSLog(@"select 3 selected");
    [playerView3 removeFromSuperview];
    [player pause];
    picker.title = @"3";
}
else
{
    NSLog(@"select 4 selected");
    [playerView4 removeFromSuperview];
    [player pause];
    picker.title = @"4";
}

[self presentViewController:picker animated:YES completion:NULL];

}

- (void)mediaPickerMPMediaPickerController *) mediaPicker didPickMediaItemsMPMediaItemCollection *) 集合 { [ self dismissViewControllerAnimated:YES完成:NULL]; MPMediaItem *item = [[收藏项] objectAtIndex:0]; NSString *title = [item valueForProperty:MPMediaItemPropertyTitle]; url = [item valueForProperty:MPMediaItemPropertyAssetURL]; [self playURL:url withPickerTag:mediaPicker.title]; NSLog(@"URL 1 : %@",url); self.sendAudio = 网址; }



Best Answer-推荐答案


如果我正确理解了您的问题,那么您想更改方法:- (void)mediaPickerMPMediaPickerController *) mediaPicker didPickMediaItemsMPMediaItemCollection *) collection 这样您就可以从中获取选定的按钮索引?

@interface ViewController 上方创建枚举:

typedef enum {
    WaveFormType_1, //you can name these enums as you want
    WaveFormType_2,
    WaveFormType_3,
    WaveFormType_4
}WaveFormType;

然后创建一个变量 WaveFormType waveType; 并根据点击的按钮在 -(IBAction)SelectSongClickid)sender 中为其赋值。然后使用这个值。

- (void)mediaPickerMPMediaPickerController *) mediaPicker didPickMediaItemsMPMediaItemCollection *) collection
{   
    [self dismissViewControllerAnimated:YES completion:NULL];

    MPMediaItem *item = [[collection items] objectAtIndex:0];
    [self.playItem:item]
}

-(void)playItemMPMediaItem *)item{
     NSString *title = [item valueForProperty:MPMediaItemPropertyTitle];
     NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];

     if (self.waveType == WaveFormType_1){

         // play with the first waveform type
         // label.text = title ..

     }else if(self.waveType == WaveFormType_2){

         // play with the second waveform type
         // label.text = title ..
   }
   // an so on
}

关于ios - 如何使用 MPMediaPickerController 使用单个 UIButton 单击选择单个歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31871038/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4