如何在Objective C中动态地将歌曲导入我们的应用程序

时间:2014-06-24 10:59:14

标签: ios iphone objective-c media-player avfoundation

我试过iPhone应用程序。在这个应用程序中,当我点击导入按钮时,我需要将所有歌曲导入到表格视图中。我不知道如何动态导入歌曲。请告诉我使用的框架是什么。 但我玩过本地保存的歌曲,如代码: - 使用这两个框架`AVFoundation.framework,MediaPlayer.framework

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

- (IBAction)playSound:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *filePath = [mainBundle pathForResource:@"drum" ofType:@"mp3"];

    NSData *fileData = [NSData dataWithContentsOfFile:filePath];

    NSError *error = nil;

    self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData
                                                     error:&error];
    [self.audioPlayer prepareToPlay];
    self.audioPlayer.numberOfLoops = -1;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)playSound:(id)sender {
    [self.audioPlayer play];

    UIView *wrapperView = [[UIView alloc] initWithFrame:CGRectMake(20, 120, 260, 20)];
    wrapperView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:wrapperView];

    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: wrapperView.bounds];
    [wrapperView addSubview:volumeView];
}

@end

此代码的输出是本地播放的歌曲,但是当我点击播放按钮时,它需要在tableView中导入10首歌曲,然后播放歌曲。 请给我任何想法。 提前致谢。

0 个答案:

没有答案