I implemented a MPMoviePlayerViewControllerlike this:
ViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UITableViewController
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
/* SOME CODE AND A TAPGESTURERECOGNIZER */
/* CALLS [self playVideo] */
-(void) playVideo{
#try {
NSURL *URL = [NSURL URLWithString:#"myGeneratedURL"];
NSLog(#"URL: %#",URL);
MPMoviePlayerViewController *video = [[MPMoviePlayerViewController alloc]
initWithContentURL:URL];
[self.navigationController presentMoviePlayerViewControllerAnimated:video];
}
#catch (NSException *exception) {
}
}
So now my problem is that it works just fine on both simulator and on my iphone (if i debug it), but when i upload a build to itunesconnect and download it via TestFlight the MPMoviePlayerViewController does not show up. The URL in NSLog is definitely the same in all three cases ( sim , iphone debug, testflight ).
I don't know where i should start searching the error...
I would suggest using strong reference rather than an local variable.
You can follow this tutorial
Related
Xcode app doesn't play MP3 music file on iphone6+ or simulator.
Just trying to play a simple MP3 file when app starts.
Was working. Tried previous working version, and it won't play.
MP3 filename is d.mp3
It plays if I double-click it in Xcode project.
Xcode top project groups:
ParticleSDK
...
myApp
d.mp3
all my .h and .c
Pods
I selected: TARGETS > myApp
Build Phases > Link Binary With Libraries:
AVFoundation
CoreGraphics
UIKit
Foundation
// .h:.............................
#import <UIKit/UIKit.h>
#interface SparkViewController : UIViewController
#end
// .m:........................................
#import "SparkViewController.h"
#import "Spark-SDK.h"
#import <UIKit/UIKit.h>
#import "AVFoundation/AVAudioPlayer.h"
#interface SparkViewController () <AVAudioPlayerDelegate>
{
AVAudioPlayer *AV_audio_player; // strong reference
}
#property (weak, nonatomic) IBOutlet UILabel *loggedInLabel;
#property (nonatomic, strong) id eventListenerID_A;
#property (nonatomic, strong) id eventListenerID_B;
#end
#implementation SparkViewController
- (void)viewDidLoad
{
[super viewDidLoad];
////////////////////////// P L A Y M P 3 F I L E ///////////////////////////
NSURL *url = [[NSBundle mainBundle] URLForResource:#"d" withExtension:#"mp3"];
printf("\n url = 0x%x \n", (int)url );
NSError* the_error;
AV_audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error: &the_error];
NSLog(#" Error is: %#", the_error );
AV_audio_player.delegate = self;
[AV_audio_player play];
printf( "\n %s AV_audio_player playing.", __FUNCTION__ );
printf("\n AV_audio_player = 0x%x", (int)AV_audio_player );
}
OUTPUT LOG...................................
-[SparkViewController viewDidLoad] ************* v 2016-09-19 X1 ************
url = 0x34e5db80
2016-09-19 12:18:41.805 AudMan1 x1[385:60539] Error is: (null)
-[SparkViewController viewDidLoad] AV_audio_player playing.
AV_audio_player = 0x34e51790
All the evidence given in your question would seem to be that the audio player is playing, or at least is starting to play. Perhaps some other code then comes along and stop it, or sets AV_audio_player and thus kills the current audio player — but that would be other code which you have not shown, so it would be impossible for us to know about it. Only you can find it. When you do, the problem will be solved.
(Of course, you might also have turned on the Silent switch on your iPhone, or turned the volume all the way down. So do also look for a hardware reason why the sound might not be audible.)
New to iOS and Objective-C, have been struggling to figure this out. I have a class that holds a strong reference to an AVAudioPlayer object and defines a method to play an mp3 depending on the parameter 'tag', which belongs to a UIButton. In my view controller I have a method that uses this class to play a sound when a button is pressed. But when I run the simulator and press the button, the mp3 is not being played. When I don't use the other class and make the AVAudioPlayer belong to my ViewController, initialize it in viewDidLoad, and call play right in the IBAction method, it works fine. I checked that the files are available to my project and that they are being referenced correctly in the code.
I looked around and found this and this, neither solved my problem. Here's my code
GuitarTuner.h
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface GuitarTuner : NSObject
- (void) play: (NSUInteger)tag;
#end
GuitarTuner.m
#import "GuitarTuner.h"
#import <AVFoundation/AVFoundation.h>
#interface GuitarTuner()
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
#end
#implementation GuitarTuner
- (void) play:(NSUInteger)tag
{
NSString *note;
switch (tag) {
case 0:
note = #"Low-E";
break;
case 1:
note = #"A";
break;
case 2:
note = #"D";
break;
case 3:
note = #"G";
break;
case 4:
note = #"B";
break;
case 5:
note = #"Hi-E";
break;
}
NSString *path = [[NSBundle mainBundle] pathForResource:note ofType:#"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:path];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
[self.audioPlayer play];
}
#end
ViewController.m
#import "ViewController.h"
#import "GuitarTuner.h"
#interface ViewController ()
#property (strong, nonatomic) GuitarTuner *tuner;
#end
#implementation ViewController
- (GuitarTuner *) tuner
{
if (!_tuner) return [[GuitarTuner alloc] init];
return _tuner;
}
- (IBAction)noteButton:(id)sender
{
UIButton *button = (UIButton*)sender;
[self.tuner play:button.tag];
}
#end
Thanks in advance
EDIT:
Silly mistake! Just didn't properly initialize GuitarTuner property in the getter in ViewController -- should be _tuner = [[GuitarTuner alloc] init] Answer below works too.
Try to initialise AVAudioPlayer like this
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:note withExtension:#"mp3"] error:&error];
UPDATE:
You give yourself your answer:
When I don't use the other class and make the AVAudioPlayer belong to
my ViewController, initialize it in viewDidLoad, and call play right
in the IBAction method, it works fine.
Try to alloc tuner in viewDidLoad or create from your class GuitarTuner a singleton and from there everything will be much easier.
Also comment this:
- (GuitarTuner *) tuner
{
if (!_tuner) return [[GuitarTuner alloc] init];
return _tuner;
}
OBJECTIVE :
I am trying to get a sound to play when a button is clicked. This sound will be preformed by an action called "play". The sound is in my resource folder and is called SoftClick.mp3
PROBLEM:
There is no sound at all being played in either simulator or the real device. The code does not give any errors.
CONNECTIONS:
The button, that needs to give sound, is connected to the action play and is used as a Modal Segue to show another view controller. Both viewcontrollers use the same .h and .m script.
CODE FOR .H
#import <UIKit/UIKit.h>
#import<AVFoundation/AVAudioPlayer.h>
#interface ViewController : UIViewController
{
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
- (IBAction) play;
#end
Action and audioplayer are defined as i thought was necessary (?)
THE CODE FOR .M
#import "ViewController.h"
#import<AVFoundation/AVAudioPlayer.h>
#interface ViewController ()
#end
#implementation ViewController
#synthesize audioPlayer;
-(IBAction)play
{
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"SoftClick"ofType:#"mp3"]] error:NULL];
[theAudio play];
}
Can someone please help me with correcting my coding so this could work in xcode5?
Ive spend some hours trying to figure out why this will not work an tried combinations like without segue etc. I do not want to steal the code from someone else and am trying to build this on my own.
Thanks in advanced,
Lien
The issue, as I'm assuming you're using ARC, is that you aren't retaining the AVAudioPlayer object. You create the object, call play, but then it is instantly released and dealloc'd. You need to add self.audioPlayer = theAudio;
I am new to Xcode so I've been trying to follow along to tutorials but haven't come across any that explain what I'm trying to achieve. I just want to play a video automatically when you open an application. I have it somewhat working in that the audio plays, but I cannot see any video. Am I missing something? I am getting this Warning in my output window:
Warning: Attempt to present MPMoviePlayerViewController: 0x831d7a0 on ViewController: 0x9d10540 whose view is not in the window hierarchy!
In my ViewController.h:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Placeholder" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Instead of
[playercontroller.moviePlayer play];
do
[playercontroller play];
Side note:
Also check up on your naming convention. In Objective-C variable names "should" have capital letter on each new word in the variable. E.g. instead of playercontroller use playerController.
I know your status.
it's warning from Xcode.
So, you just change the code.
like this,
[playercontroller performSelector:#selector(player)];
For example, on the iOS SDK download page, there's sample code; I'm using the calculator app (iPhoneUnitTests). I would like to know if it is possible to add sounds to the button presses on the app that's already built, easily.
It is actually very simple to play short sounds, like button sounds. Here is a quick example.
You must link the AudioToolbox.framework binary
SoundExampleViewController.h;
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface SoundExampleViewController : UIViewController{
SystemSoundID mySoundID;
}
#end
SoundExampleViewController.m:
#import "SoundExampleViewController.h"
#implementation SoundExampleViewController
- (void)viewDidLoad{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"SoundFileName" ofType:#"wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&mySoundID);
}
- (void)viewDidUnload{
[super viewDidUnload];
AudioServicesDisposeSystemSoundID(mySoundID);
}
#end
Then to play you simply call: AudioServicesPlaySystemSound(mySoundID);