how to trim or crop music from itunes library

2019-09-05 13:54发布

问题:

i would like to grab music from user's library for audio music fingering. what i required are just 30 sec of each songs. how do i go about it ? i do not need to save the audio but just using it.

iOS Audio Trimming

Trim audio with iOS

are the above 2 links the way to go ?

thanks for reading and appreciated any comments.

cheers

回答1:

To access the music that a user already has in his device library, you would use the MPMediaItemCollection class. Sample code on using this (although slightly old and not using ARC) can also be found in the Apple Docs in the "Add Music" sample.

I am not sure what Audio Player you are using, but if you would like to seek to a particular point in a song or audio file, you can do so by using the currentTime property in the AVAudioPlayer class, (noted in the Apple Docs).

To implement this, you would do something as simple as:

yourAudioPlayer.currentTime = 60;

This will jump the song to the 1 minute mark. I am not 100% sure on how to do this before the song plays, but I would put it in the Play IBAction, before it starts playing:

- (IBAction) playOrPause: (id) sender {

    if (self.player.playing) {

        [self.button setTitle: @"Play" forState: UIControlStateHighlighted];
        [self.button setTitle: @"Play" forState: UIControlStateNormal];
        [self.player pause];

    } else {
        [self.button setTitle: @"Pause" forState: UIControlStateHighlighted];
        [self.button setTitle: @"Pause" forState: UIControlStateNormal];

        [self.player.currentTime = 60];  //Set the seeker here

        [self.player play];
    }

} 

To stop the playback after a certain amount of time (you say 30 sec) you could use an NSTimer that will call stop on the player after a 30 second delay. Something like:

    [NSTimer 
     scheduledTimerWithTimeInterval:30.0
     target:self.player
     selector:@selector(stop)
     userInfo:nil
     repeats:NO];

If you put that in the IBAction that you use to play, that should do it!