I'm using the Soundcloud Javascript SDK (http://developers.soundcloud.com/docs/api/sdks#javascript) on a project and have happily got my player loading a sound and playing it like so:
SC.get("/resolve/",{
url: href
},function(response){
SC.stream("/tracks/"+response.id,{},function(sound){
sound.play();
});
})
I can trigger the soundmanager object to play in the callback using sound.play() but I can't work out how to access the events of the object mantioned in the docs (http://www.schillmania.com/projects/soundmanager2/doc/) like whileplaying()
How do I add these in? I've tried this kind of thing:
sound.whileplaying(function(){
alert("hooray!")
})
But that doesn't work.
Many thanks
Julian
This should work:
SC.stream("/tracks/" + response.id, {
whileplaying: function () {
console.log("track is playing");
}
}, function (sound) {
sound.play();
});
The correct way is:
SC.stream('/tracks/' + response.id).then(function (player) {
player.on('finish', function () {
console.log('finish');
});
player.play();
});
You found this answer and consult all events here:
https://developers.soundcloud.com/docs/api/sdks#player
Related
I have used Expo AV and developed a screen in my app to play audio files fetched from my server. It works fine on Android, but doesn't play anything on iPhone.
When I play a button to play the audio which loads and plays the file
soundObject.loadAsync({ uri: this.state.file });
soundObject.playAsync();
It returns an error:
This media format is not supported. - The AVPlayerItem instance has failed with the error code -11828 and domain "AVFoundationErrorDomain".
Here is my code that loads and plays the audio :
async loadAudio() {
soundObject = new Audio.Sound();
try {
await soundObject.loadAsync({ uri: this.state.file });
console.log("File loaded: " + this.state.file);
} catch (error) {
console.log(error);
}
}
async playAudio() {
if (!this.state.isPlayingAudio) {
try {
await soundObject.playAsync();
} catch (error) {
console.log(error);
}
else {
soundObject.pauseAsync();
}
}
I have tried changing the audio format to m4a, wav, caf while recording and fetching the file but that did not help
I'm running the app on iPhone 7 plus, iOS 14.2
Any suggestions/ fixes, please? Thanks in advance
You're calling loadAsync improperly.
The call should look like this:
await Audio.Sound.createAsync(
{ uri: this.state.file },
{ shouldPlay: true }
);
I'm passing the uri object and a second argument {shouldPlay: true} to the loadAsync method.
This plays my mp3 files from amazon server s3
await Audio.Sound.loadAsync( { uri: this.state.file }, { shouldPlay: true } )
Please add this method before playing soundd view "expo-av"
const enableAudio = async () => {
await Audio.setAudioModeAsync({
playsInSilentModeIOS: true,
staysActiveInBackground: false,
interruptionModeAndroid: INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
shouldDuckAndroid: false,
})
I was on Expo 44, downgrading to Expo 43 did the trick. Run expo upgrade 43.
I'm trying to do a Youtube API and I feel like I got everything working except this gapi and res thing? It says gapi is not defined. How can I make this work?
function tplawesome(e,t){res=e;for(var n=0;n<t.length;n++){res=res.replace(/\{\{(.*?)\}\}/g,function(e,r){return t[n][r]})}return res}
$(function() {
$("form").on("submit", function(e) {
e.preventDefault();
// prepare the request
var request = gapi.client.youtube.search.list({
part: "snippet",
type: "video",
q: encodeURIComponent($("#search").val()).replace(/%20/g, "+"),
maxResults: 3,
order: "viewCount",
publishedAfter: "2015-01-01T00:00:00Z"
});
// execute the request
request.execute(function(response) {
var results = response.result;
$("#results").html("");
$.each(results.items, function(index, item) {
$.get("tpl/item.html", function(data) {
$("#results").append(tplawesome(data, [{"title":item.snippet.title, "videoid":item.id.videoId}]));
});
});
resetVideoHeight();
});
});
$(window).on("resize", resetVideoHeight);
});
function resetVideoHeight() {
$(".video").css("height", $("#results").width() * 9/16);
}
function init() {
gapi.client.setApiKey("AIzaSyD646m4ZfK5yKBZj9p95LohN-PTUnRHBRY");
gapi.client.load("youtube", "v3", function() {
});
}
gapi is an object created by the Google API javascript library that manages all interactions (i.e. does all the heavy lifting of the requests) for you. If the object is not defined, you may not have included the library itself in your page. Somewhere in your HTML, you'll need a script tag that loads the library located at:
https://apis.google.com/js/client.js
Note that, in loading the library with a script tag, you should also pass it a callback ... this is a function that will be automatically called as soon as the library is done loading. So in your case, your init() method is that callback, and so your script tag would look like this:
<script src="https://apis.google.com/js/client.js?onload=init"></script>
The browser will get the library, load it, then run init() when the library is done loading, and all will be ready for your form to execute when triggered.
I'm trying to use PrivatePub within my Angular app. I've a chat and messages are managed by AngularJS, my API behind is running with Rails, in my controller I use the helper to Publish to the channel, my problem is on the client side with the Subscribe. Here is what I try to do:
chat.controller("MessageController", ['$scope','Message','Project', function($scope,Message,Project) {
//Fetch messages
Message.query(function(data) {
$scope.messages = data;
});
PrivatePub.subscribe(Project.channel, function(data, channel) {
$scope.messages.push(data.message);
});
}]);
I tried to use $apply and $watch around my PrivatePub subscribe, no way to update my scope. My PrivatePub function should be outside Angular but the data it receives should be added to the $scope.I don't what other solution I could try.
Ok I found the problem, I was not using $apply correctly, I was basically doing:
$timeout(function () {
$scope.$apply(function($scope) {
PrivatePub.subscribe("/mychannel", function(data, channel) {
$scope.addMessage(data.chat_message);
});
});
}, 0);
Instead of:
$timeout(function () {
PrivatePub.subscribe("/mychannel", function(data, channel) {
$scope.$apply(function($scope) {
$scope.addMessage(data.chat_message);
});
});
}, 0);
The changes I want to notify to Angular is not the function itself but what happened inside. Just a bad use.
I'm doing a simple AJAX call to append an album's tracks in an unordered list. It will append the tracks on the second click with this code:
window.app.views.AlbumView = Backbone.View.extend({...
events: {
'click .queue-add' : 'selectAlbum',
'click .show-tracks' : 'showTracks',
'click .hide-tracks' : 'hideTracks',
},
showTracks: function(){
_this = this
this.model.getTracks().forEach(function(track){
_this.$el.find('.tracks').append("<li>"+track.attributes.title+"</li>");
});
},
Clearly the tracks hadn't been fetched in time for the first click so I added a callback function to the showTracks method like so:
showTracks: function(){
_this = this
this.model.getTracks({
success: function(tracks){
console.log(tracks);
tracks.forEach(function(track){
_this.$el.find('.tracks').append("<li>"+track.attributes.title+"</li>");
});
}
});
},
Yet it won't enter the block and the console.log(tracks); puts nothing to the console.
Any tips would be really awesome here, thanks!!
app.models.Album = Backbone.Model.extend({
....
getTracks: function() {
this.tracks.fetch();
return this.tracks
},
....
});
I couldn't find where did you invoke that callback. you may need modify "getTracks" method like this:
getTracks: function(callback) {
this.tracks.fetch();
callback(this.tracks); //you need to invoke the callback before return
return this.tracks;
}
This is called "callback pattern", google it will find more.
and the backbone model's fetch method accept option argument, It is a object with two keys -- success and error -- both are function. If you provide this argument, backbone will call them automatically.
hope this help.
I want to import and export CSV's. I have figured out how to get the iPad to recognize my app as one that opens CSV files.
From there though I am lost. I have found explanations on how the iPad sends in my file via application:didFinishLaunchingWithOptions or handleOpenURL ...
I've figured out that adding a function called handleOpenURL(url) in my js file passes me the url for the file... so now I have this.
That is great because I now know that someone has opened my app this way. Cool... BUT how do I grab the contents of that URL?
GOT IT! Woot, this is what i did...
function handleOpenURL(url)
{
window.resolveLocalFileSystemURI(url, onResolveSuccess, fail)
}
function onResolveSuccess(fileEntry)
{
fileEntry.file(win, fail);
}
function win(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
alert("succes");
alert(evt.target.result);
}
reader.readAsText(file);
}
function fail() {
alert('fail');
}