ios - 如何以与系统音量不同的音量播放 iPod 音乐库中的轨道?
<p><p>如何以与系统音量不同的音量播放 ipod 音乐库(如用户定义的播放列表等)中的音乐?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这适用于尝试以与系统音量不同的音量播放 ipod 音乐库中的音乐/播放列表的人。有几篇帖子说 <code></code> 可以做到这一点,但我发现每当我改变 applicationMusicPlayer 的音量时,系统音量也会改变。 </p>
<p>还有一种使用 AVAudioPlayer 类播放音乐的复杂方法,但它需要您将音乐文件从 ipod 库复制到应用程序包,当您播放动态内容(例如用户生成的)时,这可能会变得很棘手播放列表。不过,该技术确实可以让您访问字节,如果您想对数据进行处理(如 DJ 应用程序),这是一种方法。链接到该解决方案 <a href="http://blog.tapsquare.com/post/803301658/ipod-library-access/" rel="noreferrer noopener nofollow">HERE</a> .</p>
<p>我使用的解决方案使用 AVPlayer 类,那里有几篇关于如何做到这一点的好帖子。这篇文章基本上是我在 Stackoverflow 和其他地方找到的几种不同解决方案的组合。</p>
<p>我链接了以下框架:</p>
<p>AV基金会<br/>
媒体播放器<br/>
音频工具箱<br/>
核心音频<br/>
核心媒体</p>
<p>(我不确定所有这些是否都很关键,但这就是我所拥有的。我也实现了一些 OpenAL 的东西,我没有在下面的代码中显示)</p>
<pre><code>// 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 = [ init];
self.audioPlayer = tempPlayer;
;
...
//load the playlist you want to play
MPMediaItemCollection* playlist = ;
if(playlist)
;
...
//initialize the playback state
self.currentPlaybackState = MPMusicPlaybackStateStopped;
//start the music playing
;
...
}
//Have a way to get a playlist reference (as an MPMediaItemCollection in this case)
- (MPMediaItemCollection*) getPlaylistWithName:(NSString *)playlistName
{
MPMediaQuery* query = [ init];
MPMediaPropertyPredicate* mediaTypePredicate = forProperty:MPMediaItemPropertyMediaType];
;
;
NSArray* playlists = ;
;
for(MPMediaItemCollection* testPlaylist in playlists)
{
NSString* testPlaylistName = ;
if()
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)
{
[ removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:m_currentItem];
;
}
if(currentItem)
m_currentItem = ;
else
m_currentItem = nil;
if(m_currentItem)
{
[ addObserver:self selector:@selector(handleMusicTrackFinished) name:AVPlayerItemDidPlayToEndTimeNotification object:m_currentItem];
}
}
//handler that gets called when the name:AVPlayerItemDidPlayToEndTimeNotification notification fires
- (void) handleMusicTrackFinished
{
;//or something similar
}
//Have a way to load a playlist
- (void) loadPlaylist:(MPMediaItemCollection *)playlist
{
self.currentPlaylist = playlist;
self.currentTrack = ;
}
//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)
{
;
}
else if(self.currentTrack)
{
//Get the system url of the current track, and use that to make an AVAsset object
NSURL* url = ;
AVAsset* asset = ;
//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 = [ objectAtIndex:0];
//Build the AVPlayerItem - this is where you modify the volume, etc. Not the AVPlayer itself
AVPlayerItem* playerItem = [ 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 = ;
;//replace 0.5 with your volume
;//here's the track id
//Set up the actual AVAudioMix object, which aggregates effects
AVMutableAudioMix* audioMix = ;
];
//apply your AVAudioMix object to the AVPlayerItem
;
//refresh the AVPlayer object, and play the track
;
;
}
self.currentPlaybackState = MPMusicPlaybackStatePlaying;
}
- (void) pauseMusic
{
if(self.currentPlaybackState == MPMusicPlaybackStatePaused)
return;
;
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)
{
;
}
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 = [ objectAtIndex:0];
AVMutableAudioMixInputParameters* audioParams = ;
;
;
AVMutableAudioMix* audioMix = ;
];
;
}
@end
</code></pre>
<p>请原谅您看到的任何错误 - 请在评论中告诉我,我会修复它们。否则,如果有人遇到与我相同的挑战,我希望这会有所帮助!</p></p>
<p style="font-size: 20px;">关于ios - 如何以与系统音量不同的音量播放 iPod 音乐库中的轨道?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/14881451/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/14881451/
</a>
</p>
页:
[1]