Play Sound File in a Web-Page in the Background

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

  1. in your application info.plist file add "Application does not run in background" to "YES"
  2. in your application info.plist file add "YES"Required background modes" and set "App plays audio"
  3. 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



Leave a reply



Submit