Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The Audio player
- var my_media = null ;
- var mediaTimer = null;
- // duration of media (song)
- var dur = -1;
- // need to know when paused or not
- var is_paused = false;
- var src = '';
- //function init() {
- // document.addEventListener("deviceready", onDR, false);
- //}
- //Set audio position on page
- function setAudioPosition(position) {
- $("#audio_position").html(position + " sec");
- }
- //onSuccess Callback
- function onSuccess() {
- setAudioPosition(dur);
- clearInterval(mediaTimer);
- mediaTimer = null;
- my_media = null;
- is_paused = false;
- dur = -1;
- }
- //onError Callback
- function onError(error) {
- alert('code: ' + error.code + '\n' +
- 'message: ' + error.message + '\n');
- clearInterval(mediaTimer);
- mediaTimer = null;
- my_media = null;
- is_paused = false;
- setAudioPosition("0");
- }
- function playAudio(src) {
- console.log("Play Audio " + src);
- if (my_media) {
- my_media.stop();
- }
- // Create Media object from src, and set the onSuccess and onError methods
- my_media = new Media(src, onSuccess, onError);
- // Play audio
- my_media.play();
- // Update my_media position every second
- if (mediaTimer == null) {
- mediaTimer = setInterval(function() {
- // get my_media position
- my_media.getCurrentPosition(
- // success callback
- function(position) {
- if (position > -1) {
- setAudioPosition((position) + " sec");
- }
- },
- // error callback
- function(e) {
- console.log("Error getting pos=" + e);
- setAudioPosition("Error: " + e);
- }
- );
- }, 1000);
- }
- }
- // get duration
- if(my_media) var dur = my_media.getDuration();
- if (dur > 0){
- console.log("Media Duration : " +dur );
- docuement.getElementById('media_dur').innerHTML=(dur) + "sec";
- }
- //Pause audio
- function pauseAudio() {
- console.log('pressed pause audio');
- if (is_paused) {
- if (my_media) {
- is_paused = false;
- my_media.play();
- $("#pauseaudio").text("Pause");
- }
- } else {
- if (my_media) {
- is_paused = true;
- my_media.pause();
- $("#pauseaudio").text("Play");
- }
- }
- }
- //Stop audio
- function stopAudio() {
- if (my_media) {
- // A successful .stop() will call .release()
- my_media.stop();
- my_media = null;
- }
- if (mediaTimer) {
- clearInterval(mediaTimer);
- mediaTimer = null;
- }
- is_paused = false;
- dur = 0;
- }
- /* Concert */
- function searchConcerts(nameArtist, nameCity) {
- // var url = http://api.setlist.fm/rest/0.1/search/setlist.json?artistName="nameArtist"&cityName="nameCity";
- var serviceURL = "http://api.setlist.fm/rest/0.1/search/setlists.json?";
- var parameter = new String();
- parameter = parameter.concat("artistName=", nameArtist, "&cityName=", nameCity);
- var url = serviceURL + "" + parameter;
- console.log(url);
- $.ajax({
- url : serviceURL + "" + parameter,
- type : "GET",
- dataType : "json",
- success : parseConcert,
- error : showError
- });
- }
- function parseConcert(dataJson){
- console.log("response " + dataJson);
- var result = document.createElement("p");
- var textNode = document.createTextNode( " Event date : " + dataJson.setlists.setlist[0]['@eventDate'] + " Event tour : " + dataJson.setlists.setlist[0]['@tour']);
- result.appendChild(textNode);
- document.getElementById("resultConcert").appendChild(result);
- }
- /* Tour */
- function searchTour(nameArtist,nameTour){
- var serviceURL = "http://api.setlist.fm/rest/0.1/search/setlists.json?";
- var parameter = new String();
- // SEE searchConcert
- $.ajax({
- url : serviceURL + "" + parameter,
- type : "GET",
- // dataType : "json",
- success : parseTour,
- error : showError
- });
- }
- function parseTour(dataJson){
- console.log("response " + dataJson);
- }
- /**
- * Callback ajax error
- * @param request
- * @param error
- * @param obj
- */
- function showError(request, error, obj) {
- console.log("Error received" + error + " " + request);
- alert("Error contacting remote Server.");
- }
- $( function() {
- /*$('#playaudio').click(function() {
- // Note: two ways to access media file: web and local file
- //src = 'http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3';
- // local (on device): copy file to project's /assets folder:
- //var src = '/android_asset/spittinggames.m4a';
- playAudio(src);
- });*/
- // Start with Manual selected and Flip Mode hidden
- $('#nav-manual').focus();
- $('#content-list1').hide();
- $('#content-list2').hide();
- $('#content-list3').hide();
- $('#content-list4').hide();
- $('#nav-manual').live('tap', function() {
- console.log("nav-manual taped");
- $('#content-list1').hide();
- $('#content-list2').hide();
- $('#content-list3').hide();
- $('#content-list4').hide();
- $('#content-manual').show();
- stopAudio();
- });
- $('#nav-list1').live('tap', function() {
- $('#content-manual').hide();
- $('#content-list2').hide();
- $('#content-list3').hide();
- $('#content-list4').hide();
- $('#content-list1').show();
- });
- $('#nav-list2').live('tap', function() {
- $('#content-manual').hide();
- $('#content-list1').hide();
- $('#content-list3').hide();
- $('#content-list4').hide();
- $('#content-list2').show();
- });
- $('#nav-list3').live('tap', function() {
- $('#content-manual').hide();
- $('#content-list2').hide();
- $('#content-list1').hide();
- $('#content-list4').hide();
- $('#content-list3').show();
- });
- $('#nav-list4').live('tap', function() {
- $('#content-manual').hide();
- $('#content-list2').hide();
- $('#content-list3').hide();
- $('#content-list1').hide();
- $('#content-list4').show();
- });
- $("#pauseaudio").live('click', function() {
- pauseAudio();
- });
- $("#stopaudio").live('tap', function() {
- stopAudio();
- });
- $('#music1').click(function() {
- src = 'http://www.universal-soundbank.com/mp3/sounds/10157.mp3';
- playAudio(src);
- });
- $('#music2').click(function() {
- src = 'http://www.universal-soundbank.com/mp3/sounds/10183.mp3';
- playAudio(src);
- });
- $('#music3').click(function() {
- src = 'http://folk.ntnu.no/henninsc/thug.mp3';
- playAudio(src);
- });
- $('#music4').click(function() {
- src = 'http://stream1.addictradio.net/addictrock.mp3';
- playAudio(src);
- });
- $("#searchButtonConcert").click(function() {
- // get the vars : artistName and cityName
- artistName=$('#artistNameConcert').val();
- nameCity=$('#cityName').val();
- // call the function to make an ajax request
- searchConcerts(artistName, nameCity);
- });
- $("#searchButtonTour").click(function() {
- // searchTour(nameArtist, nameTour);
- });
- // implement a function to read a file from the sdcard
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement