How to debug a AVAudioPlayer play failure? - iphone

I'm using an AVAudioPlayer to play a mono AIFF file from my app's Documents directory.
On the simulator, [player play] returns YES and I hear the file playing. On the device the play method returns NO and nothing happens. I grabbed the file via the Organizer - it plays back fine on my Mac and seems to be well-formed. I realize the simulator can access codecs that aren't available on the iPhone, but that shouldn't matter in this case, should it?
I'm at a loss as to how to debug this. Any suggestions?

I asked this question on Apple's forum and someone there asked if I had activated an Audio Session. I hadn't, and doing so fixed my problem.

Make sure you do not use an absolute path to the audio file. When you move to device, the paths change. You need to get the path to the documents folder using the standard methods and then add the relative path to your audio file. That way you get a correct path regardless of the hardware or other changes.

Create a delegate to the audio player, and override -audioPlayerDecodeErrorDidOccur:error: to see if any error happens.

Related

Audio disappears in Unity after Project Setting change

I have a Audio Source linked to an Audio Listener looping some music. The problem with the audio is that I cannot hear anything. There are no error messages. I have tried muting the audio and then undoing it. I have also tried to restart Unity and adjusting the values on the Audio Source including pitch, volume, and looping, but the problem continues on. What is even more confusing is that when I started a project, the audio was working well, and then I did some adjusting on the Project Settings: Physics, the audio suddenly stopped. I do not have any code involved in the audio. When I tried to add another Audio Source and testing it to see if the Audio Source is the problem, it still creates no sound. I believe the problem lies nowhere near the Audio Source (though I might be wrong :D). Is there any other way to mute audio that I have missed? Thanks for any help.
Check that the Global Volume Setting under Project settings > Audio and that it is not set to 0. That worked for me.

Using a webview to play an mp3- is it possible to keep it playing even when the screen is locked?

I have a portion of my app that simply launches a webview linked to an mp3 file- it opens the file and plays the audio. What I'd like to do is allow that audio to continue playing even if the user locks their iPhone. (As a side note, is there any way to replace the default Quicktime logo that appears in the player? Not that big a deal, just thought I'd ask =)
A third question- I also do the same thing (webview) with an .m3u linked file to stream live audio throughout the week- is it also possible to keep it playing when the iPhone is locked?
Thanks!!
Ben
Quick Edit: The same URLs work great in IOS Safari (locking and unlocking doesn't affect the audio. Just not in my app's webviews.
I did find my answer though, in case anyone comes here looking. Easiest thing in the world too- just add a row to the plist file labeled "Required Background Modes" and under that set item 0's value to "App plays audio". Voila!
Since the audio is in a UIWebView, I don't believe that you'd be able to play it in the background.
EDIT: I was wrong, adding "App Plays Audio" to the UIBackgroundModes will make it work even when audio is in a UIWebView.
Have you thought about just streaming the .mp3 file yourself? Should be easy enough and you'd be able to setup background streaming. To get you started, I'd check out Matt Gallagher's AudioStreamer class which easily allows you to stream .mp3 files and other audio files from the net: https://github.com/mattgallagher/AudioStreamer

Trying to play an audio file

I'm trying to play an audio file in a cocos2d application. Here is the line which tries to play the sound:
[[SimpleAudioEngine sharedEngine] playEffect:#"pig_squeal.wav"];
If I put a log near this line, the log appears, and I can play the sound with iTunes. But when the sound should be played, there is a message displayed:
AudioStreamBasicDescription: 2 ch, 44100 Hz, 'lpcm' (0x00000C2C) 8.24-bit little-endian signed integer, deinterleaved
What's the problem?
This was discussed in comments but I've amalgamated all the possibilities of why it may not work here.:
That's not an error message, just some information.
Is the sound definitely in your library and a part of the target?
You haven't changed the volume of the SimpleAudioEngine or the volume of your device isn't all the way down?
Click the sound in your library. Press Option+Command+1 to bring up the file editor. Scroll down to "Target Membership" and ensure the sound is checked for your target.
Try a different sound effect too? Try and narrow the problem down to "is it SimpleAudioEngine".
Also try and playBackgroundMusic for a sound (This was the solution in this case)
And try an mp3
There isn't a problem. It's a status message written to the log when you initialise the current OpenAL context using alcMakeContextCurrent. As far as I'm aware, you can't get rid of the message.
There is no problems with your code for the Playing of the Sound. Please check the Sound file has been added to the project also do check the format of the sound. It should play the Sound whenever you call the Play Effect. Please also try to PRELOAD the sound effect in the init method.
that message means the song was read correctly and should be playing. possible reasons you don't hear it?
volume turned down.
device malfunctioning
audio has silence in it
audio file is large and will take a long time to load.
sound has been redirect to come out of the headphones or the ear piece (even if not attached)
But the sound is loading and most likely playing.
If you are getting a crash while running in device from xcode pls try this(worked for me). Dissconnect the device from Xcode and run the app in the device. I dont know why it worked like that. But when I did this there was no crash.

Can I preload mp4 before play with MPMoviePlayerController?

Can I preload mp4 before play with MPMoviePlayerController ?
I have a local mp4 video. I fount it stop at the first frame little time. I'm sure it don't matter to network. Can it preload to memry before play with MPMoviePlayerController?
I'm assuming by "local" you mean "on a machine in the same room as you". And if that is true, then the short answer is: "Yes you can."
The way I would do this is to save (or cache) a copy of the movie file to your Documents or Cache directory on your iPhone and then when you create your MPMoviePlayerController object, you can refer to the file saved on the device (via a NSURL pointing to the file saved).
Take a look at Apple's documentation for MPMoviePlayerController's initWithContentURL: method (which I have linked for you).
If by "local" you mean the file is already on the device and it is inexplicably freezing, then there is likely another problem with the mp4 you are trying to play.

Cannot play a recorded sound on device

I'm using the exact code from the iPhone Application Programming Guide Multimedia Support to use AVAudioRecorder to record a file to the disk and then AVAudioPlayer to load and play that file.
This is working fine in the simulator but is not working on the device. The file gets loaded (we can see the NSTimeInterval) but does not play (play returns false).
After it didn't work with the sample code from the website, we tried changing to a bunch of different codecs with no success. And of course, the sound is on.
Thanks a bunch.
SOLVED!
The problem was that after you set the AVAudioSession category to Record for recording, you have to set it to Play for playing. Sounds obvious now but you know how it is. When you use the Simulator it uses a different underlying implementation so this problem does not show up.