I've been making a project in cocos-2dx, but needed to create a .mm to select music from the iOS library to play in the background. I got it working by playing the music in the .mm file, but for various reasons, it needs to play in cocos-2dx using SimpleAudioEngine. I've tried converting the url's absoluteString to an NSString to an id to a const char* to move it back to the cocos-2dx files, but it still didn't run.
This is what it gave me for the absoluteString(ipod-library://item/item.m4a?id=456458322781804615)
.cc-
-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)collection{
if(collection){
MPMusicPlayerController* appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[appMusicPlayer setQueueWithItemCollection:collection];
[appMusicPlayer play];
MPMusicPlayerController *iPodMusicPlayerController = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *nowPlayingItem = [iPodMusicPlayerController nowPlayingItem];
NSURL *url = [nowPlayingItem valueForProperty:MPMediaItemPropertyAssetURL];
NSString *filePath= [url absoluteString];
NSString *filePath2 = [url path];
_songUrl = filePath;
}
-(id)returnsongUrl{
return _songUrl;
}
Second .mm
const char* MediaPicker::songUrl(){
id na1 = [[MediaPickerWrapper sharedIntance] returnsongUrl];
const char *cString = [na1 cStringUsingEncoding:NSASCIIStringEncoding];
return cString;
}
Cocos-2dx file
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(media->songUrl());
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(media->songUrl(),false);
It filters down to calling AVAudioPlayer's initWithContentsOfURL: method and should work with iPod media item urls. However, AVAudioPlayer only gained support for iPod URLs in iOS6. You may need to set a breakpoint to make sure your iPod URL path is getting passed through to the CDAudioManager's load method correctly.
You'll have to write your own wrapper and use AVPlayer or AVQueuePlayer if you want to target pre-iOS6 devices.
https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS6.html#//apple_ref/doc/uid/TP40011812-SW1
Related
Sorry for bad Engilsh.
I am new to iOS development.And I am trying to create a client application which stream .m4a audio file from stream url
rtsp://192.168.1.50:1935/TestServer/abc.m4a
and play it on button click.
I have tried almost every thing which I can find but I was not able to get and play stream.
There is my code which play audio from http url
- (IBAction)btn:(id)sender {
NSString *str = #"http://download.wavetlan.com/SVV/Media/HTTP/AAC/Nero_Soundtrax/NeroSoundTrax_test1_AAC-LC_v4_Stereo_CBR_96kbps_44100Hz.m4a";
NSString *urlStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSURL * mediaURL = [NSURL URLWithString:urlStr];
player = [AVPlayer playerWithURL:mediaURL];
[player play];
}
but I want to play audio file from this type of url. rtsp://192.168.1.50:1935/TestServer/abc.m4a
i tested my streaming url on VLC to verify. It works perfect but when I put that url on button click code it didn't work.
I try to use ffmpeg library but did not find luck in that.
Try register audio session category before playing.
- (IBAction)btn:(id)sender {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
NSString *str = #"http://download.wavetlan.com/SVV/Media/HTTP/AAC/Nero_Soundtrax/NeroSoundTrax_test1_AAC-LC_v4_Stereo_CBR_96kbps_44100Hz.m4a";
NSString *urlStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSURL * mediaURL = [NSURL URLWithString:urlStr];
player = [AVPlayer playerWithURL:mediaURL];
[player play];
}
Hope this helps.
Standard iOS frameworks do not support RTSP playback, so you will have to use some third-party solution.
Hope this post helps:
https://gist.github.com/oc2pcoj/e55795550984d205d109
Whenever i play an audio file like below, it is taking few seconds to start playing the audio. I have seen from some forums and understand that, AVAudioPlayer will take few seconds to start playing if we allocate the object there itself. I am thinking to allocate this object much earlier (may be Appdelegate itself), before when i want to play, so that when i want to play it, it will play immediately.
NSURL *audioPathURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:audioName ofType:#"wav"]];
audioP = [[AVAudioPlayer alloc]initWithContentsOfURL:audioPathURL error:NULL];
[appDelegate.audioP play];
but, i am passing audio url dynamically at run time, so i would like to know how can i allocate the object earlier and then be able to pass dynamic audio url path later?
Please note, my question is different from this question Slow start for AVAudioPlayer the first time a sound is played
In the existing question, they are telling about one audio file and play it when required. But, i am having many different audio file and url path is generated randomly and at run time to play, so i do not know what the actual url path to set and play. So, the answer mentioned here->Slow start for AVAudioPlayer the first time a sound is played
won't help for my question.
I can't use prepareToPlay, because audio path url is set at run time and not just one audio is being used for all the times to play, there will more than 20 audio file randomly chosen and set to play one at a time. So, i need the right answer for it, it is not a duplicate question.
Thank you!
For a single file here the solution:
in your file.h
#import <AVFoundation/AVFoundation.h>
#interface AudioPlayer : UIViewController <AVAudioPlayerDelegate> {
}
To use with button add - (IBAction)Sound:(id)sender; in your file.h
Now in your file.m
//this is a action but you can use inside a void to start auto
#implementation AudioPlayer {
SystemSoundID soundID;
}
- (IBAction)Sound:(id)sender {
AudioServicesDisposeSystemSoundID(soundID);
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
//you can use here extension .mp3 / .wav / .aif / .ogg / .caf / and more
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) #"sound" ,CFSTR ("mp3") , NULL);
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
If you want use your code to play audio from URL you can use this:
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#interface AudioPlayer : UIViewController <AVAudioPlayerDelegate> {
AVPlayer *mySound;
}
#property (strong, nonatomic) AVPlayer *mySound;
#end
And file.m
- (IBAction)playPause:(id)sender {
if (mySound.rate == 0){
NSURL *url = [NSURL URLWithString:#"http://link.mp3"];
mySound = [[AVPlayer alloc] initWithURL:url];
[mySound setAllowsExternalPlayback:YES];
[mySound setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[mySound play];
} else {
[mySound pause];
}
}
Hope this can help you!
One thing you can do to reduce the latency of the first load+play is to "spin up" the underlying audio system before you need it by loading and playing a dummy file. For example, in application:didFinishLaunchingWithOptions: add some code like this:
// this won't effect the problem you're seeing, but it's good practice to explicitly
// set your app's audio session category
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
NSURL *audioURL = [[NSBundle mainBundle] URLForResource:#"dummy.wav" withExtension:nil];
AVAudioPlayer *dplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
// we don't want to hear it (alternatively, the audiofile you use for this purpose can be silence.)
dplayer.volume = 0.f;
[dplayer prepareToPlay];
[dplayer play];
[dplayer stop];
Once this code completes, subsequent instantiations and playback of AVAudioPlayer instances should have pretty low latency (certainly well under 1 second)
I've developed an iOS app that plays two separate audio streams simultaneously. To do this I am using AVPlayer thus:
//Use URL to set up the speech radio player.
NSURL *urlStreamSpeech = [NSURL URLWithString:self.urlAddressSpeech];
playerSpeech = [AVPlayer playerWithURL:urlStreamSpeech];
//Use URL to set up the music player.
NSURL *urlStreamMusic = [NSURL URLWithString:self.urlAddressMusic];
playerMusic = [AVPlayer playerWithURL:urlStreamMusic];
I am now trying to implement a volume control so that the user will be able to control the volume for the audio streams individually. As I've tried this I have come to the conclusion that there is no volume property for the AVPlayer class. I also looked at the AVAudioPlayer class, but from what I gather that class is only able to play local audio files, not streamed audio.
So my question is: how can I control the volume of streamed audio on iOS?
NSString* resourcePath = url; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;
app.audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
app.audioPlayer.numberOfLoops = 0;
app.audioPlayer.volume = 1.0f;
[app.audioPlayer prepareToPlay];
if (app.audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[app.audioPlayer play];
Look into the AVPlayer class, part of the AVFoundation framework... this is the objective-c (iOS) class you use for streaming audio.
look at this post Live streaming
MPMusicPlayerController* musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
[musicPlayer setVolume:[SliderVolumen value]];
Only work in the device, no simulator.
Compatible with AVPlayer.
In my app I use AVAudioPlayer to play some mp3. My code is:
- (void) play{
NSString *path = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath],
[arraysound objectAtIndex:appDelegate.indexSound]];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
soundID = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
soundID.delegate = self;
[soundID prepareToPlay];
[soundID play];
}
but I have 40 sounds to play; I use everytime soundID to play my sounds
example I have sound1.mp3, sound2.mp3, sound3.mp3......
How can I release my soundId?
When my sound change I call "play" method but this method alloc a new AVAudioPlayer everytime
I Know
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
successfully: (BOOL) flag {
but I don't understand how to release my sound id inside this delegate method, because if I release inside soundId my app crash
after some minutes that I use my app I have with crash this message "shm_open failed: "Apple Audio Queue"
please help me...thanks
You don't want to release soundID in your delegate method. You want to release player. Judging from your code, soundID is an ivar. When the delegate gets called, soundID may have been set to another value. But the player parameter will be the AVAudioPlayer that has just finished playing.
SETUP
I put a mp3 into my bundle just for testing, i will have them download into the documents directory. But for now i am going to work off the bundle.
I have found a couple tutorials, but not anything for what i need. The below is definitely a mix of a couple solutions and probably not the best approach.
Problem
How do i pull a song out of my bundle and play within the app?
//Create an instance of MPMusicPlayerController
MPMusicPlayerController* myPlayer = [MPMusicPlayerController applicationMusicPlayer];
//Read the song off the bundle.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"LittleMoments" ofType:#"mp3"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
//This wont work for me because i don't want to query songs from the iPod,
i want to play songs out of the Bundle.
//Create a query that will return all songs by The Beatles grouped by album
MPMediaQuery* query = [MPMediaQuery songsQuery];
[query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:#"The Beatles" forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonEqualTo]];
[query setGroupingType:MPMediaGroupingAlbum];
//Pass the query to the player
[myPlayer setQueueWithQuery:query];
/////**** how can i get nsdata into MyPlayer?*****//////
//Start playing and set a label text to the name and image to the cover art of the song that is playing
[myPlayer play];
}
Try playing the file with AVAudioPlayer
AVAudioPlayer *audioPlayer = [(AVAudioPlayer*)[AVAudioPlayer alloc] initWithData:myData error:nil];
audioPlayer.delegate = self;
[audioPlayer play];