I have went through many youtube tutorials and looked everywhere. I want to create a live streaming app, I want to click the button play and it plays the streaming audio, and when i click the Stop button it stops. here is my code. Although on my device no sound actually plays. I don't know what Im doing wrong.
I have imported #import <AVFoundation/AVFoundation.h> in both .m and .h
-(IBAction)LiveStream3:(id)sender {
NSString *stream = #"http://audio2.radioreference.com/il_chicago_police2.mp3";
NSURL *url = [NSURL URLWithString: stream];
myAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:url fileTypeHint:stream error:nil];
myAudio.numberOfLoops = 0;
[myAudio play];
[streamTitle setText:#"Chicago"];
NSLog(#"Playing");
}
-(IBAction)stopAudio:(id)sender {
[myAudio stop];
[streamTitle setText:#""];
NSLog(#"Stop");
}
Related
I need to play a keyboard sound effect when user touch in button.
but the problem is when the user tap 3 time in the button the
playTypingSound method call 3 time but I hear the typing sound effect once.
I do not want to use Apple's keyboard sound
This is My method:
-(void)playTypingSound
{
NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"typing" ofType:#"mp3"]];
typingSound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
typingSound.volume = 0.05;
[typingSound play];
}
http://puu.sh/gBhKF/2f271d8804.png
I'm trying to open a mpmovieplayer when the webview is finish loading. The problem is the MPMoviePlayer opens and close and gives me this error:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
I'm using following code which i've found by searching on the internet. Why wont it play the youtube video?
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
NSURL *movieURL = [NSURL URLWithString:#"http://www.youtube.com/embed/AhE5dsO8xF4"];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:mp];
[mp.moviePlayer play];
}
I got a website where I make playlists with SoundCloud. Now I want to make an app for iPhone so the users can listen to the songs there as well.
http://developers.soundcloud.com/docs/api/ios-quickstart
In the example in the link the users have to sign in to listen and share, but I want my users only to listen. Is there a way around so they don't have to sign in?
Create a page that outputs the playlist in JSON, then in xcode create a class that downloads the JSON for the track dictionaries and play the downloaded content using AVPlayer (or AVQueuePlayer if you're playing the whole list).
Here's some abstract code:
playlistDownloader.m
- (void)downloadPlaylist{
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync(concurrentQueue, ^{
NSURL *url = [NSURL URLWithString:#"http://www.yourwebsite.com/playlist.json?id=1"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
id trackData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if (!error) {
tempTrackArray = trackData;
} else {
NSLog(#"Playlist wasn't able to download");
}
});
}
tempTrackArray would be a property declared in the class.
Then in your player, you'd do something like this:
audioPlayer.m
- (void)instanciateAudioPlayer
{
NSDictionary *trackDictionary = [playListDownloader.tempTrackArray objectAtIndex:0];
NSString *urlString = [trackDictionary objectForKey:#"stream_url"];
AVAsset *asset = [AVAsset assetWithURL:streamURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[avPlayer initWithPlayerItem:playerItem];
}
This is some really rough code but its the general gist of what you're trying to do. Should get you going in the right direction.
Trying the soundcloud quickstart I realised that your stream url needs to begin with https rather than http. Also you need to add your client id from your sound cloud app to the stream url:
NSDictionary *trackDictionary = [playListDownloader.tempTrackArray objectAtIndex:0];
NSString *streamURL = [trackDictionary objectForKey:#"stream_url"];
streamURL = [streamURL stringByReplacingOccurrencesOfString:#"http" withString:#"https"];
NSString *urlString = [NSString stringWithFormat:#"%#?client_id=%#", streamURL, #"a8e117d3fa2121067e0b29105b0543ef"];
From there you just setup the AVPlayer:
self._avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:urlString]];
[self setupLayer:clayer];
[self._avPlayer play];
Everything should work fine. Hope it helps
Ive been beating my head up against the all trying to figure out a weird sound issue in my program. I will post the code below. As a brief description I have a function that gets passed a variable and it takes that and decides which category of sound it should play, within that function I have multiple sounds of reach category so I use an arc4random statement then run that through a switch statement. What keeps happening is that it will play two sounds from the case statement instead of just one, most times if I call it twice with my button push the second time it will play the sound from the first time and a new sound, other times it plays the same same over the top of each other but with a slight delay. I put in a breakpoint in the switch and when it double plays it only goes through the switch once which is really confusing. One thing to note is right before this I do play another sound but it uses a separate AVAudioplayer and path variable so that shouldn't be an issue as it never plays the second sound as the other sound I'm playing. I'm only calling the function once when I press the button so I'm not sure why it does this. I have tried putting the *path and *avaudioplayer variables inside the function but it won't play at all. Searching here it seems as though arc deallocs it before it gets a chance to play it. I ended up trying to put it at the top of my .m file as a global variable then just set the actual path and play the sound within the switch. The sound will play but it plays twice.. Hopefully someone can help me out. I tried putting the avaudioplayer definition as a property as well and it does the same thing. Here is my code snippet. And thanks in advance ...
in the .m file
// Just below my synthesize statements
NSString *path;
AVAudioPlayer *theSound
// My code that I call when the button is pressed
[self playVoice:#"buyVoice"];
// The playVoice function
- (void)playVoice:(NSString*)voiceNum;
if ([voiceNum isEqualToString:#"buyVoice"]) // Bought an Item Time for a voice
{
// play coin sound
[self coinSound];
// play random buy phrase and coin sound
int phraseNumber = 0;
phraseNumber = arc4random() %(3);
switch (phraseNumber)
{
case 0:
{
path = [[NSBundle mainBundle] pathForResource:#"sndBuy1" ofType:#"m4a"];
theSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
[theSound setNumberOfLoops:0];
[theSound play];
break;
}
case 1:
{
path = [[NSBundle mainBundle] pathForResource:#"sndBuy2" ofType:#"m4a"];
theSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
[theSound setNumberOfLoops:0];
[theSound play];
break;
}
case 2:
{
path = [[NSBundle mainBundle] pathForResource:#"sndBuy3" ofType:#"m4a"];
theSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
[theSound setNumberOfLoops:0];
[theSound play];
break;
}
case 3:
{
path = [[NSBundle mainBundle] pathForResource:#"sndBuy4" ofType:#"m4a"];
theSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
[theSound setNumberOfLoops:0];
[theSound play];
break;
}
}
}
set this code before u play the Audio sound
This will reset the player to the beginning:
[theSound setCurrentTime:0.0];
[theSound play];
try this
I want to play the music from internet by url.
I create a simply project that has one button with the following code:
NSURL *url = [[NSURL alloc] initWithString:#"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie play];
It works but with some delay (I think because it waits while file has been fully downloaded).
So what I need to do that the music starts to play immediately (when, for example, 10% of file has been downloaded)?
Use -[QTMovie autoplay] to automatically play the music once enough data has been downloaded.
In your case:
NSURL *url = [[NSURL alloc] initWithString:#"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie autoplay];
From the QTMovie class reference:
The autoplay method configures a QTMovie object to begin playing as soon as enough data is available that the playback can continue uninterrupted to the end of the movie.
If you can consider displaying a Quicktime window over your app, you can use this code :
NSString *url = #"http://someURL.mp3";
UIWebView* tempAudioPlayer = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[tempAudioPlayer loadHTMLString:[NSString stringWithFormat:#"<iframe frameborder=\"0\" width=\"0\" height=\"0\" src=\"%#\"></iframe>", url] baseURL:nil];
[self addSubview:tempAudioPlayer];
I first create a UIWebView which will not be displayed. Then I loadHTMLString a <iframe> in it, with the URL of my file as the src value. And I add it to my view. Quicktime appears immediately.