Play sound file in a web-page in the background
<audio src="/music/good_enough.mp3">
<p>If you are reading this, it is because your browser does not support the audio element. </p>
</audio>
and if you want the controls
<audio src="/music/good_enough.mp3" controls>
<p>If you are reading this, it is because your browser does not support the audio element.</p>
</audio>
and also using embed
<embed src="/music/good_enough.mp3" width="180" height="90" loop="false" autostart="false" hidden="true" />
How to play an audio file in the background after clicking a link? (no embed)
Actually the href
attribute is redirecting you to the new page, you can use e.prevenDefault()
in the link click
event handler to stop this redirection and create a dynamic audio
element with this href
as source and play it.
This is what you need:
function playItHere(e, link) { var audio = document.createElement("audio"); var src = document.createElement("source"); src.src = link.href; audio.appendChild(src); audio.play(); e.preventDefault();}
<a href="https://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg" onclick="playItHere(event, this)">Pronunciation of a word</a>
Playing MP3 in background of webpage
If you don't want the controls to be shown, delete the controls
atribute, but add the autoplay
atribute instead:
<audio loop="loop" autoplay="autoplay">
<source src="NyanCat.mp3" type="audio/mpeg" />
</audio>
Anyway, it's always a good idea to give the user the control to play/stop the music.
How to play a audio sound in a web page as a background music
Until HTML 5, flash is going to be your best solution, this is mostly because it's an intrusion to have a page commandeer control of the user's speakers, and thus why people have recommended not doing this at all or making it so that the user must explicitly click something.
Here's a page with some freebie flash player options:
http://www.premiumbeat.com/flash_resources/free_flash_music_player/
how to play sound file which is on my web server and run in background when user click Home button...?
after searching things here I'm with answer.for run application in background playing songs, step is as below
- in your application info.plist file add "Application does not run in background" to "YES"
- in your application info.plist file add "YES"Required background modes" and set "App plays audio"
in your .m file
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"select" ofType:@"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
if (error)
{
NSLog(@"Error in audioPlayer: %@", [error localizedDescription]);
} else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer play];
}
}
Related Topics
Do I Need a "/" at the End of an <Img> or <Br> Tag, etc.
Css: Workaround to Backdrop-Filter
Can Comments Appear Before the Doctype Declaration
Make Columns of Equal Width in <Table>
How to Show Multiline Text in a Table Cell
Stretching <A> Tag to Fill Entire <Li>
Difference Between Width Auto and Width 100 Percent
Change the Color of a Bullet in a HTML List
How to Upload a File to My Server Using HTML
How to Apply an Opacity Without Affecting a Child Element with HTML/Css
Right Align an Image Using CSS HTML
Why Are Dashes Preferred for CSS Selectors/HTML Attributes
Input Type=Password, Don't Let Browser Remember the Password
White Space Inside Xml/HTML Tags
Google Bot Crawling on Angularjs Site with HTML5 Mode Routes