I. Develop a music player:
1. UI file of the music player: a text box that can enter the path of the music file to be played, and four buttons:
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
Android: textSize = "20sp"/>
Android: id = "@ + id/et_path"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
// When the interface is opened, the text box gets the focus
Android: layout_width = "fill_parent"
>
Android: gravity = "center_horizontal" // horizontally centered display of the specified component
Android: orientation = "horizontal">
Android: id = "@ + id/play"
Android: layout_width = "0dip" // set the button to 0 in width.
Android: layout_height = "wrap_content"
Android: layout_weight = "1" // set the button weight to 1
Android: text = "play"/>
Android: id = "@ + id/pause"
Android: layout_width = "0dip"
Android: layout_height = "wrap_content"
>
Android: text = "paused"/>
Android: id = "@ + id/replay"
Android: layout_width = "0dip"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
>
Android: id = "@ + id/stop"
Android: layout_width = "0dip"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: text = "stop"/>
2. Player playback, replay, pause, and stop operations.
① Playback:
// Create a MediaPlayer object
MediaPlayer>
// Set the playback type
MPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );
// Set the playback file path
MPlayer. setDataSource (path );
// Note: very important
MPlayer. prepare (); // call the underlying c code to complete>
}
});
// Start playing:
MPlayer. start ();
② Pause:
// MPlayer. isPlaying () determines whether the video is being played.
If (mPlayer! =>
>
}
/Create a MediaPlayer object
MediaPlayer>
// Set the playback type
MPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );
// Set the playback file path
MPlayer. setDataSource (path );
// Note: very important
MPlayer. prepare (); // call the underlying c code to complete>
}
});
// Start playing:
MPlayer. start ();
④ Stop:
If (mPlayer! =>
}
2. display the lyrics with music:
1. parse the lyrics file. lrc file first.
① The style of the lyrics file is:
[. 93] Love in the village
[. 36] The back of the river
[. 53] cuddling in a small village
[. 82] Flowers in the blue sky
② Parse the. lrc file, read the lyrics in one row, parse the time and lyrics, use the time as the key, and the corresponding lyrics as the value, and place them in the map set.
Map>
String>
>
>
String>
SimpleDateFormat>
Date>
System. out. println (>
Map. put (date, value );
2. perform the following operations to display the lyrics during playback:
Public>
>
// Handler used to modify the lyrics
>
@ Override
>
// Take the time as the key and display the lyrics in the map:
}
};
@ Override
>
>
>
>
>
>
>
>
>
>
>
>
}
>
>
>
}
>
>
}
Play (path );
Break;
}
}
Private void play (String path ){
Try {
>
>
}
});
MPlayer. start ();
>
//>
// Load the audio file into the sound pool; return the id
// Method parameter 1: Context
Parameter 2: Specifies the Audio Resource file
Parameter 3:
Int>
}
// Click the fire button:
Public void fire (View view ){
// Play the sound in the sound pool: play () method
Method parameter 1: Sound id
Parameter 2: Left-channel size,
Parameter 3: Right-channel sound size,
Parameter 4: specify the priority. The value 0 indicates the lowest priority.
Parameter 5: Specifies the playing mode of the sound. The value 0 indicates playing only once, and-1 indicates permanent loop.
Parameter 6: Specifies the playback speed.
Soundpool. play (soundid, 1.0f, 1.0f, 0, 0, 1.0f );
}
③ The Call of the play () method is asynchronous.