2015-12-29 63 views
0

我是新的android應用程序開發。我正在開發一個項目。包含100個音頻文件。所有我都被放置在資產文件夾中。用於播放音頻我需要一個媒體播放器類在bluestack應用播放器播放音頻工作正常嗎?但不是在設備

MusicPlayer.java 


/** Plays music and one-off sound files while managing the resources efficiently */ 
public class MusicPlayer { 

    private static MusicPlayer mInstance; 
    private MediaPlayer mMediaPlayer; 

    private MusicPlayer() { } 

    /** 
    * Returns the single instance of this class 
    * 
    * @return the instance 
    */ 
    /* */ 

    public static MusicPlayer getInstance() { 

     if (mInstance == null) { 
      mInstance = new MusicPlayer(); 
     } 

     return mInstance; 
    } 

    /** 
    * Plays the sound with the given resource ID 
    * 
    * @param context a valid `Context` reference 
    * @param soundResourceId the resource ID of the sound (e.g. `R.raw.my_sound`) 
    */ 
    public synchronized void play(final Context context, final int soundResourceId) { 
     // if there's an existing stream playing already 
     if (mMediaPlayer != null) { 
      // stop the stream in case it's still playing 
      try { 
       mMediaPlayer.stop(); 
      } 
      catch (Exception e) { } 

      // release the resources 
      mMediaPlayer.release(); 

      // unset the reference 
      mMediaPlayer = null; 
     } 

     // create a new stream for the sound to play 
     mMediaPlayer = MediaPlayer.create(context.getApplicationContext(), soundResourceId); 

     // if the instance could be created 
     if (mMediaPlayer != null) { 
      // set a listener that is called when playback has been finished 
      mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 

       @Override 
       public void onCompletion(final MediaPlayer mp) { 
        // if the instance is set 
        if (mp != null) { 
         // release the resources 
         mp.release(); 

         // unset the reference 
         mMediaPlayer = null; 
        } 
       } 

      }); 

      // start playback 
      mMediaPlayer.start(); 
     } 
    } 

    public synchronized void stop() { 
     // if there's an existing stream playing already 

       mMediaPlayer.stop(); 

      // release the resources 
      mMediaPlayer.release(); 

      // unset the reference 
      mMediaPlayer = null; 
     } 

    public synchronized void isPlaying() { 
     // if there's an existing stream playing already 
if(mMediaPlayer != null) 
{ 
    mMediaPlayer.stop(); 

    // release the resources 
    mMediaPlayer.release(); 

    // unset the reference 

} 

    } 
} 

的活動我用這樣的

MusicPlayer.getInstance().play(Activity.this, R.raw.gajju_urturn); 

當我運行在bluestack應用播放器所有的音頻正在播放的項目。但是當我運行在真正的設備不播放音頻和獲取錯誤。 PLZ任何一個幫我解決問題。在我的logcat顯示以下錯誤...

12-29 15:02:23.076 219-18360/? E/GenericSource: Failed to init from data source! 
12-29 15:02:23.077 18346-18358/? E/MediaPlayer: error (1, -2147483648) 
12-29 15:02:34.130 219-18366/? E/GenericSource: Failed to init from data source! 
12-29 15:02:34.131 18346-18357/? E/MediaPlayer: error (1, -2147483648) 
12-29 15:02:38.100 219-18368/? E/GenericSource: Failed to init from data source! 
12-29 15:02:38.101 18346-18357/? E/MediaPlayer: error (1, -2147483648) 
12-29 15:02:53.618 219-18370/? E/GenericSource: Failed to init from data source! 
12-29 15:02:53.620 18346-18358/? E/MediaPlayer: error (1, -2147483648) 
12-29 15:03:03.138 219-18373/? E/GenericSource: Failed to init from data source! 
12-29 15:03:03.138 18346-18358/? E/MediaPlayer: error (1, -2147483648) 

回答

0

與我一起工作很好。檢查資源格式,並檢查您的「原始」目錄是否包含請求的文件。另外在另一部手機上運行你的代碼。我已經使用了相同的代碼

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    MusicPlayer.getInstance().play(this, R.raw.no_promises); 
} 
+0

感謝man的迴應..對某些文件擴展名爲.wma – siddhu

相關問題