Long audio file not playing - ios

Im trying to play a really long audio file in iOS (like 10 minutes long) and it doesn't want to play. I've gotten other files to work with this code just fine but this one just doesn't want to play.
Heres my code:
void SoundFinished (SystemSoundID snd, void* context) {
NSLog(#"finished!");
AudioServicesRemoveSystemSoundCompletion(snd);
AudioServicesDisposeSystemSoundID(snd);
}
- (IBAction)lolol{
NSURL* sndurl = [[NSBundle mainBundle] URLForResource:#"full video" withExtension:#"mp3"];
SystemSoundID snd;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)sndurl, &snd);
AudioServicesAddSystemSoundCompletion(snd, nil, nil, SoundFinished, nil);
AudioServicesPlaySystemSound(snd);
}

The file is too long for System Sound Services to play it as mentioned in the docs -
You can use System Sound Services to play short (30 seconds or shorter) sounds.
I suggest using an alternate way to play the file. Have a look at using AVAudioPlayer class to play the audio file.

Related

use systemSound from SystemSoundID for UNNotificationSound

I am trying to add a sound to my UNNotification. Instead of my own custom sound or the default sound, I would like to use one of the iOS SystemSounds in AVFoundation. I can get the sound by:
import AVFoundation
...
let systemSoundID: SystemSoundID = 1016
AudioServicesPlaySystemSound(systemSoundID)
How can I add this sound to my notification?

AVAudioPlayer will not play audio for one mp3 but will for others?

All of my other audio files play perfectly through the audio player. Most of the files are less than 5 seconds long, and the longest file that still plays is 23 seconds long. For some reason my 1:15 long file "sortSong" is completely silent when it is called.
Here is the code for my audio player:
import Foundation
import AVFoundation
class AudioPlayer {
static let sharedInstance = AudioPlayer()
enum Sound: String {
case sortSongPlay = "sortSong"
case centerButtonRelease = "centerButtonRelease"
case buttonTap = "tapSound"
static var all: [Sound] {
return [.centerButtonRelease, .buttonTap, sortSongPlay]
}
var url: URL {
return URL.init(fileURLWithPath: Bundle.main.path(forResource: self.rawValue, ofType: "mp3")!)
}
}
private var soundMap = [String: SystemSoundID]()
init() {
for sound in Sound.all {
let soundUrl = sound.url
var soundId: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundUrl as CFURL, &soundId);
soundMap[sound.rawValue] = soundId
}
}
func play(sound: Sound) {
AudioServicesPlaySystemSound(soundMap[sound.rawValue]!)
}
}
The sound is played when this function is called in my view controller.
func successfulSort() {
AudioPlayer.sharedInstance.play(sound: .sortSongPlay)
rotateSortImageOne()
rotateSortImageTwo()
}
This is the action that calls the successfulSort func
if inputText == "hello" && seedArray == [1, 4, 1] {
successfulSort()
}
If I simply change the case sortSongPlay to = "shortSortSong" (the 23 second version) it plays just fine.
All of my sound files have their target memberships checked for this project file, and all of the files have the correct path. The audio will play in the interface builder if I press the play button. There are no compiler errors or warnings, the app never crashes, the audio for sortSong simply isn't playing when it is called in the app.
This is a link containing examples I have tried on my project. The first two sounds play silently in the app while the shorter sounds all play perfectly. https://soundcloud.com/spiffystache
What is causing this to be silent?
You should put a correct title on your question.
Your code does not use AVAudioPlayer, but uses System Sound Services.
The documentation clearly states its limitation:
You can use System Sound Services to play short (30 seconds or
shorter) sounds.
I have never checked if its limitation is exactly 30 seconds, but it matches the description in your question.
Consider using AVAudioPlayer (as in the title) to play longer sound.

Swift/iOS: AVAudioPlayer does not play sound in NSData format

I am trying to create an AVAudioPlayer that plays NSData downloaded from Parse.
I am pretty certain the sound (.wav format) has been uploaded to Parse. I am also certain that the sound can be downloaded from Parse in the NSData format. So I am creating an AVAudioPlayer object using the downloaded data from Parse:
if audioData != nil {
print("successful downloading audio!") //this prints out
let audioPlayer = try! AVAudioPlayer(data: audioData!, fileTypeHint: AVFileTypeWAVE)
audioPlayer.prepareToPlay()
audioPlayer.volume = 0.5
audioPlayer.play()
}
As you can see above, the audioPlayer is created, but it does not play the sound. Where might be wrong?
Is this code within a function? If so, your making the audioPlayer variable locally. This means that the audioPlayer is created, starts playing and then is deallocated (at the end of the function call), resulting in no audio. Your audioPlayer object needs to be a class property or within global space (such as a singleton for example), so the object life persists after the function ends.

Playing Avi video in swift based application

I'm trying to play a video of type avi in an application created in xcode written in swift.
The thing is all of the videos I have are in avi and I cannot afford time spent converting the videos right now. Is there a way to play the video in the provided Media player library?
the code I wrote plays the audio of the video only:
func playVideo() {
let path = NSBundle.mainBundle().pathForResource(symbol, ofType:"avi")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
player.prepareToPlay()
player.scalingMode = .AspectFill
self.view.addSubview(player.view)
}
or maybe there's a way to convert a video when the function is called?
I,
following the documentation of the MPMediaPlayer :
For movie files, this typically means files with the extensions .mov, .mp4, .mpv, and .3gp
So, unfortunately, you can't use the MPMediaPlayer to read your avi video.
Thanks to VLC for iOs source code, you can re-use it to implement a custom player to read these files.

How to play MIDI with bassmidi? (ios)

I'm trying to play midi but it's not working, I can play an mp3 but when I change the code to midi and build - there is no sound. (with "bassmidi" plugin).
my code:
NSString *fileName = #"1";
NSString *fileType = #"mid"; // mid
BASS_Init(-1, 44100, 0, 0, 0); // initialize output device
NSString *respath=[[NSBundle mainBundle]pathForResource:fileName ofType:fileType]; // get path of audio file
HSTREAM stream=BASS_MIDI_StreamCreateFile(0, [respath UTF8String], 0, 0, BASS_SAMPLE_LOOP, 1);
BASS_ChannelPlay(stream, FALSE); // play the stream
For those who would be interested in using the BASSMIDI player on iOS, we implemented an AUv3 Audio Unit wrapped around the bassmidi library.
The main advantage is that this audio unit can be inserted into a graph of audio nodes handled by the iOS Audio Engine (just like you would do with the AVAudioUnitSampler).
The code is available on a public repository:
https://github.com/newzik/BassMidiAudioUnit
Feel free to use it!

Resources