Saturday, October 6, 2012

Play Attachment songs in Detail Page:

Play Attachment songs in Detail Page:
Visualforce page gives the ability to play the songs with in the page instead of download and play.
Here is the example for lead ,You can download the player Here
public without sharing class PlayAudio {
    
    public List<Attachment> listAttachment {get;set;}
    public Lead lead {get;set;}
    
    //constructor
    public PlayAudio(ApexPages.StandardController controller) {
        
        //get the controller instance
        lead = (Lead)controller.getRecord();
        
        //initialize the attachments list
        listAttachment = new List<Attachment>();
        
        //get all the lead related attachments
        listAttachment = [Select Id, ContentType, Name from Attachment where ParentId = :lead.Id];
        
        //get all the related tasks attachments
        List<Task> listTask = [Select WhatId from Task where WhoId =: lead.Id ];
        listAttachment.addAll([Select Id, ContentType, Name from Attachment where ParentId IN :listTask]);
        
        //get all the related events attachments
        List<Event> listEvent = [Select WhatId from Event where WhoId =: lead.Id ];
        listAttachment.addAll([Select Id, ContentType, Name from Attachment where ParentId IN :listEvent]);
        
    }
    
    @isTest
    private static void testPlayAudio()
        {
            //create a lead record
            Lead lead = new Lead(LastName='test', Company='test');
            insert lead;
            
            //start test from here 
            Test.startTest();
            
            PlayAudio controller = new PlayAudio(new ApexPages.StandardController(lead));
            
            System.assert(lead != null);
            //stop test here
            Test.stopTest();
        }

}
and the page looks like this

<apex:page standardController="Lead" id="pg" extensions="PlayAudio" sidebar="false">
<apex:includeScript value="{!URLFOR($Resource.AudioPlayer, 'audio-player/audio-player.js')}"/>
<script type="text/javascript">  
  AudioPlayer.setup("{!URLFOR($Resource.AudioPlayer, 'audio-player/player.swf')}", {  
   width: 290  
  });  
</script> 

<script type="text/javascript">  
 function playAudio(trackObj)
  {
   AudioPlayer.embed("audioplayer_1", {soundFile: trackObj});
  }  
</script>
<apex:form id="frm">
 <apex:pageBlock id="pb">
  <apex:variable value="{!0}" var="count" />
  <apex:pageBlockTable value="{!listAttachment}" var="item" id="pbt">
   <apex:column headerValue="Name">
    {!item.Name}
   </apex:column>
   <apex:column headerValue="ContentType">
    {!item.ContentType}
   </apex:column>
   <apex:column id="colPlayer" rendered="{!CONTAINS(item.ContentType, 'audio')}">
    <p id="{!count}.audioplayer"></p>
    <script type="text/javascript">
     AudioPlayer.embed("{!count}.audioplayer", {soundFile: 'https://c.na9.content.force.com/servlet/servlet.FileDownload?file={!item.Id}'});
    </script>
    <apex:variable value="{!count+1}" var="count" />
   </apex:column>
  </apex:pageBlockTable>
 </apex:pageBlock>
</apex:form>
</apex:page>
             

No comments:

Post a Comment