2013-02-14 43 views

回答

2

這適用於任何試圖以不同於系統音量的音量播放iPod音樂庫中的音樂/播放列表的人。在那裏有幾個帖子說[MPMusicPlayerController applicationMusicPlayer]可以做到這一點,但我發現,任何時候我改變applicationMusicPlayer的音量,系統音量也會改變。

使用AVAudioPlayer類還有一種更加複雜的播放音樂的方法,但它需要您將音樂文件從ipod庫複製到應用程序包,並且在播放動態內容時會變得棘手,如用戶生成播放列表。這種技術確實可以讓你訪問字節,如果你想對數據進行處理(如DJ應用程序),這種方法也是可行的。鏈接到該解決方案HERE

我使用的解決方案使用AVPlayer類,有幾個很好的職位關於如何做到這一點。這篇文章基本上是我在Stackoverflow和其他地方找到的幾種不同解決方案的組合。

我有以下框架鏈接:

AVFoundation
MediaPlayer的
AudioToolbox
CoreAudio的
CoreMedia

(我不知道如果所有這些都是重要的,但是這就是我。我也實現了一些OpenAL的東西,我沒有在下面的代碼中顯示)

// Presumably in your SoundManage.m file (or whatever you call it) ... 

#import <CoreAudio/CoreAudioTypes.h> 
#import <AudioToolbox/AudioToolbox.h> 

@interface SoundManager() 

@property (retain, nonatomic) AVPlayer* audioPlayer; 
@property (retain, nonatomic) AVPlayerItem* currentItem; 

@property (retain, nonatomic) MPMediaItemCollection* currentPlaylist; 
@property (retain, nonatomic) MPMediaItem* currentTrack; 
@property (assign, nonatomic) MPMusicPlaybackState currentPlaybackState; 

@end 


@implementation SoundManager 

@synthesize audioPlayer; 
@synthesize currentItem = m_currentItem; 

@synthesize currentPlaylist; 
@synthesize currentTrack; 
@synthesize currentPlaybackState; 

- (id) init 
{ 
    ... 

    //Define an AVPlayer instance 
    AVPlayer* tempPlayer = [[AVPlayer alloc] init]; 
    self.audioPlayer = tempPlayer; 
    [tempPlayer release]; 

    ... 

    //load the playlist you want to play 
    MPMediaItemCollection* playlist = [self getPlaylistWithName: @"emo-pop-unicorn-blood-rage-mix-to-the-max"]; 
    if(playlist) 
     [self loadPlaylist: playlist]; 

    ... 

    //initialize the playback state 
    self.currentPlaybackState = MPMusicPlaybackStateStopped; 


    //start the music playing 
    [self playMusic]; 

    ... 

} 


//Have a way to get a playlist reference (as an MPMediaItemCollection in this case) 
- (MPMediaItemCollection*) getPlaylistWithName:(NSString *)playlistName 
{ 
    MPMediaQuery* query = [[MPMediaQuery alloc] init]; 
    MPMediaPropertyPredicate* mediaTypePredicate = [MPMediaPropertyPredicate predicateWithValue: [NSNumber numberWithInteger: MPMediaTypeMusic]                    forProperty:MPMediaItemPropertyMediaType]; 
    [query addFilterPredicate: mediaTypePredicate]; 
    [query setGroupingType: MPMediaGroupingPlaylist]; 

    NSArray* playlists = [query collections]; 
    [query release]; 

    for(MPMediaItemCollection* testPlaylist in playlists) 
    { 
     NSString* testPlaylistName = [testPlaylist valueForProperty: MPMediaPlaylistPropertyName]; 
     if([testPlaylistName isEqualToString: playlistName]) 
      return testPlaylist; 
    } 

    return nil; 
} 

//Override the setter on currentItem so that you can add/remove 
//the notification listener that will tell you when the song has completed 
- (void) setCurrentItem:(AVPlayerItem *)currentItem 
{ 
    if(m_currentItem) 
    { 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:m_currentItem]; 
     [m_currentItem release]; 
    } 

    if(currentItem) 
     m_currentItem = [currentItem retain]; 
    else 
     m_currentItem = nil; 

    if(m_currentItem) 
    { 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMusicTrackFinished) name:AVPlayerItemDidPlayToEndTimeNotification object:m_currentItem]; 
    } 
} 

//handler that gets called when the name:AVPlayerItemDidPlayToEndTimeNotification notification fires 
- (void) handleMusicTrackFinished 
{ 
    [self skipSongForward]; //or something similar 
} 

//Have a way to load a playlist 
- (void) loadPlaylist:(MPMediaItemCollection *)playlist 
{ 
    self.currentPlaylist = playlist; 
    self.currentTrack = [playlist.items objectAtIndex: 0]; 
} 

//Play the beats, yo 
- (void) playMusic 
{ 
    //check the current playback state and exit early if we're already playing something 
    if(self.currentPlaybackState == MPMusicPlaybackStatePlaying) 
     return; 

    if(self.currentPlaybackState == MPMusicPlaybackStatePaused) 
    { 
     [self.audioPlayer play]; 
    } 
    else if(self.currentTrack) 
    { 
     //Get the system url of the current track, and use that to make an AVAsset object 
     NSURL* url = [self.currentTrack valueForProperty:MPMediaItemPropertyAssetURL]; 
     AVAsset* asset = [AVURLAsset URLAssetWithURL:url options:nil]; 

     //Get the track object from the asset object - we'll need to trackID to tell the 
     //AVPlayer that it needs to modify the volume of this track 
     AVAssetTrack* track = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 

     //Build the AVPlayerItem - this is where you modify the volume, etc. Not the AVPlayer itself 
     AVPlayerItem* playerItem = [[AVPlayerItem alloc] initWithAsset: asset]; //initWithURL:url]; 
     self.currentItem = playerItem; 

     //Set up some audio mix parameters to tell the AVPlayer what to do with this AVPlayerItem 
     AVMutableAudioMixInputParameters* audioParams = [AVMutableAudioMixInputParameters audioMixInputParameters]; 
     [audioParams setVolume: 0.5 atTime:kCMTimeZero]; //replace 0.5 with your volume 
     [audioParams setTrackID: track.trackID]; //here's the track id 

     //Set up the actual AVAudioMix object, which aggregates effects 
     AVMutableAudioMix* audioMix = [AVMutableAudioMix audioMix]; 
     [audioMix setInputParameters: [NSArray arrayWithObject: audioParams]]; 

     //apply your AVAudioMix object to the AVPlayerItem 
     [playerItem setAudioMix:audioMix]; 

     //refresh the AVPlayer object, and play the track 
     [self.audioPlayer replaceCurrentItemWithPlayerItem: playerItem]; 
     [self.audioPlayer play]; 

    } 

    self.currentPlaybackState = MPMusicPlaybackStatePlaying; 
} 

- (void) pauseMusic 
{ 
    if(self.currentPlaybackState == MPMusicPlaybackStatePaused) 
     return; 

    [self.audioPlayer pause]; 

    self.currentPlaybackState = MPMusicPlaybackStatePaused; 
} 

- (void) skipSongForward 
{ 
    //adjust self.currentTrack to be the next object in self.currentPlaylist 
    //start the new track in a manner similar to that used in -playMusic 
} 

- (void) skipSongBackward 
{ 
    float currentTime = self.audioPlayer.currentItem.currentTime.value/self.audioPlayer.currentItem.currentTime.timescale; 

    //if we're more than a second into the song, just skip back to the beginning of the current track 
    if(currentTime > 1.0) 
    { 
     [self.audioPlayer seekToTime: CMTimeMake(0, 1)]; 
    } 
    else 
    { 
     //otherwise, adjust self.currentTrack to be the previous object in self.currentPlaylist 
     //start the new track in a manner similar to that used in -playMusic 
    } 
} 

//Set volume mid-song - more or less the same process we used in -playMusic 
- (void) setMusicVolume:(float)vol 
{ 
    AVPlayerItem* item = self.audioPlayer.currentItem; 

    AVAssetTrack* track = [[item.asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 

    AVMutableAudioMixInputParameters* audioParams = [AVMutableAudioMixInputParameters audioMixInputParameters]; 
    [audioParams setVolume: vol atTime:kCMTimeZero]; 
    [audioParams setTrackID: track.trackID]; 

    AVMutableAudioMix* audioMix = [AVMutableAudioMix audioMix]; 
    [audioMix setInputParameters: [NSArray arrayWithObject: audioParams]]; 

    [item setAudioMix:audioMix]; 
} 


@end 

請原諒你看到的任何錯誤 - 讓我知道在評論中,我會解決它們。否則,如果有人遇到同樣的挑戰,我希望這會有所幫助!

2

其實我找到了一個非常簡單的方法來做到這一點loading iPod URL's from MPMusicPlayer,但隨後通過AVAudioPlayer進行回放。

// Get-da iTunes player thing 
MPMusicPlayerController* iTunes = [MPMusicPlayerController iPodMusicPlayer]; 

// whazzong 
MPMediaItem *currentSong = [iTunes nowPlayingItem]; 

// whazzurl 
NSURL *currentSongURL = [currentSong valueForProperty:MPMediaItemPropertyAssetURL]; 

info("AVAudioPlayer playing %s", [currentSongURL.absoluteString UTF8String]) ; 

// mamme AVAudioPlayer 
NSError *err; 
avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:currentSongURL error:&err] ; 
if(err!=nil) 
{ 
    error("AVAudioPlayer couldn't load %s", [currentSongURL.absoluteString UTF8String]) ; 
} 
avAudioPlayer.numberOfLoops = -1; //infinite 

// Play that t 
[avAudioPlayer prepareToPlay] ; 
[avAudioPlayer play]; 

[avAudioPlayer setVolume:0.5]; // set the AVAUDIO PLAYER's volume to only 50%. This 
// does NOT affect system volume. You can adjust this music volume anywhere else too.