I am trying to start playing music from within my application. I have iOs 6 but I need it to run in 5.1 as I need to support iPad 1 which goes up to that version.
I imported both CoreMedia and MediaPlayer frameworks and I am playing via the MPMusicPlayerController. I am able to get the list of songs, but playing doesn't work. I am testing in a real device, not the simulator.
Any thoughts into what is wrong?
__strong NSArray * _myMusic;
__strong MPMusicPlayerController * _player;
// Specify a media query
MPMediaQuery *query = [MPMediaQuery songsQuery];
// Obtain the media item collections from the query
_myMusic = [query collections];
_player = [MPMusicPlayerController applicationMusicPlayer]; //also tried iPodMusicPlayer
MPMediaItemCollection *currentQueue = [[MPMediaItemCollection alloc] initWithItems:_myMusic];
[_player setQueueWithItemCollection:currentQueue];
[_player setShuffleMode: MPMusicShuffleModeOff];
[_player setRepeatMode: MPMusicRepeatModeNone];
[_player setVolume:1];
[_player play];
Related
I have an app that plays various musical sounds using AVAudioPlayer. It lets people select a song from their library and plays it with MPMusicPlayerController. It used to work that they could jam with it, but now when MPMusicPlayerController is playing it doesn't play the sound using AVAudioPlayer. It even doesn't play the sound once MPMusicPlayerController has stopped. I've tested it on 13.4.1 and 13.6. What changed to make it stop working and what can I do to fix it?
It plays the sound like this:
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
[player prepareToPlay];
[player setVolume: 1.0];
[player play];
For the music, it gets a MPMediaItemCollection using MPMediaPickerController, then plays it like this:
- (void) playMusic: (MPMediaItemCollection *) collection {
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
if (musicPlayer == nil) {
musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[musicPlayer setShuffleMode: MPMusicShuffleModeOff];
[musicPlayer setRepeatMode: MPMusicRepeatModeNone];
}
[musicPlayer setQueueWithItemCollection:collection];
[musicPlayer play];
}
Feeling stupid. The phones were set to silent mode. For some reason, it can play the sounds or the music on silent, but it can't play them both together.
I am looking at ways to store song information and then play a specific song. I've seen this post:
How do you use MPMediaItemPropertyPersistentID to play music in iPhone Music Player Framework?
If I have a MPMediaItemPropertyPersistentID already, can I directly play that song without looping every single song until I find a matching id?
You don't have to do this by looping through all the items in the library. It can be done via MPMediaQuery, something like this:
NSNumber *persistentIDNumber = [NSNumber numberWithInteger:4238475234];
MPMusicPlayerController *player = [MPMusicPlayerController applicationMusicPlayer];
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:persistentIDNumber forProperty:MPMediaEntityPropertyPersistentID];
MPMediaQuery *query = [[MPMediaQuery alloc] init];
[query addFilterPredicate: predicate];
[player setQueueWithQuery:query];
[player prepareToPlay];
[player play];
I am trying to play MPMedaiItem in MPMusicPlayerController in iOS.
In my case , i have a UITableView that show songs from playlist.
When i tap cell on UITableView , i want to play that song with MPMusicPlayerController.
And i also want to skip next songs from playlist when i tap Next Button.
How can i play it?
Here is some of my codes that write in didSelected Method of UITableView.
That doesn't play anything.
MPMediaItemCollection *songs = [self.arrayOfSongs objectAtIndex:indexPath.row ];
MPMediaItem *item = [songs representativeItem ];
NSLog(#"%#",[item valueForProperty:MPMediaItemPropertyTitle ]);
[self.player setNowPlayingItem:[item valueForProperty:MPMediaItemPropertyAssetURL]];
[self.player play ];
I know this is a little late, but your problem is that MPMusicPlayerController's nowPlayingItem property expects a MPMediaItem object, and you're passing it an NSString containing the URL of the asset. Here's an example of how this could be accomplished.
MPMusicPlayerController *controller = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:arrayOfMediaItems];
MPMediaItem *item = [collection representativeItem];
[controller setQueueWithItemCollection:collection];
[controller setNowPlayingItem:item];
[controller prepareToPlay];
[controller play];
I find it easier to use AVPlayer in an instance like this.
in your header file declare the AVPlayer object;
AVPlayer *audioPlayer;
Then in your method file use something like:
if(!audioPlayer){
audioPlayer = [[AVPlayer alloc] initWithURL:[item valueForProperty:MPMediaItemPropertyAssetURL]];
} else {
[audioPlayer replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:itemURL]];
}
[audioPlayer play];
I'm using AVPlayer for playing radio stream.
For initializing player i'm using next code:
self.asset = [AVAsset assetWithURL:self.url];
self.item = [AVPlayerItem playerItemWithAsset:self.asset];
self.player = [AVPlayer playerWithPlayerItem:self.item];
I've read this question Adjusting the volume of a playing AVPlayer
and i'm trying to set volume with slider value
- (void)setVolume:(float )volume
{
_volume = volume;
NSArray *audioTracks = self.asset.tracks;
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
AVMutableAudioMixInputParameters *audioInputParams =
[AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:volume atTime:kCMTimeZero];
[allAudioParams addObject:audioInputParams];
[audioInputParams setTrackID:[track trackID]];
}
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
[audioMix setInputParameters:allAudioParams];
NSLog(#"%#", self.player.currentItem);
NSLog(#"%f", volume);
[self.player.currentItem setAudioMix:audioMix];
}
I'm also trying to set
[audioInputParams setVolume:volume atTime:self.player.currentTime];
but there are not any tracks in that array (self.asset.tracks),
and its have not any effect on volume. (also i can't get any metadata),
I can't use AVAudioPlayer for playing audio stream by url
An instance of the AVAudioPlayer class, called an audio player,
provides playback of audio data from a file or memory.
Apple recommends that you use this class for audio playback unless you
are playing audio captured from a network stream or require very low
I/O latency.
I'm thinking about using that code https://github.com/DigitalDJ/AudioStreamer/downloads
for playing radio stream, but i don't know how set volume with Audiotoolbox.
Please, help me! Thanks!
i have tried lots of solutions, but the best way to do that is to implement an instance ov MPVolumeVIew:
self.myViewVolume = [[MPVolumeView alloc] initWithFrame:CGRectMake(20, 330, 280, 50)];
[self.myViewVolume sizeToFit];
[self.view addSubview:self.myViewVolume];
You can add this to any UIView you want, just change the class to MPVolumeView
Do not forget to add MediaPlayer framework.
Here is what you have to do if you want to get metadata from your stream:
[playerItem addObserver:self forKeyPath:#"timedMetadata" options:NSKeyValueObservingOptionNew context:NULL];
With this you can simply add the information to any UILabel
So I'm trying to play a sound file at a different rate in iOS 5.1.1, and am having absolutely no luck. So far I have tried setting the rate of the AVAudioPlayer:
player = [[AVAudioPlayer alloc] initWithContentsOfURL:referenceURL error:&error];
player.enableRate = YES;
player.rate = 1.5;
player.numberOfLoops = 0;
player.delegate = self;
[player prepareToPlay];
[player play];
with no luck at all, the sound plays but just ignores the rate I give it. I have also tried AVPlayer:
avPlayer = [[AVPlayer alloc] initWithURL:referenceURL];
avPlayer.rate = 0.5;
[avPlayer play];
Again, it plays but just ignores the rate I set. I have tried a number of different audio files but for the sake of this thread I selected Rooster-mono.wav from this catalogue: http://sig.sapp.org/sounds/wave/
Has anybody had any success with changing the playback rate on iOS 5.1.1? Or does anybody know what I am missing here?
I am doing this to change the pitch slightly of some of my samples, I realise I could do this by using RemoteIO or something similar but that seems total overkill for what I am trying to achieve (a simple playback rate adjustment).
here is some code that i know works, just re-tested in an app i've been working on. as you mention, using setEnableRate: and setRate: will only work with iOS 5.0 and above. so i use respondsToSelector: to test on the device whether or not the device will accept the request:
_noticeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Rooster-mono" ofType:#"wav"]]
error:nil];;
if ([_noticeAudio respondsToSelector:#selector(setEnableRate:)])
_noticeAudio.enableRate = YES;
if ([_noticeAudio respondsToSelector:#selector(setRate:)])
_noticeAudio.rate = 2.0;
running on an iOS 5 device, it performs the double-rate successfully. running on iOS 4.3, it plays it at normal speed.
so, the only way you'll get the proper rate is if your device has iOS 5 on it.
Modify the code to:
avPlayer = [[AVPlayer alloc] initWithURL:referenceURL];
[avPlayer play]; //call play first
avPlayer.rate = 0.5; //then set rate
This is how you do it.
rate value is between 0.1f - 2.0f
player = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:&err];
player.volume = 0.4f;
player.enableRate=YES;
[player prepareToPlay];
[player setNumberOfLoops:0];
player.rate=2.0f;
[player play];
Swift 2.0
let player = AVPlayer(URL: url)
player.play()
player.rate = 0.9