I'm trying to create an iOS/iPhone radio app using Xcode 4.5.2.
I wanted to stream #"http://xx.xxxxxxx.com/8111/radio.m3u" with play, pause, volume control and able to play on background feature/multitasking.
I've added AVFoundation, Mediaplayer and AudioToolBox frameworks thus far. I've added play, pause and slider objects to xib.
ViewController.h
#interface ViewController : UIViewController
#property (strong,nonatomic) MPMoviePlayerController *myPlayer;
#property (weak, nonatomic) IBOutlet UISlider *myslider;
- (IBAction)playButtonPressed;
- (IBAction)myslider:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController ()
{
UISlider *volumeSlider;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
}
- (IBAction)playButtonPressed;
{
NSString *urlAddress = #"http://xxxxxxx.com/8111/listen.m3u";
NSURL *url = [NSURL URLWithString:urlAddress];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:url];
player.movieSourceType = MPMovieSourceTypeStreaming;
[player prepareToPlay];
self.myPlayer = player;
[self.view addSubview:self.myPlayer.view];
[self.myPlayer play];
}
- (IBAction)stopButtonPressed;
{
[self.myPlayer stop];
}
- (IBAction)myslider:(id)sender
{
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectMake(10, 10, 200, 40)];
[volumeSlider addSubview:volumeView];
[volumeView sizeToFit];
}
There are Two way to achieve this.
You can directly load you URL in UIWebView and it will properly.
You can also use MPMoviePlayerController.
Create a "MPMoviePlayerController *player" as a strong object in your ViewController.
So you code would look something like below:
#interface ViewController ()
{
UISlider *volumeSlider;
MPMoviePlayerController *player;
}
#end
- (IBAction)playButtonPressed;
{
NSString *urlAddress = #"http://xxxxxxx.com/8111/listen.m3u";
NSURL *url = [NSURL URLWithString:urlAddress];
if(nil != player)
{
player = nil; // Alternatively you can stop and restart with the different stream.
}
player = [[MPMoviePlayerController alloc]initWithContentURL:url];
player.movieSourceType = MPMovieSourceTypeStreaming;
[player prepareToPlay];
self.myPlayer = player;
[self.view addSubview:self.myPlayer.view];
[self.myPlayer play];
}
Related
Hi I have an iOS Objective-C app which does not use Storyboards (It uses XIB files)
I want to add a splash screen to play a video so I added a new Coca Touch Class derived from UIViewController (And ticked 'Also create XIB file).
I have swapped this class as my new main screen and it loads properly but does not play the video. I have added the AVFoundation framework etc so that is not an issue.
Here is my code.
.h File
#import <UIKit/UIKit.h>
#interface VideoIntroViewController : UIViewController
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#end
.m file
import <AVFoundation/AVFoundation.h>
static const float PLAYER_VOLUME = 0.0;
#interface VideoIntroViewController ()
#property (nonatomic) AVPlayer *player;
#property (weak, nonatomic) IBOutlet UIView *playerView;
#end
#implementation VideoIntroViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self createVideoPlayer];
}
- (void)createVideoPlayer
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"welcome_video" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:filePath];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player.volume = PLAYER_VOLUME;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.videoGravity = UIViewContentModeScaleToFill;
playerLayer.frame = self.playerView.layer.bounds;
[self.playerView.layer addSublayer:playerLayer];
[self.player play];
}
The screen launched but no video plays.
What could be going wrong?
Try this code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"welcome_video" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:filePath];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
CALayer *superlayer = self.playerView.layer;
self.player.volume = 1.0;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;// you can also use AVLayerVideoGravityResizeAspect to clip video to view's bound
playerLayer.frame = self.playerView.layer.bounds;
[superlayer addSublayer:playerLayer];
[self.player seekToTime:kCMTimeZero];
[self.player play];
I have checked it for some other video file and it is working fine.
In my main XIB file I did not have a View control placed on it.
When I placed a View Control and then CTL+Right clicked and connected it to my playerView it then played the video.
I try to play audio from server url but nothing play, But i try to play audio from document directory path url and it play fine.
NSData *songData=[NSData dataWithContentsOfURL:[NSURL URLWithString:
[NSString stringWithFormat:#"%#",aSongURL]]];
AVAudioPlayer *abc = [[AVAudioPlayer alloc] initWithData:songData error:nil];
abc.numberOfLoops=0;
[abc prepareToPlay];
[abc play];
You can try this. Hope it helps.
AVPlayer *objAVPlayer = [[AVPlayer alloc] initWithURL:url];
[objAVPlayer play];
My answer is below
ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
#property (nonatomic,strong)AVAudioPlayer *player;
- (IBAction)actionPlayAudio:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize player;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)actionPlayAudio:(id)sender {
NSString *strAudioFileURL = #"dev.epixelsoft.co/love_app/audio/img_14957068361.caf";
NSURL *soundFileURL = [NSURL fileURLWithPath:strAudioFileURL];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
[player play];
}
My issue is the following when I get the file from the bundle I don't have any issue to play it with the AVPlayer.
But when I use the file sharing with iTunes and I try to play the file from the document folder and it doesn't work.
.h
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#property (strong, nonatomic) AVPlayerViewController *videoController;
#property (strong, nonatomic) AVPlayer *player;
.m
The url get is the following (already try with mov , mp4 and mpg) : file:///var/mobile/Containers/Data/Application/D35C60F9-6656-421C-8798-FB938321C86E/Documents/foo.mpg
- (IBAction)playVideo:(id)sender {
if(videoArray && [videoArray count] >= 1){
if(!self.videoController){
self.videoController = [AVPlayerViewController new];
}
// create an AVPlayer
NSURL *videoURL = [NSURL fileURLWithPath:videoArray[0]];
//self.player = [AVPlayer playerWithURL:videoURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:videoURL];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.videoController.player = self.player;
[self.videoController.player play];
//listen for end
//[self.videoController.player addObserver:self forKeyPath:#"status" options:0 context:0];
// show the view controller
self.videoController.showsPlaybackControls = false;
self.videoController.view.frame = self.view.frame;
[self addChildViewController:self.videoController];
[self.view addSubview:self.videoController.view];
}
}
This is my code,
#interface ViewController ()
{
AVPlayer *avPlayer;
}
#end
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIView *containerView = [[UIView alloc] initWithFrame:self.view.frame];
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"TestVideo" ofType:#"mp4"];
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:filepath]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayerLayer.frame = self.view.frame;
[containerView.layer addSublayer:avPlayerLayer];
[self.view addSubview:containerView];
[avPlayer play];
}
That really shouldn't work on any iOS version as
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
is going out of scope and being deallocated at the end of viewDidAppear.
You should assign the AVPlayer to a class member variable, to stop this happening.
Otherwise there may be a problem with the video file.
Your AVPlayer item needs a strong reference as it sets to nil automatically
Please change
#interface ViewController ()
{
AVPlayer *avPlayer;
}
#end
to this
#interface ViewController ()
#property (strong, nonatomic) AVPlayer *avPlayer;
#end
and change all avPlayer reference to self.avPlayer
I hope this helps.
Is it possible to make the video not close after it has finished playing? So this would allow the user to press "done" when the video is finished and then the video will close.
ViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface player1: UIViewController {
}
-(IBAction) playMovie;
#end
ViewController.m
#import "ViewController.h"
#implementation player1
-(IBAction) playMovie {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"2.DragtoopenfileinaVM" ofType:#"mp4"]];
MPMoviePlayerViewController *windowsmac = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:windowsmac];
windowsmac.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[windowsmac.moviePlayer play];
windowsmac = nil;
}
This is the coding I'm using, could someone help?
moviePlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
[[NSNotificationCenter defaultCenter] removeObserver:moviePlayer
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer.moviePlayer];
[self presentViewController:moviePlayer animated:YES completion:nil];