Audio plays in background but no sound Cordova 3+ IOS

China☆狼群 提交于 2019-12-12 02:49:44

问题


I'm playing a mp3 file. I used cordova 3+ I'm using

snd = new Media(source,onSuccess,onplayError,mediaStatus);
snd.setVolume(current_volume);                      
snd.play();

Everything goes well. Then I set these in info.plist

Application does not run in background - NO
Required background modes:
Item0 - Audio...............etc.

Then I tried this

if ([resourceURL isFileURL]) {
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    audioFile.player = [[ AVAudioPlayer alloc ] initWithContentsOfURL:resourceURL error:&error];
} else {
    NSData* data = [NSData dataWithContentsOfURL:resourceURL];
    audioFile.player = [[ AVAudioPlayer alloc ] initWithData:data error:&error];
}

This is found from here http://www.joeldare.com/wiki/play_an_mp3_audio_stream_in_phonegap

Though I found that sound.m file wasn't there as described. I found that file in plugin folder. And those lines were near line no. 363

Then I tried to play the audio. Next I pressed the home button. No sound came from the iphone emulator. After that I preesed the app icon again, app ui came to front and sound can be heard as if it was playing all along. Means suppose I have pressed the app icon after 1 minute from the home button press, then the song is playing from 1 minute currently. So song was actually playing but sound can't be heared during that period after pressing home key. Hope I explained it properly.

Can anyone please tell me what I'm missing? I want the sound to be heared even after home key is pressed or phone gets locked or display gets off. Please help.......

Also if I use html5 audio player, what I've to do to handle this 'home key press' problem to play the audio after minimization?


回答1:


First,DO those things that I wrote in question. Then import AVFoundation into your AppDelegate.m #import <AVFoundation/AVFoundation.h>

Then add the following to application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL ok;
NSError *setCategoryError = nil;
ok = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];

Found from here https://github.com/devgeeks/ExampleHTML5AudioStreaming

And

TEST IN REAL DEVICE, NOT IN SIMULATOR. BACKGROUND AUDIO DOESN'T WORK WITH SIMULATOR.



来源:https://stackoverflow.com/questions/23732463/audio-plays-in-background-but-no-sound-cordova-3-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!