How can i hide MPMoviePlayer controls? - mpmovieplayer

i want put a movie at my application starting but i want hide the movie controls like (stop , play , next and ... ) here is my code :
#import <MediaPlayer/MediaPlayer.h>
-(void)viewDidLoad
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"myvideo" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[IntroMovie play];
}

You could do IntroMovie.controlStyle = MPMovieControlStyleNone;
Since .movieControlMode was deprecated.

Ok, i figured it out.
Code:
IntroMovie.movieControlMode = MPMovieControlModeHidden;

In Objective-C
moviePlayer.controlStyle = MPMovieControlStyleNone;
In Swift
moviePlayer.controlStyle = MPMovieControlStyle.None

Related

Unable to play mp4 videos in AVPlayer

I'm using AVPlayer to play videos. I couldn't play .mp4 videos.I'm getting this black screen. here is my code.
`NSString *filePath = [self.videoArray objectAtIndex:index];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: #"mp4];
url = [[NSURL alloc] initFileURLWithPath: soundFilePath];
playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.videoGravity = AVLayerVideoGravityResize;
playerViewController.view.frame = self.view.bounds;
playerViewController.showsPlaybackControls = NO;
video=[AVPlayer playerWithURL:url];
playerViewController.player = _video;
playerViewController.view.userInteractionEnabled = false;
[video play];
[self.view addSubview:playerViewController.view];`
Try this code I think it would be working for you, because It seems your URL is not for bundle its from web.
// your filepath which is may be "http" type
NSString *filePath = [self.video_Array objectAtIndex:index];
// http to NSURL using string filepath
NSURL *url= [[NSURL alloc] initWithString:filePath];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.videoGravity = AVLayerVideoGravityResize;
playerViewController.view.frame = self.view.bounds;
playerViewController.showsPlaybackControls = NO;
video=[AVPlayer playerWithURL:url];
playerViewController.player = _video;
playerViewController.view.userInteractionEnabled = false;
[video play];
[self.view addSubview:playerViewController.view];

Play sound when app is in background or screen is locked

I have a timer that plays a sound when time reaches 0, but now I can't understand how to fire the event in order to play the sound if the app is in background mode or the screen is locked, What I found searching on the web was to set "UIBackgroundModes" in my .plist file, and add "audio" as array member.
UIBackgroundModes Array
Item0 String audio
Then add in the app delegate the following code:
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
But the sound doesn't get played.. So I was wondering if it's possible to do something like this or I should change method
EDIT:
I imported:
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
And this is the code I use to create the sound:
AVAudioPlayer *player;
NSString *soundFilePath = [NSString stringWithFormat:#"%#/clock.mp3",
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = -1;
dispatch_async(dispatch_get_main_queue(), ^{
[player prepareToPlay];
});
Then to play it:
[player play];
I have just a snippet of Swift code but it should be easy to convert it to Obj-C
Try to use AVPlayerItem init with URL and then set AVPlayer init with PlayerItem
let assetUrl = self.nowPlayingItem!.associatedItem!.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL
let playerItem = AVPlayerItem(URL: assetUrl)
if let player = self.player {
player.replaceCurrentItemWithPlayerItem(playerItem)
} else {
self.player = AVPlayer(playerItem: playerItem)
}
self.player!.play()
and for this line of code:
NSString *soundFilePath = [NSString stringWithFormat:#"%#/clock.mp3",
[[NSBundle mainBundle] resourcePath]];
Have you tried:
[[NSBundle mainBundle] URLForResource:#"song" withExtension:#"mp3"];
// Create a new player item
NSURL *assetUrl = [nowPlayingItem valueForProperty:MPMediaItemPropertyAssetURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:assetUrl];
// Either create a player or replace it
if (self.player) {
[self.player replaceCurrentItemWithPlayerItem:playerItem];
} else {
self.player = [AVPlayer playerWithPlayerItem:playerItem];
}
nowPlayingItem is here a MPMediaItem but just replace NSURL *assetUrl = [nowPlayingItem valueForProperty:MPMediaItemPropertyAssetURL]; by NSURL *assetUrl =[[NSBundle mainBundle] URLForResource:#"song" withExtension:#"mp3"];

Play Audio iOS Objective-C

I'm trying to get an audio file playing in my iOS app. This is my current code
NSString *soundFilePath = [NSString stringWithFormat:#"%#/test.m4a", [[NSBundle mainBundle] resourcePath]];
NSLog(#"%#",soundFilePath);
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer setVolume:1.0];
audioPlayer.delegate = self;
[audioPlayer stop];
[audioPlayer setCurrentTime:0];
[audioPlayer play];
I've being checking a bunch of other posts but they all generally do the same thing. I'm not getting an error, but I don't hear any audio playing on the simulator or device.
This is the path I get for the audio file on the simulator
/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/long-numbered-thing/BookAdventure.app/test.m4a
Any help is appreciated!
Maybe you should try using the NSBundle method pathForResource:ofType: method to create the path to the file.
The following code should successfully play an audio file. I have only used it with an mp3, but I imagine it should also work with m4a. If this code does not work, you could try changing the format of the audio file. For this code, the audio file is located in the main project directory.
/* Use this code to play an audio file */
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
EDIT
Ok, so try this:
NSString *soundFilePath = [NSString stringWithFormat:#"%#/test.m4a",[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
Also, make sure you do the proper imports:
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
You need to store a strong reference to 'AVAudioPlayer'
#property (strong) AVAudioPlayer *audioPlayer;
And if you are sure that your audio file is in following Bundle Resources, it should play.
Project>Build Phases>Copy
To play file with extension .caf, .m4a, .mp4, .mp3,.wav, .aif
Download following two files from GitHub
SoundManager.h
SoundManager.m
Add these files to your project
Also add audio files to resource folder (sample files)
mysound.mp3, song.mp3
Import header file in desired file
#import "SoundManager.h"
And add following two lines in -(void)viewDidLoad
[SoundManager sharedManager].allowsBackgroundMusic = YES;
[[SoundManager sharedManager] prepareToPlay];
Use following line to play sound (mysound.mp3)
[[SoundManager sharedManager] playSound:#"mysound" looping:NO];
Use following line to stop the sound (mysound.mp3)
[[SoundManager sharedManager] stopSound:#"mysound"];
Also you can play music (song.mp3) as
[[SoundManager sharedManager] playMusic:#"song" looping:YES];
And can be stop music as
[[SoundManager sharedManager] stopMusic];
Complete Documentation is on GitHub
First import this framework to your file
#import <AVFoundation/AVFoundation.h>
Then declare an instance of AVAudioPlayer.
AVAudioPlayer *audioPlayer;
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Name of your audio file"
ofType:#"type of your audio file example: mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
To play your bundle audio file using below code if you generate the system sound ID
for(int i = 0 ;i< 5;i++)
{
SystemSoundID soundFileObject;
NSString *audioFileName = [NSString stringWithFormat:#"Alarm%d",i+1];
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: audioFileName
withExtension: #"caf"];
// Store the URL as a CFURLRef instance
CFURLRef soundFileURLRef = (__bridge CFURLRef) tapSound;
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (
soundFileURLRef,
&soundFileObject
);
[alarmToneIdList addObject:[NSNumber numberWithUnsignedLong:soundFileObject]];
}
You can play the sound by using below code
AudioServicesPlaySystemSound([[alarmToneIdList objectAtIndex:row]unsignedLongValue]);
To achieve this below framework need to add in your Xcode
AudioToolbox
Finally below header files are needs to be import in controller
#import AudioToolbox/AudioToolbox.h
#import AudioToolbox/AudioServices.h
Updated for Swift 4
Playing from main bundle
- iOS 11 only
import AVFoundation
var player: AVAudioPlayer?
The following code loads the sound file and plays it, returning an error if necessary.
guard let url = Bundle.main.url(forResource: "test", withExtension: "m4a") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
guard let player = player else { return }
player.play()
} catch let error {
// Prints a readable error
print(error.localizedDescription)
}
Swift 4.2
var soundFilePath = "\(Bundle.main.resourcePath ?? "")/test.m4a"
var soundFileURL = URL(fileURLWithPath: soundFilePath)
var player = try? AVAudioPlayer(contentsOf: soundFileURL)
player?.numberOfLoops = -1 //Infinite
player?.play()

Objective C error when playing multiple sounds

Hi i am trying to play many sounds using AVAudioPlayer and foundation.
The code is this
- (IBAction)pushButton19 {
NSString *path = [[NSBundle mainBundle] pathForResource:#"The Terminator" ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
- (IBAction)pushButton20 {
NSString *path = [[NSBundle mainBundle] pathForResource:#"The Wizard of Oz" ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
on the line theAudio.delegate = self;
it says
class * does not implement the
protocol
as well as
Semantic Issue Assigning to ID
from
incompatible type MainView *
what do i need to do to fix it and can u show me the correct code as im a noob at this?
Your controller must conforms to the AVAudioPlayerDelegate protocol and implements its necessary methods. This is a sample code for playing sound.

Can I play loops using Audio Toolbox?

in my app i use AudioToolbox framework and i need to play a loop can i ?
this is the AVAudioplayer code that goes slowly with 2 touches simultaneously
- (AVAudioPlayer *)getNoteFromFilename:(NSString *)name andoftype:(NSString *)type{
NSString *soundPath = [[NSBundle mainBundle] pathForResource:name
ofType:type];
NSURL *fileURL = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *note = [[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
error:nil] autorelease];
return note;
}
in the viewdidload
ViewDidLoad {
self.Note = [self getNoteFromFilename:#"Note" andoftype:#"wav"];
}
calling it by this in IBAction :
[Note prepareToPlay];
[Note play];
NB: i have retaind all the button and the AVAudioPlayer
Use AVAudioPlayer instead. There you can easily loop using the numberOfLoops property.