Skip to content

Instantly share code, notes, and snippets.

@vleborgne
Created July 9, 2014 12:02
Show Gist options
  • Save vleborgne/70a492c9ea0345758211 to your computer and use it in GitHub Desktop.
Save vleborgne/70a492c9ea0345758211 to your computer and use it in GitHub Desktop.
Radio player
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#import "CDVStream.h"
#import <Cordova/NSArray+Comparisons.h>
#import <Cordova/CDVJSON.h>
#define HTTP_SCHEME_PREFIX @"http://"
#define HTTPS_SCHEME_PREFIX @"https://"
@implementation CDVStream
@synthesize objAVPlayer;
- (void)create:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
NSString* mediaId = [command.arguments objectAtIndex:0];
NSString* resourcePath = [command.arguments objectAtIndex:1];
NSLog(@"Init '%@'", resourcePath);
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}];
}
- (void)startPlayingAudio:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
NSString* callbackId = command.callbackId;
#pragma unused(callbackId)
NSString* mediaId = [command.arguments objectAtIndex:0];
NSString* resourcePath = [command.arguments objectAtIndex:1];
NSURL* resourceURL = [NSURL URLWithString:resourcePath];
NSLog(@"Playing '%@'", resourcePath);
[self setObjAVPlayer:[[AVPlayer alloc] initWithURL:resourceURL]];
[[self objAVPlayer] addObserver:self forKeyPath:@"status" options:0 context:nil];
[[self objAVPlayer] play];
return;
}];
}
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (object == [self objAVPlayer] && [keyPath isEqualToString:@"status"]) {
if ([self objAVPlayer].status == AVPlayerStatusReadyToPlay) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[self objAVPlayer] play];
}
if ([self objAVPlayer].status == AVPlayerStatusFailed) {
NSLog(@"Something went wrong: %@", [self objAVPlayer].error);
}
}
}
- (void)stopPlayingAudio:(CDVInvokedUrlCommand*)command
{
NSString* mediaId = [command.arguments objectAtIndex:0];
NSString* jsString = nil;
/*if ((audioFile != nil) && (audioFile.player != nil)) {
NSLog(@"Stopped playing audio sample '%@'", audioFile.resourcePath);
[audioFile.player stop];
audioFile.player.currentTime = 0;
jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_STOPPED];
} // ignore if no media playing
if (jsString) {
[self.commandDelegate evalJs:jsString];
}*/
}
- (void)dealloc
{
}
- (void)onReset
{
}
@end
@implementation CDVAudioPlayer
@synthesize mediaId;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment