Implementing src attribute to HTML5 audio tag utilizing angularjs

Currently, I am in the process of creating a music website and facing an issue where I need to pass the name of the file when the user clicks on the music cover image to the HTML5 audio tag. My approach involves playing songs using PHP, but it requires a page refresh before the audio can start playing. I aim to achieve this functionality with AngularJS so that the page remains dynamic without the need for refreshing.

To optimize efficiency, I have decided to store the file name as the Image Name. This saves me from accessing the database repeatedly to retrieve the file name. Here is my code:

<div class="container" ng-app="myapp" ng-controller="myController" ng-init="select()">
  <div class="col-md-3" ng-repeat="file in files">
    <a href="?play={{file.songname}}"><img ng-src="mainpagecontent/{{file.name}}" name="{{file.songname}}" ng-init="select()" onclick=""width="220" height="220" style="padding:16px; margin:2px;" alt="music"/>
      <br/>

    <label style="margin-left:20px;padding-left:5px ">{{file.songname}}</label></a>
  </div>
</div>

I would greatly appreciate any assistance or guidance provided regarding this matter.

Answer №1

If you're trying to figure out how to initiate audio playback with a click on an image, it's a bit tricky without seeing more of your code. However, here's a basic example that can guide you in the right direction:

app.js

$onInit(){
  this.track1 = 'https://www.example.com/track1.mp3';
  this.track2 = 'https://www.example.com/track2.mp3';
  this.trackToPlay = this.track1;
}

switchTrack(){
  this.trackToPlay = (this.track === this.track1) ? this.track2 : this.track1;
}

index.html

<div>Audio track URL: {{$ctrl.trackToPlay}}</div>
<hr>
<button ng-click="$ctrl.playTrack()">Play</button>
<audio
    id="test"
    controls
    src="{{$ctrl.trackToPlay}}">
    Your browser does not support the <code>audio</code> element.
</audio>

I hope this example is helpful for your project!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When there is no content in the responseText, AJAX will display an error

I've encountered an issue while trying to fetch data using AJAX. The problem lies in receiving an empty responseText. Here's the code I'm working with: JavaScript: function getFounder(id) { var founder = ""; $.ajax({ ...

Move the box upwards and change it to grayscale when hovering over the image, then reverse the effect when the hover

As I hover over the image, my goal is to make a gray box appear at the bottom and smoothly slide up. While it slides up, everything it covers should turn grayscale. When I move my mouse away from the image, I want the gray box to slide back down and remove ...

Incorporating external function from script into Vue component

Currently, I am attempting to retrieve an external JavaScript file that contains a useful helper function which I want to implement in my Vue component. My goal is to make use of resources like and https://www.npmjs.com/package/vue-plugin-load-script. Thi ...

Modifying Copyright Feature in Footer

I am attempting to implement this function within my dark-colored footer: import Typography from '@material-ui/core/Typography'; function Copyright() { return ( <Typography variant="body2" color="textSecondary" align="center"> ...

Auto play feature malfunctioning in Onsen-UI Carousel attribute settings

When utilizing onsen UI in my mobile web application, I encountered an issue with the autoplay property not functioning properly within the carousel. <ons-page> <ons-toolbar> <div class="left"> <ons-toolbar-button on ...

What is the process for exporting Three.js files to .stl format for use in 3D printing?

I stumbled upon a page with this link: Is it possible to convert Three.js to .stl for 3D printing? var exporter = new THREE.STLExporter(); var str = exporter.parse(scene); console.log(str); However, despite using the code provided, I am unable to export ...

Using protractor and AngularJS to extract text content from two labels by identifying the elements

My app is organized like this: <ul ng-repeat="variable in link.variables" ng-show="SearchboxCtrl.showData" class="ng-scope"> <li class="ng-binding"> Member Since: </li> <span class="ng-binding"> 2010-01-01 </span></ul& ...

Understanding intricate JSON structures with JavaScript

Here is the JSON structure that I am working with: var data = [ { "country":"Andorra", "code":"AD", "state":[ { "state_code":"AD1", "state_description":"aaAndorra1" }, { " ...

Determining the location of the cursor within 'ng2-ckeditor'

I'm a beginner with Angular 2 and I'm using 'ng2-ckeditor 1.0.7' in my Angular 2 application. The editor is functioning well within the app. However, I am now faced with the challenge of appending text at the cursor position. Unfortunat ...

Storing data in the browser's LocalStorage after a Vue3 component has

Whenever a user successfully logs into the application, a nav bar is supposed to load user data. However, there seems to be a timing issue with localStorage being set slightly after the nav bar is loaded. Using a setTimeout() function resolves the issue, b ...

Would it be considered improper to implement an endless loop within a Vue.js instance for the purpose of continuously generating predictions with Tensorflow.js?

In my project, I am leveraging different technologies such as Tensorflow.js for training and predicting foods, Web API to access the webcam in my notebook, and Vue.js to create a simple web page. Within the addExample() method, there is an infinite loop r ...

Using a URL parameter inside an AJAX call

Greetings! I am looking to incorporate an ajax function for data transfer to my php page while utilizing the $_GET variable for verification purposes. This is the code snippet that I have implemented: $.ajax({ type: "POST", url: "coords ...

What is the functionality of client-side applications?

I am new to web development and my programming background is primarily focused on algorithms and creating visualization tools using local Windows forms. I often distribute these tools via email as compiled exe files (C++ or C# win form) to my math students ...

What is the best way to arrange an array of identical objects based on a specific sub property?

Here's an array of items to work with: myArray = [ { someDate: "2018-01-11T00:00:00", name: "John Smith", level: 5000 }, { someDate: "2017-12-18T00:00:00", name: "Jane Doe", level: 1000 }, { ...

Accessing values from a JSON object in AngularJS without using a loop based on another field

Is there a way to extract the "value" associated with the "name" in JSON using Angular JS without having to iterate through the array? For instance, if I have an array like the one below with name and value fields, how can I retrieve the value "ABC" based ...

I'm facing issues with Webpack not being able to resolve and locate the node

Encountering difficulties while setting up and running the Three.js library as a module based on the guidelines provided in the manual at Here is a summary of the steps taken: Created package.json npm init Installed webpack npm i --save-dev webpack we ...

How can you provide unique css box shadows for various browsers without relying on browser detection?

I have observed that each browser displays box shadow blur radius a bit differently, so I want to ensure consistency across all browsers. However, since they use the unprefixed version, I need to provide different stylesheets for each browser. What is the ...

Attempting to employ the HTML5 file picker feature within the AppMaker platform

Hey there, I'm currently working on integrating the HTML5 file picker into my AppMaker app. (Since my app needs to run as the developer, I can't use Drive Picker). I've been able to display the file picker in an HTML widget using the follow ...

Understanding the Usage of FormData in NextJS

I'm trying to read fetch's body contents. Here's the code I'm using: fetch('/api/foo', { method: 'POST', body: new FormData(formRef.current), }); https://i.sstatic.net/6YB1V.png Now I need to parse the body dat ...

Self-contained VUE app

Can Vue.js be built as a standalone application so that I am not reliant on backend services from my web hosting provider? In essence, I would like to avoid having to start up the app through npm and instead simply open the index.html file from the dist f ...