Discussion Forums  >  Audio, Video, Playing, Recording

Replies: 17    Views: 301

amysus
Lost but trying
Profile
Posts: 90
Reg: Oct 28, 2011
traveling the w...
3,650
10/22/12 03:02 PM (11 years ago)

How to Create a Simple Audio Player

Hi - I have been trying to create a simple audio player for my app, with no luck. I have been working with JPlayer (and have it running fine locally) but cannot get it to run in XCode. I now searched on the Apple Developer site and found a bit of info about playing audio with the AVAudioPlayer Class. This is starting to get beyond me - as I am really not sure how to implement, but, seems that someone else may have figured this out and created a plugin, tutorial, or something. Here is info from Apple (and this is all I need my player to do): From Apple: <a href="https://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW6" target="_blank" rel="nofollow">https://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW6</a> Playing Sounds Easily with the AVAudioPlayer Class The AVAudioPlayer class provides a simple Objective-C interface for playing sounds. If your application does not require stereo positioning or precise synchronization, and if you are not playing audio captured from a network stream, Apple recommends that you use this class for playback. Using an audio player you can: Play sounds of any duration Play sounds from files or memory buffers Loop sounds Play multiple sounds simultaneously (although not with precise synchronization) Control relative playback level for each sound you are playing Seek to a particular point in a sound file, which supports application features such as fast forward and rewind Obtain audio power data that you can use for audio level metering Any advice is appreciated. Amy
 
theMonster
Code is Art
Profile
Posts: 435
Reg: Oct 18, 2011
US
8,050
like
10/22/12 03:39 PM (11 years ago)
I might make a audio player plugin, I heard someone else was working on one and stopped but that was about a month ago. I might just take up the project.
 
amysus
Lost but trying
Profile
Posts: 90
Reg: Oct 28, 2011
traveling the w...
3,650
like
10/22/12 03:41 PM (11 years ago)
That would be awesome.
 
theMonster
Code is Art
Profile
Posts: 435
Reg: Oct 18, 2011
US
8,050
like
10/22/12 04:22 PM (11 years ago)
Definatly, I'll see what I can do.
 
ictguy
Aspiring developer
Profile
Posts: 564
Reg: Jun 17, 2011
Mildura, Austra...
15,840
like
10/22/12 05:16 PM (11 years ago)
Hi @amysus, I created a podcasting app at one stage and used mediaelement.js (which is similar to jplayer - HTML 5 javascript audio player). I just created a custom URL screen and pointed it to the web page with the audio and JavaScript player. When trying to get jplayer on the app, have you been excluding the .js files from the compile sources in xcode? Kind regards Darrel
 
amysus
Lost but trying
Profile
Posts: 90
Reg: Oct 28, 2011
traveling the w...
3,650
like
10/23/12 05:36 AM (11 years ago)
Hi Darrel - Thanks for the info. I have not been excluding the .js files - in fact, after working with JPlayer for a while with no luck, I watched David's first video in the Xcode series (here on BT) which explained about folders and organization. I then redid my local file structure to align with BTs structure (e.g., BT_Sound). I tried it a few ways (.js files in BT_Docs, images in BT_Docs, images in BT_Images, etc.) always reworking my htm file to point to the correct directory. I always got it to work locally, never in XCode. Frustrating! I'll look into mediaelement - thanks for the tip. Amy
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
10/23/12 06:25 AM (11 years ago)
I am looking for an audio player too. If you need very simple, it can be done with html5 - I have it working for IOS in a BT app but it doesn't seem to work as well for Android.
 
amysus
Lost but trying
Profile
Posts: 90
Reg: Oct 28, 2011
traveling the w...
3,650
like
10/23/12 06:50 AM (11 years ago)
Hi AlanMac - I would love to hear how you did it in html5, or see the code. A simple player with looping, play/pause is really all I need, at least to get this thing going. I am working in IOS too. Thank you, Amy
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
10/23/12 07:04 AM (11 years ago)
Hi Amy It is literally only a line of code. If you drop the following into your html in the body section, it should work in ios. <audio autoplay="autoplay" controls="controls" loop="loop" preload="auto" tabindex="0"> <source src="your_audio_file.mp3" type="audio/mpeg"> Your browser does not support the audio element. </source></audio> Let me know how you get on, Alan
 
amysus
Lost but trying
Profile
Posts: 90
Reg: Oct 28, 2011
traveling the w...
3,650
like
10/23/12 07:17 AM (11 years ago)
OK - thanks Alan - I'll give it a try.
 
amysus
Lost but trying
Profile
Posts: 90
Reg: Oct 28, 2011
traveling the w...
3,650
like
10/23/12 07:44 AM (11 years ago)
Hi Alan - I now recall trying this a while back ... I have added your code to an HTML5 doc on BT, in the body section. I tested it locally and it works great. But again, when I test in XCode it does not find the audio file. I have put the audio file in BT_Sound and referenced it in the html (trying it many ways, none work): <source src="../BT_Sound/song.mp3" type="audio/mpeg"> <source src="/BT_Sound/song.mp3" type="audio/mpeg"> and <source src="BT_Sound/song.mp3" type="audio/mpeg"> I also copied the audio file into my BT_Docs folder and referenced it as: <source src="song.mp3" type="audio/mpeg"> Not sure what I'm doing wrong. Thanks again, Amy
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
10/23/12 08:10 AM (11 years ago)
Aah, I know the problem. Reference the file simply as <source src="song.mp3" type="audio/mpeg"> Put song.mp3 is in your BT_Sound folder. Hopefully, BT Magic does the rest!
 
amysus
Lost but trying
Profile
Posts: 90
Reg: Oct 28, 2011
traveling the w...
3,650
like
10/23/12 08:15 AM (11 years ago)
Alan - One more bit of info. I finally did get it to play by using the custom HTML plugin (the version that links to a file that I upload, not the HTML5 plugin). I also put the audio in BT_Sound and did not reference the folder/path - simply referred to as <source src="song.mp3" type="audio/mpeg"> . So now it does play, but the player display is tiny (not sure how to resize - I've tried that before too with no luck) and... it does not auto play. It does loop (yeah!!) but the loop glitches (slight pause and then plays the first second of the loop twice). Thoughts? Thank you! Amy
 
amysus
Lost but trying
Profile
Posts: 90
Reg: Oct 28, 2011
traveling the w...
3,650
like
10/23/12 08:17 AM (11 years ago)
And just seeing your note, above - re folder structure, thank you. Still having trouble with the loop and auto play and size.
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
10/23/12 08:26 AM (11 years ago)
Glad this got you going, but as you say, the player is awfully narrow. I never went further than the basic presentation because I had compatibility issues with my Android phone; to restyle probably needs css and javascript. autoplay seems to have a few variants, maybe one of them works? <audio autoplay> <audio autoplay="autoplay"> <audio autoplay=""> I referred here to find out about all the options :- http://www.w3schools.com/html5/att_audio_autoplay.asp and here http://www.htmlgoodies.com/primers/html/article.php/3920991/HTML5-Primer-How-To-Use-the-Audio-Tag.htm If you manage improve the look, please let me know how!
 
amysus
Lost but trying
Profile
Posts: 90
Reg: Oct 28, 2011
traveling the w...
3,650
like
10/23/12 08:30 AM (11 years ago)
Thanks again Alan - I'll take a look at w3 and the other site you mention. And yes, if I ever get this going I'll be sure to post! Best, Amy
 
ictguy
Aspiring developer
Profile
Posts: 564
Reg: Jun 17, 2011
Mildura, Austra...
15,840
like
10/23/12 06:28 PM (11 years ago)
Keep at it, sounds like you're getting close :) As for the size of the player, have you included the meta tag below in the head section of your html files? <meta content="width=device-width; initial-scale=1.0; maximum-scale=2.0; user-scalable=1;" name="viewport" />
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
10/24/12 01:16 AM (11 years ago)
Amy, here is an interesting tutorial on how to replace the standard media controls with JavaScript :- http://www.adobe.com/devnet/html5/articles/html5-multimedia-pt3.html Alan
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.