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];
}
Related
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];
So I have an app where I want to include background music. I use the following code for the music:
.h: (I did also add the framework)
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
{
AVAudioPlayer *player;
}
.m:
- (void)viewDidAppear:(BOOL)animated {
NSURL *url = [[NSBundle mainBundle] URLForResource:#"song" withExtension:#"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
player.numberOfLoops = -1;
[player play];
NSLog(#"playing music");
}
The path for the music file is definitly correct. The app does not crash but I just do not hear any music.
By the way: the LogMessage ("playing music") appears in the debugger.
Any ideas?
Add prepareToPlay method before [player play];
[player prepareToPlay];
Hope this will work...
This question already has answers here:
How do I programmatically play an MP3 on an iPhone?
(4 answers)
Closed 4 years ago.
I can't figure out how to play a music file in an iPhone game.
Im creating a Game and I am trying to figure out how to play music whenever the app is launched.
I tried this:
- (void)awakeFromNib {
NSString *path = [[NSBundle mainBundle] pathForResource:#"musicamenu" ofType:#"mp3"];
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
This is how you do it. In your v1AppDelegate.h file add
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#interface v1AppDelegate : UIResponder <UIApplicationDelegate>
{
AVAudioPlayer *myAudioPlayer;
}
#property (nonatomic, retain) AVAudioPlayer *myAudioPlayer;
#property (strong, nonatomic) UIWindow *window;
#end
Now in your v1AppDelegate.m file add this
#import "v1AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#implementation v1AppDelegate
#synthesize window = _window;
#synthesize myAudioPlayer;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//start a background sound
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Soothing_Music2_Long" ofType: #"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.numberOfLoops = -1; //infinite loop
[myAudioPlayer play];
// Override point for customization after application launch.
return YES;
}
If you wish to stop or start this music from anywhere else in your code then simply add this
#import "v1AppDelegate.h"
- (IBAction)stopMusic
{
v1AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.myAudioPlayer stop];
}
- (IBAction)startMusic
{
v1AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.myAudioPlayer play];
}
I recommend to add the play music method in applicationDidBecomeActive: method. Because you want the music played every time the app is launched. Note you should hold a reference to the player. Else the music will not be played.
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Play music on another queue so that the main queue is not blocked.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self playMusic];
});
}
- (void)playMusic
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"done" ofType:#"mp3"];
NSError *error = nil;
NSURL *url = [NSURL fileURLWithPath:path];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[self.player play];
}
For Swift 3.1 :
Use this two imports :
import AVFoundation
import AudioToolbox
Create a reference for your AVAudioPlayer :
private var mAudioPlayer : AVAudioPlayer?
Use this function for play a sound you are storing in your app :
func onTapSound(){
let soundFile = Bundle.main.path(forResource: "slap_placeholder.wav", ofType: nil)
let url = URL(fileURLWithPath: soundFile!)
self.mAudioPlayer = try! AVAudioPlayer(contentsOf: url as URL)
self.mAudioPlayer?.play()
}
This is a newbie question.
I have a very simple app that is supposed to only play an audio file when a button on the main view is tapped.
I am using XCode version 4.6.
I added an audio file 'audioclip.wav' to my project. I have added the AVFoundation.framework.
My project only has ViewController.h and ViewController.m.
I double clicked and dragged from the button in the storyboard to the .h file using the assistant editor to create my IBAction connection.
My ViewController.h:
#import <UIKit/UIKit.h>
#import<AVFoundation/AVAudioPlayer.h>
#interface ViewController : UIViewController <AVAudioPlayerDelegate> {
}
- (IBAction)playAudio:(id)sender;
#end
My ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"audioclip" ofType:#"wav" ];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
}
#end
For whatever reason (probably something silly I left out) The audioclip.wav file does not play when I click the button. I have tested on the iPhone 6.1 simulator and on my device.
I think there is some problem with wav files. Some people seem to get it to work, but I've tried several different files and none of them play. The mp3 and m4a files that are commented out in the code below worked fine. There's no need to do anything with a delegate, it's optional.
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
#property (strong,nonatomic) AVAudioPlayer *audioPlayer;
#end
#implementation ViewController
- (IBAction)playAudio:(id)sender {
//NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"11 Prayer" ofType:#"mp3" ];
//NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"Sunshine" ofType:#"m4a" ];
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"space" ofType:#"wav" ];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
}
If I log the error, with the wav file I get:
Error Domain=NSOSStatusErrorDomain Code=1685348671 "The operation couldn’t be completed. (OSStatus error 1685348671.)"
Please try to use this one..
- (IBAction)playAudio:(id)sender
{
NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"audioclip" ofType:#"wav"]];
NSData *data =[NSData dataWithContentsOfURL:url];
audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
audioPlayer.delegate = self;
[audioPlayer setNumberOfLoops:0];
[audioPlayer play];
}
i hope this will help u
try like this ,
NSString *audio=[[NSBundle mainBundle]pathForResource:#"audioclip" ofType:#"wav"];
NSURL *url=[[NSURL alloc]initFileURLWithPath:audio];
avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
avPlayer.delegate = self;
[avPlayer prepareToPlay];
[avPlayer play];
and once check IBOutlet connected or not and check the button action method with breakpoints.
As i can see you have made the code quite simple, so the way it's made does require that it is not a big wav file.
And by that you need to cut down the audio file to something like 10 seconds and under.
I was using the .m4a when I encountered this problem. For me, not implementing AVAudioSession was the issue
var session = AVAudioSession.sharedInstance()
do {
session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try session.setActive(true)
} catch {
gf.displayAlert("problem encountered", userMessageTitle: "")
self.navigationController?.popViewControllerAnimated(true)
}
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];
}