I'm trying to play audio from a URL using AVPlayer when a row is selected in a table view but it doesn't play. What am I doing wrong?
Here is the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *streamingString = [NSString stringWithFormat:#"%#.json?client_id=fc886d005e29ba78f046e5474e3fdefb", [streamURLArray objectAtIndex:indexPath.row]];
NSURL *streamingURL = [NSURL URLWithString:streamingString];
NSLog(#"%#", streamingURL);
AVPlayer *player = [AVPlayer playerWithURL:streamingURL];
[player play];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
}
An example of streamingURL when I log it is this.
http://api.soundcloud.com/tracks/146814101/stream.json?client_id=fc886d005e29ba78f046e5474e3fdefb
declare AVAudioPlayer globally in your .h file
like this AVPlayer *player;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *streamingString = [NSString stringWithFormat:#"%#.json?client_id=fc886d005e29ba78f046e5474e3fdefb", [streamURLArray objectAtIndex:indexPath.row]];
NSURL *streamingURL = [NSURL URLWithString:streamingString];
NSLog(#"%#", streamingURL);
player = [AVPlayer playerWithURL:streamingURL];
[player play];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
}
You'll find numerous audio streaming kit all around GitHub.
https://github.com/tumtumtum/StreamingKit
https://github.com/muhku/FreeStreamer
https://github.com/mattgallagher/AudioStreamer
call this method
[player prepareToPlay];
before calling
[player play];
It may help to u.
Related
I get Song title in TableView using this code:
MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [everything items];
for (MPMediaItem *song in itemsFromGenericQuery) {
NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
[songTitleArray addObject:songTitle];
}
Now I want to play that song inside tableView I added Two button play and Stop how to play using Title in same ViewController.
To play song you can do something like :
MPMusicPlayerController *controller = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:arrayOfMediaItems];
MPMediaItem *item = [collection representativeItem];
[controller setQueueWithItemCollection:collection];
[controller setNowPlayingItem:item];
[controller prepareToPlay];
[controller play];
You can also use AVPlayer to play song, after picking it through MPMediaPickerController :
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker
didPickMediaItems: (MPMediaItemCollection *) collection {
MPMediaItem *item = [[collection items] objectAtIndex:0];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
[self dismissModalViewControllerAnimated: YES];
// Play the item using MPMusicPlayer
MPMusicPlayerController* appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[appMusicPlayer setQueueWithItemCollection:collection];
[appMusicPlayer play];
// Play the item using AVPlayer
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[player play];
}
I'm taking an online course and they're using outdated code using MPMoviePlayerController, and I need specific instructions on what to change it to so that it works. The app I'm making has a table view controller with an inbox with messages in it. Short video files can be sent, and when you tap on a row that has a video message, I need the video file to play. The free trial is expiring soon, so I need help please.
This is what needs to be changed in my header file:
#property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
This is what needs to be changed in my viewDidLoad method in my implementation file:
self.moviePlayer = [[MPMoviePlayerController alloc] init];
This is also what needs to be changed in the implementation file. I'm getting the videos through a backend via Parse.com, so that's what the PFFile is about:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PFFile *videoFile = [self.selectedMessage objectForKey:#"file"];
NSURL *fileUrl = [NSURL URLWithString:videoFile.url];
self.moviePlayer.contentURL = fileUrl;
[self.moviePlayer prepareToPlay];
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
}
In header file :
#property(nonatomic,strong)AVPlayer *avPlayer;
In viewDidLoad method in implementation file
self.avPlayer = [[AVPlayer alloc] init];
In implementation UITableView Method :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PFFile *videoFile = [self.selectedMessage objectForKey:#"file"];
NSURL *fileURL = [NSURL fileURLWithPath:videoFile.url];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, width, height);
[self.view.layer addSublayer: layer];
[self.avPlayer play];
}
Hope it will help you :)
I want to play music from URL with an AVPlayer on the iOS Simulator, but it doesn't work :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Track *selectedTrack = self.tracksByAlbum[indexPath.row];
NSLog(selectedTrack.preview);
NSURL *url = [NSURL URLWithString:[selectedTrack.preview stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
if (!player) {
NSLog(#"Error");
}
[player play];
}
The url is :
http://cdn-preview-6.deezer.com/stream/6d180c6b61078f437b04ec21bbe0fb76-3.mp3
I read that I have to enable user interface sounds effects on OS X, but it doesn't change anything...
If you're using ARC, you'll need to retain the AVAudioPlayer as it sets to nil automatically, which would be a likely reason for no sound. In your header file if you could make sure your self.player's property is set up like this,
#property (strong, nonatomic) AVPlayer *player;
from within your didSelectRowAtIndexPath change the AVPlayer code to this
self.player = [[AVPlayer alloc] initWithURL:url];
if (!self.player) {
NSLog(#"Error");
}
[self.player play];
I hope this helps
Here i want to play videos from server using MediaPlayer framework. 1)I am adding mediaplayer framework. 2)I am importing #import header file 3)I implemented code by using google.
My Code is :
- (void)act_GoToVideoPlayer:(UIButton*)sender
{
NSString* resourcePath = [NSString stringWithFormat:#"http://%#",[postMediaFileArr objectAtIndex:sender.tag]]; //Video file URL is getting from server and stored it in a MutableArray
NSURL *url = [NSURL URLWithString: resourcePath];
mediaplayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:str]];
[mediaplayer.view setFrame:CGRectMake(20, 100, 250, 150)];
[self.view addSubview:mediaplayer.view];
mediaplayer.fullscreen = YES;
mediaplayer.allowsAirPlay = YES;
mediaplayer.shouldAutoplay = YES;
mediaplayer.controlStyle = MPMovieControlStyleEmbedded;
}
The view goes to blank screen and infinite loading when this function is called. I have tried many other versions of this implementation and the results are all fail. The log in the is particular case is:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
If i passed any other videos from websites. it will be playing audios not videos.
Got ideas on the cause? This is my first venture into playing video and it has turned out to be a nightmare at this point.
-(void)notifyCompletelyVisible
{
NSURL *aUrl = [NSURL URLWithString:#"*YOUR URL*"];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:aUrl];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.view.frame = asyvideoImage.frame;
_moviePlayer.scalingMode=MPMovieScalingModeFill;
[self.view addsubview _moviePlayer.view];
[_moviePlayer prepareToPlay];
[_moviePlayer setFullscreen:NO animated:NO];
}
this is working just import
import "MediaPlayer/MediaPlayer.h"
you try in .h:
#import <MediaPlayer/MediaPlayer.h>
and you add delegate
interface YourViewcontroler : UIViewController <MPMediaPlayback>
{
MPMoviePlayerController *player;
}
and in your .m file:
-(void) launchVideo{
YourAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString* myURL =#"http://yourUrl/file.mp4";
NSURL *urlString = [NSURL URLWithString:myURL];
player = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
[player view].alpha=1;
[appDelegate.window.rootViewController.view addSubview:[player view]];
[player play];
}
and the delegate methode for finishing:
-(void)videoPlayBackDidFinish:(NSNotification*)notification {
[player.view removeFromSuperview];
[player stop];
player=nil;
[player release];
}
i hope this help you
I have an iOS app which works like this:
First view
3 buttons that link to different views, each has its own audio file within
Imagine user opens one view, and starts to play the audio file. When they navigate around the app, the audio file still plays which is fine, Im happy with that.
However, if user then opens the second audio file and presses play, it plays on top of the first one so 2 audios are playing together.
How do I tell the code that if another play button is pressed it should stop the first audio file that is playing??
I am using AVFoundation framework to do this
Code is as follows:
NSString *stringPath = [[NSBundle mainBundle]pathForResource:#"01" ofType:#"MP3"];
NSURL *url = [NSURL fileURLWithPath:stringPath];
NSError *error;
avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
[avPlayer setNumberOfLoops:2];
[avPlayer setVolume:self.sliderVolumeOutlet.value];
[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:#selector(updateMyProgress) userInfo:nil repeats:YES];
-(void)updateMyProgress
{
float progress = [avPlayer currentTime]/[avPlayer duration];
self.myProgressView.progress = progress;
}
- (IBAction)sliderVolumeAction:(id)sender {
UISlider *mySlider = sender;
[avPlayer setVolume:mySlider.value];
}
- (IBAction)stopButton:(id)sender {
[avPlayer stop];
[avPlayer setCurrentTime:0];
}
- (IBAction)pauseButton:(id)sender {
[avPlayer pause];
}
- (IBAction)playButton:(id)sender {
[avPlayer play];
}
- (IBAction)backButtonEvent:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
}
You can check whether it is playing the previous track or not by if ([AVPlayer rate] != 0.0)
If it is playing then you can either add the next track into queue or just use this function to replace current track with the new one - (void)replaceCurrentItemWithPlayerItem:(AVPlayerItem *)item
Perhaps creating some sort of playSound function would be better suited for this. Something like this:
- (void)playSoundWithFileName:(NSString *)fileName andExtension:(NSString *)extension {
NSString *stringPath = [[NSBundle mainBundle]pathForResource:fileName ofType:extension];
NSURL *url = [NSURL fileURLWithPath:stringPath];
NSError *error;
if (avPlayer) {
[avPlayer stop];
avPlayer = nil;
}
avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
if (error)
NSLog(#"Error");
else {
[avPlayer setNumberOfLoops:2];
[avPlayer setVolume:self.sliderVolumeOutlet.value];
[avPlayer play];
}
}
Use:
[self playSoundWithFileName:#"01" andExtension:#"MP3"];
[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:#selector(updateMyProgress) userInfo:nil repeats:YES];
and call -(void)playSoundWithFileName whenever you want to play another sound.
Or if you were using the AVPlayer class:
- (void)playSoundWithFileName:(NSString *)fileName andExtension:(NSString *)extension {
NSString *stringPath = [[NSBundle mainBundle]pathForResource:fileName ofType:extension];
NSURL *url = [NSURL fileURLWithPath:stringPath];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
if (avPlayer)
[avPlayer replaceCurrentItemWithPlayerItem:item];
else
avPlayer = [AVPlayer playerWithPlayerItem:item];
[avPlayer setVolume:self.sliderVolumeOutlet.value];
[avPlayer play];
}
Though the latter doesn't allow you set a number of loops.