Sigh-AniDe
Sigh-AniDe

Reputation: 237

audio file not playing in silverlight

I am trying to play an audio file in silverlight using WCF, language C#, i dont get any errors in my app, although the mp3 would not play.

Here is what i have so far:

stream fileStream;
private void DownloadMusic_Click( object sender, RoutedEventArgs e )//this is the button that plays the music
    {
        FileServiceClient FSC = new FileServiceClient();
        FSC.DownloadTestCompleted += new EventHandler<DownloadTestCompletedEventArgs>( FSC_DownloadTestCompleted );
        FSC.DownloadTestAsync( FileSearch.Text );
    }
void FSC_DownloadTestCompleted( object sender, DownloadTestCompletedEventArgs e )
    {
        FileObject fileObject;
        if ( e.Error == null )
        {
            if ( e.Result != null )
            {
                fileObject = e.Result;
                fileStream = new MemoryStream( fileObject.FileStream );
                mediaElement1.SetSource( fileStream );
                mediaElement1.AutoPlay = false;
                mediaElement1.Play();
                ResultBlock.Text = "Song is playing . . .";
            }
            else
            {
                ResultBlock.Text = "Song could not be found";
            }
        }
    }

The service:

public FileObject DownloadTest( string fileName )
    {
        FileStream fileStream = null;
        BinaryReader reader = null;
        string filePath = "";
        byte[] fileBytes;

        try
        {
            filePath = Path.Combine( HttpContext.Current.Server.MapPath( "." ), "Pictures", fileName);

            if ( File.Exists( filePath ) )
            {
                fileStream = new FileStream( filePath, FileMode.Open, FileAccess.Read );
                reader = new BinaryReader( fileStream );
                fileBytes = reader.ReadBytes( (int)fileStream.Length );

                return new FileObject() { FileName = fileName, FileStream = fileBytes };
            }
            return null;
        }
        catch ( Exception )
        {
            return null;
        }
    }

The interface:

 namespace WCF_Silverlight_UploadFile.Web
 {
  [ServiceContract]
  public interface IFileService
  {

    [OperationContract]
    bool UploadTest(FileObject file);

    [OperationContract]
    FileObject DownloadTest(string fileName);

   }

[DataContract]
public class FileObject
{
    [DataMember]
    public string FileName { get; set; }

    [DataMember]
    public byte[] FileStream { get; set; }

    [DataMember]
    public string FileType { get; set; }
 }
}

All help will be appreciated. Thanks

Upvotes: 1

Views: 523

Answers (2)

Sigh-AniDe
Sigh-AniDe

Reputation: 237

Somehow changing the .AutoPlay to true worked. I can't understand why but it works. There is, however, a 2 second delay before the music starts to play.

mediaElement1.AutoPlay = true;

Thanks for the help.

Upvotes: 0

Amar Palsapure
Amar Palsapure

Reputation: 9680

Try to put fileStream.Seek(0, SeekOrigin.Begin); statement before your mediaElement1.SetSource( fileStream );.

Your code should look like this:

fileObject = e.Result;
fileStream = new MemoryStream( fileObject.FileStream );
fileStream.Seek(0, SeekOrigin.Begin);
mediaElement1.SetSource( fileStream );
mediaElement1.AutoPlay = false;

Read more about Seek() on MSDN.

This might help you.

Upvotes: 1

Related Questions