Getting current MP3 filename with AVAudioPlayer - iphone

I've got AVAudioPlayer playing some MP3 in background. AVAudioPlayer is singleton instance shared across app. Is it possible to detect current filename of MP3 being played at a time? I can't see any method in delegate, there's duration, currentTime but nor filename or something like this.
Other thing I consider if this method fails is reading MP3 tags and get track name from it - is it possible with AVAudioPlayer?
Thanks in advance!

You can get filename of current playing media in AVAudioPlayer like this:
NSString *mediaFileName = [[youAVAudioPlayer.url absoluteString] lastPathComponent];
Note condition is that: Returns nil if the audio player was not initialized with a URL.

If you initialize the AVAudioPlayer with a url, you can read that property. AVAudioPlayer does not read the tags in the MP3 file, so it will not help with that.
You may find AVAsset interesting as a way to get metadata from an MP3. Quick Tutorial.

Related

Sending a Value to an AVAudioPlayer Object to Play a Different Sound

I have an iphone app where I am playing a sound with one button using an AVAudioPlayer object. However, I would like to use a variable for the sound name and send a value to that variable. I have two other buttons that play different sounds and would like to send the sound name values to the variable that is in the AVAudioPlayer. How do I do that?
Thanks!
you can keep your audio files (in resource) with numeric names such as 1.mp3, 20.mp3, 41.wav etc and then you can use [NSString stringWithFormat:#"%d.mp3",soundId]; to generate sound file name to use generating absolute resource path and pass it to AVAudioPLayer
Best of Luck!!
This cannot be done. Create a new AVAudioPlayer for each new sound. If you wish to interrupt the current sound, send stop to the current instance.

MPMoviePlayerController - how to tell what bitrate is playing?

The HTTP Live Streaming format supports variable bitrates, which are described in the m3u8 file.
Is it possible to get the bitrate of the currently playing stream?
No, you can't get that information from MPMoviePlayerController
To get the information you want, you could use AVPlayer and AVPlayerItems, which will then create AVAsset items that you can interrogate to discover their properties.
Once you have a AVPlayer, you can find the current AVPlayerItem using currentItem. From that, you can get the asset property.
A AVAsset has AVAssetTracks and this has the formatDescriptions property. Somewhere in there you should find the bitrate.

Streaming with an AVAudioplayer

In order to load a sound file, I have the following code in my application :
- (id) init:(NSString*)a_filename ext:(NSString*)a_ext
{
...
NSString *t_soundFilePath = [CFileLoader getPathForResource:filename WithExtension:ext];
NSURL *t_fileURL = [[[NSURL alloc] initFileURLWithPath: t_soundFilePath] autorelease];
player = [[AVAudioPlayer alloc] initWithContentsOfURL: t_fileURL error: nil];
[player prepareToPlay];
...
}
All the sounds that I load are in my bundle, so I would like to know if the method "initwithcontentsofurl" stream the sound file or if all the file is cached.
I have a lot of sprites in my app so I want to minimize memory space used for sounds.
thx for your help
I used AVPlayer (not AVAudioPlayer) to stream background audio.
Sounds that are played using the AVAudioPlayer are streamed real time from storage. The drawback being that you can occasionally notice a small lag when it is due to start up. If you use OpenAL the sounds are loaded entirely into memory.
There's a good discussion of this sort of thing in the ObjectAL documentation - a library that is meant to simplify the playing of sounds and effects on the iPhone.
If you want to stream audio in your app you can use the following player instead of using AVAudioPlayer
https://github.com/mattgallagher/AudioStreamer
For this player you don't need to put sound files in your bundle, you can put them at server and use url to stream audio.
Hope, this wil help you.
" The AVAudioPlayer class does not provide support for streaming audio
based on HTTP URL's. The URL used with initWithContentsOfURL: must be
a File URL (file://). That is, a local path".
https://developer.apple.com/library/ios/qa/qa1634/_index.html
But you can work around - for example write asynchronously to local file, and init AVAudioPlayer with this file URL, it will work.

How to use AVAudioPlayer to play a playlist in iPhone/iPad

Please I know how to play a sound in AVAudioPlayer but I would like to know how to give it a list of on-disk files to play one after the other (play list)
Anyone has any idea?
You have to manage this by yourself, using the 'audioPlayerDidFinishPlaying' method to know when the current song is over, and when to start a new one.
The Apple "AddMusic" sample program does exactly this, setting up a playlist of audio files using MPMusicPlayerController and AVAudioPlayer to do most of the work.

Playing audio files on the iPhone

I've made a simple app, where I have a list of songs. The user taps a list entry and the song begins playing.
I've lifted the SoundEffect class from Apple's sample projects (e.g. Metronome, BubbleLevel). It seems to work fine with the following code:
// declare in the .h file
SoundEffect *audio;
// setup - when controller loads
audio = [SoundEffect alloc];
// play when user taps entry
NSBundle *mainBundle = [NSBundle mainBundle];
[audio initWithContentsOfFile:[mainBundle pathForResource:#"entry1" ofType:#"mp3"]];
[audio play];
However, if the 'audio' object is already playing, I'd like to stop it before it starts playing the sound again. SoundEffect class does not have a stop method or I am simply missing something.
How do i stop the audio before playing it again?
Why don’t you simply use AVAudioPlayer?
The SoundEffect class is a wrapper around the C-based System Sounds API (see the .m file from the Bubble Level project), which is a simple "fire and forget" style API that doesn't provide a "stop" function. More info in the System Sounds Services Reference.
I also agree with (and have voted up) zoul's suggestion to use AVAudioPlayer. System Sounds are wholly inappropriate for long, encoded audio files like songs in MP3 files.