How to Play Streaming Audio in UITableView - iphone

I have searched relentlessly but with no luck.
I am simply trying to play streaming audio files (mp3) in my tableview (didSelectRowAtIndexPath) but their is no error in xcode but the mp3 wont play
Here is a snippet
NSString *urlstr = #"www.nosajsolomon.com/pull_up_to_mi_bumper.mp3";
NSURL *url = [NSURL URLWithString:urlstr];
AVPlayerItem *playerItem2 = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:playerItem2];
[player play];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
Any idea what I am doing wrong?

Related

MPMoviePlayerController Endless Loading

I'm trying to play an MP4 file with the MPMoviePlayerController (also tried two m4v files, same problem), and finally got the video player to come up, but it just stays loading. Here's my code, executed on a button press:
NSString *videoFile = [[NSBundle mainBundle] pathForResource:#"attn8" ofType:#"mp4" inDirectory:#""];
NSURL *url = [NSURL fileURLWithPath:videoFile isDirectory:NO];
player = [[MPMoviePlayerController alloc] initWithContentURL:url];
player.movieSourceType = MPMovieSourceTypeFile;
//put on a whim from another answer, made no difference
[self.view addSubview:[player view]];
player.fullscreen = true;
[player prepareToPlay];
[player play];

Play audio without buffer

I am playing an audio file in my iphone app,I fetch the audio from a url.While playing it stops for a while to buffer.
How can I make playing audio without any buffer or play after loaded fully ?
-(void) playAudio2
{
NSURL *url = [NSURL URLWithString:#"http:domain/iphonetest/mp3/sol.mp3"];
NSData *data = [NSData dataWithContentsOfURL:url];
audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audioPlayer setVolume:1];
[audioPlayer play];
}
Try using NSURLConnection or AFNetworkingKit to download the file (There is lots of resources show how to download a file. Just search for it).
Then in the -(void)connectionDidFinishLoading:(NSURLConnection *)connection delegate method for NSURLConnection or in the completionBlock for AFNetworkingKit, play the file like your code. But use NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file] for initializing url and use [audioPlayer prepareToPlay] before playing that.

play video using url ios 6

I am struggling to play a video which is a url of the video file on a server. I have a view in which I display url from the web service. When I click the url(contained in a table cell) I want that a new view should appear with movie player on it playing the video. I have tried MPMoviePlayerViewController and also MPMoviePlayerController and various combinations of the two but I could not play the video on simulator. Currently I don't have a device so please consider simulator as well as device while answering. Currently i am using:
NSURL *url = [NSURL fileURLWithPath:filePath];
self.player= [[ MPMoviePlayerViewController alloc] initWithContentURL:url];
//self.player.navigationController.navigationBar.hidden = YES;
[self.player.moviePlayer prepareToPlay];
//self.player.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
self.player.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.player.moviePlayer.fullscreen = NO;
[self presentModalViewController:self.player animated:NO];
[self.player.moviePlayer play];
filepath is a nsstring containing the video url.
Replace your this line : NSURL *url = [NSURL fileURLWithPath:filePath];
with this: NSURL *url=[NSURL URLWithString:filePath]; & then try.
My Code I am using MPMOVIEPLAYERVIEWCONTROLLER:
NSData *geturl = [[videoparsing objectAtIndex:btntag]objectForKey:#"iurl"];
myString = [[NSString alloc] initWithData:geturl encoding:NSASCIIStringEncoding];
NSLog(#"myString..%#",myString);
NSURL *fileURL=[NSURL URLWithString:myString];
NSLog(#"fileURL..%#",fileURL);
moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
[moviePlayerController.moviePlayer prepareToPlay];
moviePlayerController.moviePlayer.shouldAutoplay=YES;
[moviePlayerController.moviePlayer play];
If video file is on server then
NSURL *url=[NSURL URLWithString:filePath];
other should be change movieSourceType to MPMovieSourceTypeStreaming :
self.player.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
Also
self.player.moviePlayer.fullscreen = YES;
EDIT : Add :
self.player.moviePlayer.shouldAutoplay=YES;
remove :
[self.player.moviePlayer play];

Get an audio stream from URI and play it on iPhone

I would like to get an audio stream from URL and play it on iPhone. Unfortunately, there is only C# example of how to play that stream, but I need a realization on Objective C in iPhone frameworks. I have tried my self to use different audio frameworks but it was all unsuccessful. This is what in C#:
response = httpWebRequest.GetResponse();
using (Stream stream = response.GetResponseStream())
{
using (SoundPlayer player = new SoundPlayer(stream))
{
player.PlaySync();
}
}
What should I do in objective C to get a stream audio from that url? In that URL I don't have an mp3 file or something, I just get a stream. Probably I must somehow download that stream to an iPhone audio file and then play it to get rid of lags.
I tried this:
AVPlayer *player = [AVPlayer playerWithURL:url];
[player play];
And this:
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:nil];
[theAudio setVolume :0.50]; //setting the volume
[theAudio play];
Neither worked.
I have tested my url, it works well in browser.
This is the code that worked:
NSData *_objectData = [NSData dataWithContentsOfURL:url];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
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];
Hey I faced this problem of audio streaming for a radio application. I had searched for the answers and they did not work. AVAudioPlayer is not the class to be used here. Downloading the contents of URL using NSData object is not the solution either as it downloads the contents then the code further executes. It is similar to downloading a mp3 and then playing it locally on a player.
[NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]]; //Downloads the contents of the url and in case of radio the url's content is unlimited. The program gets frozen on executing this.
Instead use AVPlayer class.
This code worked in my radio application
AVPlayer *objAVPlayer = [[AVPlayer alloc] initWithURL:url];
[objAVPlayer play];
This code does not download the contents at the url but it plays the stream as it is received.
Look into the AVPlayer class, part of the AVFoundation framework... this is the objective-c (iOS) class you use for streaming audio.
MPMoviePlayerController *player;
player = [[MPMoviePlayerController alloc] init] ;
player.view.frame = CGRectMake(0, 0, 0, 0);
player.movieSourceType = MPMovieSourceTypeStreaming;
player.view.hidden = YES;
[self.view addSubview: [wikiMusicPlayer view]];
NSString *address = Your URL;
address = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:address];
[wikiMusicPlayer setContentURL:url];
[wikiMusicPlayer prepareToPlay];
[wikiMusicPlayer setShouldAutoplay: NO];
[wikiMusicPlayer play];

How to play video file in MPMOviePlayer?

I have implementing uITabbar application in which i want to play video file.Video file is playing but not seen.And simulator is Landscapmode bu MPMoviePlayer in Potrait mode.How to solve this problem.
Try to play the mp4 format.Because MPMoviePlayer Controller does not support many formats like avi.Here is the code which i implement.Hope it will help you and download any mp4 video file to play.
NSString *soundFilePath = [[NSBundle mainBundle]pathForResource:#"SoundFileName" ofType:#"mp4"];
//NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
NSURL *fileURL=[[NSURL fileURLWithPath:soundFilePath]retain];
player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[fileURL release];
[player play];