AVPlayer only plays in simulator - iphone

I am downloading an MP3 file using NSURLConnection and I save the data to a file using NSFileHandle. Whenever a certain percentage of the file is downloaded (all the files are roughly the same size), I will start playing the MP3 file.
I thought that I could use the AVPlayer to do this but it doesn't work on a device - only in simulator.
I do the following:
if (self.player.rate == 0.0f && percentage > kMinimumDownloadPercentage)
{
NSError *error;
self.player = [AVPlayer playerWithURL:[[NSURL alloc] initFileURLWithPath:localPath]];
if (!error)
[self.player play];
else
NSLog(#"%#", error);
}
This works in simulator but not on device. No error is given. It does work if I do the following, though:
self.player = [AVPlayer playerWithURL:url];
Where url is the remote URL to the MP3 file.
Does anyone know why I can't play the local file using AVPlayer?

This turned out to be an error due to the way the file was saved.
-playerWithURL: in AVPlayer works perfectly fine.

Related

AVPlayer not finishing the stream track

I am using AVPlayer to play long audio mp3 stream music (8 minutes), short musics (1 to 3 minutes) plays perfectly, but with these bigger musics the music starts playing, but after play some random minutes (between 2 and 3:20) the player starts the track again from the beginning. Although the player restart the music, the status information (duration and current time) keeps counting normally, without restart, just the audio restarts. Someone has an idea?
The file I am playing is this: http://storage-new.newjamendo.com/download/track/57864/mp31/
The player code:
AVAudioSession *mySession = [AVAudioSession sharedInstance];
// Assign the Playback category to the audio session.
NSError *audioSessionError = nil;
[mySession setCategory: AVAudioSessionCategoryPlayback
error: &audioSessionError];
if (audioSessionError != nil) {
NSLog (#"Error setting audio session category.");
return;
}
// Activate the audio session
[mySession setActive: YES
error: &audioSessionError];
if (audioSessionError != nil) {
NSLog (#"Error activating audio session during initial setup.");
return;
}
player = [AVPlayer playerWithURL:[NSURL URLWithString:url]];
[player play];
And here is how I track the information about the current time, that keeps counting normally.
AVPlayerItem *item = player.currentItem;
CMTime duration = [item duration];
CMTime currentTime = [item currentTime];
Seems that the problem is that the mp3 file I was streaming was a mpeg-1 and the Apple documentation says that I have to use mpeg-2, so I have just changed to the correct version, and the error is not happening again.
I have managed to fix this problem, it would appear (for me) that the server was sending the file as one block.
I don't know the ins and outs but the stream is now provided as a "ranged" stream (it is provided in segments I am told). Not only did this solve the problem but such things below started to work for me (as apposed to providing NaN).
float duration = CMTimeGetSeconds(self.avQueuePlayer.currentItem.duration);
I hope this helps, I am not being told much about what was changed on the server. Strangly my URL links worked on just about all other devices but I got this problem when playing from my App AND Safari.

Play remote mp3 using iOS

I have a remote mp3 (small file, just a few seconds) which I have the URL.
I need to play it on an App that uses iOS 4.
The URL is not exactly a .mp3 file, but it is a dynamic .php url that requests the .mp3 file. If I put that php route in the browser it downloads the mp3 file.
I am trying to use this project (https://github.com/mattgallagher/AudioStreamer) but it isn't working. The audio starts but it only plays a part of it.
How can I do it?
If it's truly just a small (and static, non-streaming) mp3 file, why not consider doing something like:
NSError * error = nil;
AVAudioPlayer * avPlayerObject =
[[AVAudioPlayer alloc] initWithContentsOfURL: yourRemoteURL error:&error];
if(avPlayerObject)
{
[avPlayerObject play];
}
Check out Apple's AVAudioPlayer class (documentation linked for you).

iOS record video and play MP3 at the same time

I need to record video from the iPhone camera, and play an MP3 file at the same time.
I started with AVCam sample code, which I'm sure you all have. It works great for recording video.
However, I then added the following code to play an MP3. This MP3-playing code works in a different app of mine, but when I insert it into this sample code the MP3 not only does not play, the AVCamCaptureManager's recordingDidFinishToOutputFileURL never gets called, so the video never gets saved out.
It's like the audio playing code conflicts with the video capture code. Any ideas?
Here's the audio playing code I put into AVCam:
AVAudioPlayer *audioPlayer = nil;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/soundeffect.mp3", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer setVolume:1.0];
[audioPlayer prepareToPlay];
[m_audioPlayer play];
Pretty simple code here. Not sure why this audio playing code causes recordingDidFinishToOutputFileURL to never get called...
Thanks for any ideas.
I figured it out: I just had to remove some code within AVCam that allocated AVCaptureDeviceInput - audioInput. That was unnecessary and conflicted with my audio playback code.

Unable to play .wav file Using AVPlayer

I have some wav files and they completely working with MPMoviePlayer. but not working with AVPlayer. For some reason I have changed my player to AVPlayer. But after implementation I found that some of my audio files are not working with AVPlayer.I have used apples sample code of AVPlayer. Any suggestion would be appreciated.
Does other format work ? Perhaps it's a coding problem.
Anyway. If your WAV files are ok, import them into iTunes, convert them once (right click on the file into the library), and convert the converted one back to wav (same method). You should then have an iPhone compatible WAV file.
If that does not work, that should be a coding problem :-)
Try :
NSString* path = [[NSBundle mainBundle] pathForResource:soundfileName ofType:fileType];
if ([[NSFileManager defaultManager] fileExistsAtPath:path] == NO) NSLog (#"No file");
AVPlayer* player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]];
[player play];

How can I make safe mp3 files for AVAudioPlayer, without getting errors

I have problem when trying to play some mp3 files on iOS app.
First, I noticed that these files was also not working on Safari for iOS, but now I am having files that works on Safari for iOS, and doesn't work on my App.
I am using thi code to play mp3 audio files:
NSString *pronociationPath = …..
if (pronociationPath != nil)
{
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:pronociationPath];
NSError *error;
self.player=[[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error] autorelease];
[fileURL release];
[self.player setNumberOfLoops:0];
if (self.player == nil) {
NSLog(#"%#",error);
}
else
{
[self.player prepareToPlay];
[self.player play];
}
}
And I am having this error for a file that works on Safari iOS:
Error Domain=NSOSStatusErrorDomain Code=1685348671 "The operation
couldn’t be completed. (OSStatus error 1685348671.)"
Is something wrong with my code?
Is there any guide to create safe mp3 files for iOS Apps ?
I've seen Apple documentation, but I don't see any useful informations https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW28
I also received same error (OSStatus error 1685348671) when I tried to play mp3 files on iOS 4.3.
Later on I found that it was the mp3 sound problem which made it incompatible to be played in iOS 4.3. So, I used iTunes Player to convert new version of mp3 sound with existing one. Now it works fine and better.
Steps:
Open your current mp3 sound in iTunes
Select it and Click on "Advanced" Menu >> "Create MP3 Version"
It wil create copy of current audio. Use newly created mp3 file for your app.
It helped me. I hope it might help others as well. All the best!