2013-10-02 93 views
0

我想在我的WP 8的应用程序来播放mp3,我想忘记的东西,你可以请帮的Windows Phone 8播放MP3的问题

我的简化代码,因为它有云:

在page.xaml的.cs代码:

public void Play(object sender, RoutedEventArgs e) 
     { 


       if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState) 
       { 
        BackgroundAudioPlayer.Instance.Pause(); 
       } 
       else 
       { 
        BackgroundAudioPlayer.Instance.Play(); 
       } 

     } 

在App.xaml.cs代码:

using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       string[] files = new string[] { "song.mp3"}; 

       foreach (var _fileName in files) 
       { 
        if (!storage.FileExists(_fileName)) 
        { 
         string _filePath = "Sounds/" + _fileName; 
         StreamResourceInfo resource = Application.GetResourceStream(new Uri(_filePath, UriKind.Relative)); 

         using (IsolatedStorageFileStream file = storage.CreateFile(_fileName)) 
         { 
          int chunkSize = 4096; 
          byte[] bytes = new byte[chunkSize]; 
          int byteCount; 

          while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0) 
          { 
           file.Write(bytes, 0, byteCount); 
          } 
         } 
        } 
       } 
      } 
     } 

我可以看到我BackgroundAudioPlayer.Instance状态永不改变,但我不明白为什么(播放功能被触发)

+0

如果你告诉它什么曲目/文件播放? –

+0

很好通常我使用SoundEffect.Stream控制,我不熟悉 –

回答

1

您需要告诉BackgroundAudioPlayer播放哪个曲目。
喜欢的东西:

var track = new AudioTrack(
          new Uri("/song.mp3", UriKind.Relative), 
          "song name", 
          "artist name", 
          "album name", 
          null); // no artwork 
BackgroundAudioPlayer.Instance.Track = track; 
BackgroundAudioPlayer.Instance.Play(); 
相关问题