Stream .m4a audio file from rtsp url objective-c ios - ios

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

Related

Using MPMediaItem with simpleaudioengine

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

I am trying to play audio from server using AVAudioPlayer, but file not Playing

i am trying to make an online audio player. but i can't paly audio using AVAudioPlayer object. Please Help Me. Thanks in Advance.
Here is my audio player code.
================>>
NSURL *url =[[NSURL alloc] initWithString:#"My Url"];
xPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
xPlayer.numberOfLoops = 0;
xPlayer.volume = 1.0f;
[xPlayer prepareToPlay];
[xPlayer play];
Q: Does the AVAudioPlayer provide support for streaming audio content?
A: The AVAudioPlayer class does not provide support for streaming audio based on HTTP URL's. The URL used with initWithContentsOfURL: must be a File URL (file://). That is, a local path.
Source: https://developer.apple.com/library/ios/qa/qa1634/_index.html

How to play a radio station on button press in iOS

I want to play a radio station when someone presses the play radio button. However, it does not work.
Here is my code:
NSURL *url = [NSURL URLWithString:#"http://www.radio.net.pk/"];
NSData *data = [NSData dataWithContentsOfURL:url];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audio play];
The AVAudioPlayer class only lets you play sound in any audio format available in iOS and OS X
This means, the AVAudioPlayer class does not provide support for streaming audio based on HTTP URL's. The URL used with initWithContentsOfURL: must be a local path URL.
So you should save the NSData locally (with different chunks) and play it using AVAudioPlayer.
It won't work ever.
Does radio.net.pk offers any APIs?
datWithContentsOfURL will return HTML string that you are trying to play using AVAudioPlayer !!. Please go through the website again and check for any developer API.
// Using this Cannel
NSURL *url6 = [NSURL URLWithString:#"http://sc6.spacialnet.com:35464/"]
layerItem = [AVPlayerItem playerItemWithURL:_strTemp]; // add url to playerItem
player = [AVPlayer playerWithPlayerItem:playerItem]; // add player item to AVAudioPlayer
player = [AVPlayer playerWithURL:_strTemp]

I want to play dailymotion video from URL in iOS 7 its not working

I want to play daily-motion Video from URL in iOS 7 its not working
NSString *urlString = #"http://www.dailymotion.com/video/xwxfk5";
NSURL *URL = [NSURL URLWithString:urlString];
NSData * data = [[NSData alloc] initWithContentsOfURL:URL];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
DMPlayerViewController *mpWatchController = [[DMPlayerViewController alloc] initWithVideo:str];
mpWatchController.view.frame = CGRectMake(0,39,320,186);
[mpWatchController play];
You can't play the video because you need to supply your player with a direct link to a video file (ending in .mov or .mp4 for example), not to a webpage, like Dailymotion, which hosts a video through a player.
Looks like you use DMPlayerViewController - official Dailymotion iOS player. In this case you should pass not #"http://www.dailymotion.com/video/xwxfk5", but #"xwxfk5". Change your code:
NSString *URLString = #"http://www.dailymotion.com/video/xwxfk5";
DMPlayerViewController *mpWatchController = [[DMPlayerViewController alloc] initWithVideo:URLString.lastPathComponent];
mpWatchController.view.frame = CGRectMake(0,39,320,186);
[mpWatchController play];

How can I stream audio and also be able to control volume on iOS?

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.

Resources