3
A
回答
0
您可以查看Android附帶的音樂播放器的源代碼。 https://android.googlesource.com/platform/packages/apps/Music/
0
在某些時候,您在Android上播放音樂時必須處理這個問題。
http://developer.android.com/reference/android/media/MediaPlayer.html
0
這是一個簡單的一個很好的例子就明白了MP3播放器:https://github.com/saquibhafiz/MP3Player
或者,如果你寧願tutorail:
5
好的教程基本開始
- http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/
- http://alucard1990.hubpages.com/hub/How-to-Make-a-Simple-Media-Player-for-Android
- http://www.hrupin.com/2010/12/simple-android-mp3-media-player
- http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-i
- http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/
Android開發人員參考
+0
鏈接到blog.pocketjourney.com中的答案已經死亡。 – Pang 2018-02-15 05:18:49
1
一個完整的MP3播放器隨時可以集成到項目中: github.com/avafab/URLMediaPlayer
1
簡單的功能的媒體播放器,大部分的功能,
類代表樂曲
private class SongDetails {
String songTitle = "";
String songArtist = "";
//song location on the device
String songData = "";
}
函數將所有媒體文件標記爲mp3並將它們添加到arra Ÿ
private void getAllSongs()
{
//creating selection for the database
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
final String[] projection = new String[] {
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA};
//creating sort by for database
final String sortOrder = MediaStore.Audio.AudioColumns.TITLE
+ " COLLATE LOCALIZED ASC";
//stating pointer
Cursor cursor = null;
try {
//the table for query
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// query the db
cursor = getBaseContext().getContentResolver().query(uri,
projection, selection, null, sortOrder);
if (cursor != null) {
//create array for incoming songs
songs = new ArrayList<SongDetails>(cursor.getCount());
//go to the first row
cursor.moveToFirst();
SongDetails details;
while (!cursor.isAfterLast()) {
//collecting song information and store in array,
//moving to the next row
details = new SongDetails();
details.songTitle = cursor.getString(0);
details.songArtist = cursor.getString(1);
details.songData = cursor.getString(2);
songs.add(details);
cursor.moveToNext();
}
}
} catch (Exception ex) {
} finally {
if (cursor != null) {
cursor.close();
}
}
}
玩家和活動
public interface OnPlayerEventListener {
void onPlayerComplete();
void onPlayerStart(String Title,int songDuration,int songPosition);
}
類代表的媒體播放器
public class simplePlayer
{
OnPlayerEventListener mListener;
Activity mActivity;
//give access to the gui
public ArrayList<songDetails> songs = null;
public boolean isPaused = false;
public int currentPosition = 0;
public int currentDuration = 0;
//single instance
public static MediaPlayer player;
//getting gui player interface
public simplePlayer(Activity ma)
{
mActivity = ma;
mListener = (OnPlayerEventListener) mActivity;
}
//initialize the player
public void init(ArrayList<songDetails>_songs)
{
songs = _songs;
currentPosition = 0;
if(player == null)
{
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
player.stop();
player.reset();
nextSong();
mListener.onPlayerSongComplete();
}
});
}
}
//stop the current song
public void stop()
{
if(player != null)
{
if(player.isPlaying())
//stop music
player.stop();
//reset pointer
player.reset();
}
}
//pause the current song
public void pause()
{
if(!isPaused && player != null)
{
player.pause();
isPaused = true;
}
}
//playing the current song
public void play()
{
if(player != null)
{
if(!isPaused && !player.isPlaying())
{
if(songs != null)
{
if(songs.size() > 0)
{
try {
//getting file path from data
Uri u = Uri.fromFile(new File(songs.get(currentPosition).songData));
//set player file
player.setDataSource(mActivity,u);
//loading the file
player.prepare();
//getting song total time in milliseconds
currentDuration = player.getDuration();
//start playing music!
player.start();
mListener.onPlayerSongStart("Now Playing: "
+ songs.get(currentPosition).songArtist
+ " - "+ songs.get(currentPosition).songTitle
,currentDuration,currentPosition);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
else
{
//continue playing, reset the flag
player.start();
isPaused = false;
}
}
}
}
//playing the next song in the array
public void nextSong()
{
if(player != null)
{
if(isPaused)
isPaused = false;
if(player.isPlaying())
player.stop();
player.reset();
if((currentPosition + 1) == songs.size())
currentPosition = 0;
else
currentPosition = currentPosition + 1;
play();
}
}
//playing the previous song in the array
public void previousSong()
{
if(player != null)
{
if(isPaused)
isPaused = false;
if(player.isPlaying())
player.stop();
player.reset();
if(currentPosition - 1 < 0)
currentPosition = songs.size();
else
currentPosition = currentPosition -1;
play();
}
}
//getting new position for playing by the gui seek bar
public void setSeekPosition(int msec)
{
if(player != null)
player.seekTo(msec);
}
//getting the current duration of music
public int getSeekPosition()
{
if(player != null)
return player.getDuration();
else
return -1;
}
}
類的主要代表活動
public class MainActivity extends ActionBarActivity
implements OnPlayerEventListener {
simplePlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting all songs in the device
getAllSongs();
if (player == null) {
//create new instance and send the listener
player = new simplePlayer(this);
//initialize the simplePlayer
player.init(songs);
//start playing the first song
player.play();
seekBar.setMax(player.currentDuration);
}
}
@Override
public void onPlayerSongComplete() {
//need to be implement!
}
@Override
public void onPlayerSongStart(String Title, int songDuration, int songPosition) {
this.setTitle(Title);
seekBar.setMax(songDuration);
seekBar.setProgress(0);
}
}
好運之間
共享接口!
相關問題
- 1. MP3歌曲沒有媒體播放器播放
- 2. 如何在播放列表中拖放mp3歌曲
- 3. 播放來自SDcard的MP3歌曲
- 4. Windows Phone 8 mp3播放列表播放
- 5. 同時播放兩首mp3歌曲
- 6. 使用asp.net播放mp3歌曲
- 7. 音樂播放列表中的下一首歌曲不播放
- 8. 無法播放播放列表中的所有歌曲
- 9. 嘗試播放播放列表中的歌曲
- 10. 在默認音樂播放器上播放歌曲 - android
- 11. Android:使用默認音樂播放器播放歌曲文件
- 12. Android音樂播放器未播放所選歌曲
- 13. Android媒體播放器播放歌曲有序
- 14. android媒體播放器再次播放歌曲
- 15. 自動播放列表中的歌曲?
- 16. AxWindowsMediaPlayer播放歌曲
- 17. 播放DRM歌曲
- 18. 滾動播放的MP3播放器
- 19. 播放列表和Jplayer,無法播放其他歌曲
- 20. jQuery插件顯示播放列表(和播放歌曲)?
- 21. 如何從播放列表中播放歌曲
- 22. AJAX動態播放列表不會播放歌曲
- 23. 「最近播放」列表的自定義MP3播放器
- 24. 來自XML播放列表的Flash MP3播放器循環
- 25. Android:在播放新歌之前停止播放歌曲。
- 26. 從默認Android播放器(從代碼)播放播放列表
- 27. 有沒有Flash播放器播放MP3
- 28. 流媒體播放器不播放MP3
- 29. JACO MP3播放器
- 30. 播放第一首歌曲後,我的播放器停止播放
http://www.streamhead.com/android-tutorial-sd-card/ – 2012-04-12 08:50:23
這可能會幫助您朝正確的方向發展:http://code.google.com/p/rockon-android/source/browse /#svn/trunk/src/org/abrantes/filex – kiswa 2010-06-29 16:06:07