시스템 볼륨과 다른 볼륨에서 iPod 음악 라이브러리 (예 : 사용자 정의 재생 목록 등)의 음악을 재생하는 방법은 무엇입니까?AVPlayer를 사용하여 시스템 볼륨과 다른 볼륨으로 iPod 음악 라이브러리에서 트랙을 재생하는 방법
답변
이것은 시스템 볼륨과 다른 볼륨으로 iPod 음악 라이브러리에서 음악/재생 목록을 재생하려는 사용자를위한 것입니다. 거기에 몇 가지 게시물은 [MPMusicPlayerController applicationMusicPlayer]
할 수있는 말을하지만, 나는 언제든지 내가 applicationMusicPlayer의 볼륨을 변경 발견, 시스템 볼륨도 변경됩니다.
AVAudioPlayer 클래스를 사용하여 음악을 재생하는 방법이 더 복잡하지만 ipod 라이브러리의 음악 파일을 응용 프로그램 번들로 복사해야하며 사용자와 같이 동적 인 내용을 재생할 때 까다로운 작업이 필요할 수 있습니다. 생성 된 재생 목록. 이 기술은 바이트에 대한 액세스를 제공하지만 데이터 (DJ 응용 프로그램과 같은)에 대한 처리를 수행하려는 경우 이동하는 방법입니다. 해당 솔루션에 대한 링크 HERE.
내가 사용한 해결책은 AVPlayer 클래스를 사용하는 방법에 대한 몇 가지 좋은 게시물이 있습니다. 이 게시물은 기본적으로 Stackoverflow 및 다른 곳에서 찾은 몇 가지 다른 솔루션의 합성물입니다.
가 나는 다음과 같은 프레임 워크는 연결 한 :
AVFoundation
MediaPlayer를
AudioToolbox
의 CoreAudio
CoreMedia
(I 그 모든 중요한 있는지 확실하지 않습니다,하지만 내가 가진 무엇 . 다음 코드에서는 표시되지 않는 일부 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
표시되는 오류를 용서하십시오. 의견에 대해 알려 주시면 해결하겠습니다. 그렇지 않은 사람이라면 누구나 내가했던 것과 똑같은 도전에 빠지면 도움이되기를 바랍니다!
실제로 나는 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.
- 1. iPod 음악 라이브러리에서 재생
- 2. ipodlibrary에서 노래를 가져 와서 AVPlayer를 사용하여 재생하는 방법
- 3. AS3에서 음악 트랙을 재생하는 방법은 무엇입니까?
- 4. 음악 라이브러리에서 특정 노래를 재생하는 방법은 무엇입니까?
- 5. AVPlayer를 사용하는 전 기능 음악 플레이어
- 6. AVPlayer를 사용하여 라이브 스트림의 오디오 트랙을 음소거
- 7. AVPlayer에서 iPod 라이브러리 파일을 재생하는 방법
- 8. iPod 음악으로 AVPlayer를 초기화 할 수 없습니다.
- 9. iPod 라이브러리에서 두 개의 오디오 파일을 동시에 재생하는 방법은 무엇입니까?
- 10. 라이브러리에서 음악 파일을 얻는 방법
- 11. 음악 이름을 연속 재생하는 음악
- 12. AVPlayer가 음악 라이브러리에서 재생되지 않습니다.
- 13. 벨소리 볼륨과 연결된 시스템 볼륨
- 14. iPod 음악 폴더에 액세스
- 15. 음악 라이브러리에서 음악 파일 재생
- 16. spotify 음악 스트림을 재생하는 방법
- 17. 아이폰에있는 iTunes 라이브러리에서 노래를 재생하는 방법
- 18. libspotify : 트랙 uri에서 트랙을 재생하는 방법?
- 19. iPod 라이브러리에서 노래 제목으로 재생하기
- 20. MPMoviePlayerController는 iPod 음악 재생을 중지합니다.
- 21. 여러 트랙을 재생하는 AVFoundation AudioPlayer?
- 22. spotify apps API를 사용하여 특정 위치에서 트랙을 재생하는 방법
- 23. 중복을 포함하여 iPod 라이브러리에서 노래를 계산하는 방법
- 24. setVolume이 AVMutableAudioMixInputParameters에서 작동하지 않습니다. 로컬 iPod 라이브러리
- 25. iOS 더미 AVAudioPlayer를 사용하여 링거에서 시스템 볼륨으로 전환
- 26. 음악 플레이어에서 LRC 및 음악을 재생하는 방법
- 27. 음악 라이브러리에서 노래 세부 정보 가져 오기
- 28. 음악 라이브러리에서 가져온 음악 파일의 벨소리를 만드는 방법
- 29. iTunes의 API를 사용하여 트랙을 "드러내거나"선택하는 방법
- 30. GW-Basic에서 배경 음악 (음악)을 재생하는 방법