2016-03-05 165 views
0

我想創建一個Android音樂播放器,如播放音樂,所以我從谷歌github導入通用音樂播放器,但我不知道在哪裏插入代碼,以便從本地存儲讀取音樂。我知道一切都提供背景音樂服務,通知欄等..但我不知道如何從手機存儲讀取音樂文件。Android - 爲Google Play音樂創建Android音樂播放器?

環球音樂播放器 - https://github.com/googlesamples/android-UniversalMusicPlayer

我已在Android的一些簡單的應用程序,但沒有建立這種複雜的任何應用程序。我介於初學者和中級之間。請幫忙!

+0

看看如何使用內部存儲:http://developer.android.com/guide/topics/data/data-storage.html#filesInternal – reinder

回答

0

建議

嗨!用簡單的音樂播放器查看my repository。歌曲從外部存儲裝載。 Pre-Lollipop支持。

SOLUTION

至於你的問題,用歌聲負載的解決方案:你可以加載從外部存儲的歌曲是這樣的:

私人ArrayList的歌曲=新的ArrayList <>(); //宋是一些數據模型

public void getSongList() { 
     ContentResolver musicResolver = getContentResolver(); 
     Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
     Uri artworkUri = Uri.parse("content://media/external/audio/albumart"); 

     Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null); 

     if (musicCursor != null && musicCursor.moveToFirst()) { 
      // song title 
      int titleColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media.TITLE); 
      // song unique id 
      int idColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media._ID); 
      // song's artist 
      int artistColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media.ARTIST); 
      // path to album art 
      int albumIdColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media.ALBUM_ID); 

      do { 
       long thisId = musicCursor.getLong(idColumn); 
       String thisTitle = musicCursor.getString(titleColumn); 
       String thisArtist = musicCursor.getString(artistColumn); 
       long albumId = musicCursor.getLong(albumIdColumn); 
       String albumArt = ""; 

       try { 
        albumArt = (ContentUris.withAppendedId(artworkUri, albumId)).toString(); 
        Log.d(TAG, "TEST Album art: " + albumArt); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
       // Adding new song to ArrayList 
       songs.add(new Song(thisId, thisTitle, thisArtist, albumArt)); 
      } 
      while (musicCursor.moveToNext()); 
     } 

     if (musicCursor != null) 
      musicCursor.close(); 
    }